//
//  ResourceUploadAction.java
//  blogwt
//
//  Created by William Shakour (billy1380) on 11 Aug 2015.
//  Copyright © 2015 WillShex Limited. All rights reserved.
//
package com.willshex.blogwt.server.upload;

import gwtupload.server.exceptions.UploadActionException;
import gwtupload.server.gae.CloudStorageFileItemFactory.CloudStorageFileItem;
import gwtupload.server.gae.CloudStorageUploadAction;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileItem;

import com.willshex.blogwt.server.service.resource.ResourceServiceProvider;
import com.willshex.blogwt.shared.api.datatype.Resource;

/**
 * @author William Shakour (billy1380)
 *
 */
public class ResourceUploadAction extends CloudStorageUploadAction {
	private static final long serialVersionUID = 4092355669644652816L;

	/* (non-Javadoc)
	 * 
	 * @see
	 * gwtupload.server.gae.CloudStorageUploadAction#executeAction(javax.servlet
	 * .http.HttpServletRequest, java.util.List) */
	@Override
	public String executeAction (HttpServletRequest request,
			List<FileItem> sessionFiles) throws UploadActionException {
		StringBuffer resourcesJson = new StringBuffer();
		Resource resource;
		final Date now = new Date();
		for (FileItem i : sessionFiles) {
			if (!i.isFormField()) {
				resource = new Resource();
				resource.created = now;
				resource.data = "gs://"
						+ ((CloudStorageFileItem) i).getKey().getKeyString();
				resource.description = "New uploaded file " + i.getName();
				resource.name = i.getName();
				resource.properties = "{\"contentType\":" + i.getContentType()
						+ "}";
				resource = ResourceServiceProvider.provide().addResource(
						resource);

				if (resourcesJson.length() != 0) {
					resourcesJson.append(" ");
				}

				resourcesJson.append(resource.id.toString());
			}
		}

		return resourcesJson.toString();
	}
}

@@ -539,16 +539,135 @@
 eItem;%0A%0A
+import com.google.appengine.api.images.ImagesServiceFactory;%0Aimport com.google.appengine.api.images.ServingUrlOptions;%0A
 import c
@@ -1639,32 +1639,405 @@
  + i.getName();%0A
+%0A%09%09%09%09// try to get a permanent url for an image, if it works%0A%09%09%09%09// add it to the description%0A%09%09%09%09try %7B%0A%09%09%09%09%09resource.description += %22%5CnDirect url:%5Cn%22%0A%09%09%09%09%09%09%09+ ImagesServiceFactory.getImagesService()%0A%09%09%09%09%09%09%09%09%09.getServingUrl(ServingUrlOptions.Builder%0A%09%09%09%09%09%09%09%09%09%09%09.withBlobKey(%0A%09%09%09%09%09%09%09%09%09%09%09%09%09((CloudStorageFileItem) i)%0A%09%09%09%09%09%09%09%09%09%09%09%09%09%09%09.getKey()));%0A%09%09%09%09%7D catch (Throwable e) %7B%7D%0A%0A
 %09%09%09%09resource.nam
@@ -2181,16 +2181,23 @@
 rovide()
+%0A%09%09%09%09%09%09
 .addReso
@@ -2201,23 +2201,16 @@
 esource(
-%0A%09%09%09%09%09%09
 resource

add a static url for images to the resource description