share-updates/src/main/java/org/gcube/portlets/user/shareupdates/server/UploadToWorkspaceThread.java

94 lines
3.0 KiB
Java
Raw Normal View History

package org.gcube.portlets.user.shareupdates.server;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
2021-03-25 12:19:27 +01:00
import java.util.UUID;
import org.gcube.common.scope.api.ScopeProvider;
2021-03-25 12:19:27 +01:00
import org.gcube.common.storagehub.client.dsl.FileContainer;
import org.gcube.common.storagehub.client.dsl.FolderContainer;
import org.gcube.common.storagehub.client.dsl.OpenResolver;
import org.gcube.common.storagehub.client.dsl.StorageHubClient;
import org.gcube.common.storagehub.model.exceptions.InvalidItemException;
2021-04-01 16:25:12 +02:00
import org.gcube.common.storagehub.model.exceptions.StorageHubException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Massimiliano Assante ISTI-CNR
*
*/
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
*/
private String username;
/**
* the identifier of the workspace you are putting
*/
private String fullName;
/**
* the name of the file you are putting
*/
private String fileName;
/**
* the path (with name) of the file you are putting
*/
private String fileabsolutePathOnServer;
/**
*
* @param sClient the instance of the storage client
* @param fileToUpload the absolute path of the file
*/
public UploadToWorkspaceThread(String fullName, String username, String fileName, String fileabsolutePathOnServer) {
super();
this.username = username;
this.fullName = fullName;
this.fileName = fileName;
this.fileabsolutePathOnServer = fileabsolutePathOnServer;
}
@Override
public void run() {
try {
String currScope = ScopeProvider.instance.get();
2021-03-25 12:19:27 +01:00
StorageHubClient shc = new StorageHubClient();
_log.info("Trying to get VRE folder for scope="+currScope);
FolderContainer vreFolder = shc.openVREFolder();
_log.info("File to upload="+fileabsolutePathOnServer);
File file = new File(fileabsolutePathOnServer);
InputStream fileData = new FileInputStream(file);
2021-03-25 12:19:27 +01:00
FolderContainer attachmentFolder = null;
try {
OpenResolver oRes = vreFolder.openByRelativePath(ATTACHMENT_FOLDER);
attachmentFolder = oRes.asFolder();
2021-04-01 16:25:12 +02:00
} catch (StorageHubException e) {
2021-03-25 12:19:27 +01:00
_log.info(ATTACHMENT_FOLDER + " in VRE Folder does not exists, creating it for "+currScope);
vreFolder.newFolder(ATTACHMENT_FOLDER,"Folder created automatically by the System");
}
2021-03-25 12:19:27 +01:00
String itemName = getUniqueName(fileName);
FileContainer uploadedFile = attachmentFolder.uploadFile(fileData, itemName, "File shared by " + fullName + "("+username+")");
_log.debug("Uploaded " + uploadedFile.get().getName() + " - Returned Workspace id=" + uploadedFile.getId());
}
catch (Exception e) {
e.printStackTrace();
2021-03-25 12:19:27 +01:00
_log.error("Something wrong while uploading " + fileName + " in Workspace VRE Folder " + ATTACHMENT_FOLDER + ": "+ e.getMessage());
}
}
2021-03-25 12:19:27 +01:00
private String getUniqueName(String filename) {
return UUID.randomUUID().toString().substring(0, 8) + "_" + filename;
}
}