- Also save a copy in my Workspace
+ Also save a copy in the Workspace (group folder)
\ No newline at end of file
diff --git a/src/main/java/org/gcube/portlets/user/shareupdates/server/UploadToWorkspaceThread.java b/src/main/java/org/gcube/portlets/user/shareupdates/server/UploadToWorkspaceThread.java
index a1ef463..89a2c5a 100644
--- a/src/main/java/org/gcube/portlets/user/shareupdates/server/UploadToWorkspaceThread.java
+++ b/src/main/java/org/gcube/portlets/user/shareupdates/server/UploadToWorkspaceThread.java
@@ -3,11 +3,13 @@ package org.gcube.portlets.user.shareupdates.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
-import java.util.Date;
import org.gcube.common.homelibrary.home.HomeLibrary;
import org.gcube.common.homelibrary.home.workspace.Workspace;
-import org.gcube.common.homelibrary.home.workspace.exceptions.ItemAlreadyExistException;
+import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
+import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
+import org.gcube.common.homelibrary.home.workspace.folder.FolderItem;
+import org.gcube.common.homelibrary.util.WorkspaceUtil;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.scope.api.ScopeProvider;
import org.slf4j.Logger;
@@ -20,6 +22,7 @@ import org.slf4j.LoggerFactory;
*/
public class UploadToWorkspaceThread implements Runnable {
private static Logger _log = LoggerFactory.getLogger(UploadToWorkspaceThread.class);
+ public static final String ATTACHMENT_FOLDER ="Shared attachments";
/**
* the identifier of the workspace you are putting
*/
@@ -50,41 +53,39 @@ public class UploadToWorkspaceThread implements Runnable {
try {
String currScope = ScopeProvider.instance.get();
ScopeProvider.instance.set("/"+PortalContext.getConfiguration().getInfrastructureName());
-
+
Workspace ws = HomeLibrary
.getHomeManagerFactory()
.getHomeManager()
.getHome(username).getWorkspace();
-
+
_log.info("File to upload="+fileabsolutePathOnServer);
File file = new File(fileabsolutePathOnServer);
- String mimeType = FilePreviewer.getMimeType(file, fileName);
InputStream fileData = new FileInputStream(file);
- String theId = "";
- _log.info("mimeType="+mimeType + " fileData null? " + (fileData == null) );
- try {
- theId = ws.createExternalFile(fileName ,"File added automatically by Share Updates" , mimeType ,fileData, ws.getRoot().getId()).getId();
+
+ _log.info("Trying to get Group folder for scope="+currScope);
+ WorkspaceFolder folder = ws.getVREFolderByScope(currScope);
+ WorkspaceFolder attachment = null;
+ try{
+ attachment = (WorkspaceFolder) ws.getItemByPath(folder.getPath() + "/" + ATTACHMENT_FOLDER);
+ } catch (ItemNotFoundException e) {
+ _log.info(ATTACHMENT_FOLDER + " Workspace Folder does not exists, creating it for "+currScope);
+ Workspace ownerWS = HomeLibrary
+ .getHomeManagerFactory()
+ .getHomeManager()
+ .getHome(folder.getOwner().getPortalLogin()).getWorkspace();
+ attachment = ownerWS.createFolder(ATTACHMENT_FOLDER, "Folder created automatically by the System", folder.getId());
}
- catch (NullPointerException exn) {
- _log.warn("null pointer");
- exn.printStackTrace();
- }
- catch (ItemAlreadyExistException ex) {
- _log.warn("fileName " + fileName + " exists, appending timestamp");
- theId = ws.createExternalFile(fileName+" ("+ new Date()+")" ,"File added automatically by Share Updates" , mimeType ,fileData, ws.getRoot().getId()).getId();
- ex.printStackTrace();
- } finally {
- fileData.close();
- }
- fileData.close();
- _log.debug("Uploaded " + fileName + " - Returned Workspace id=" + theId);
- ScopeProvider.instance.set(currScope);
- }
+ String itemName = WorkspaceUtil.getUniqueName(fileName,attachment);
+ FolderItem item = WorkspaceUtil.createExternalFile(attachment, itemName, "File added automatically by Share Updates", null, fileData);
+
+ _log.debug("Uploaded " + item.getName() + " - Returned Workspace id=" + item.getId());
+ ScopeProvider.instance.set(currScope);
+ }
catch (Exception e) {
e.printStackTrace();
_log.error("Something wrong while uploading " + fileName + " in Workspace " + e.getMessage());
}
}
-
}