Added uploadArchive

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/widgets/workspace-uploader@171508 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Francesco Mangiacrapa 2018-09-18 15:01:31 +00:00
parent 2a64c70052
commit 19a6deac59
2 changed files with 157 additions and 107 deletions

View File

@ -32,6 +32,7 @@ import org.apache.commons.fileupload.util.Streams;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;
import org.gcube.common.homelibrary.util.MimeTypeUtil;
import org.gcube.common.portal.PortalContext;
import org.gcube.common.storagehub.model.items.FolderItem;
import org.gcube.common.storagehub.model.items.Item;
@ -550,26 +551,26 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
}else {//IS ARCHIVE UPLOAD
//TODO UPLOAD ARCHIVE
// logger.debug("Archive content type: "+uploadItem.getContentType());
//
// if (MimeTypeUtil.isZipContentType(uploadItem.getContentType())){ //UNZIP??
// logger.debug("Unziping content");
// //THE WORKSPACE HAS BEEN INSTANCIED ABOVE
// workspaceUploader = WorkspaceUploaderMng.uploadArchive(user, scopeGroupId, workspaceUploader, request, itemName, inputStream, destinationFolder, size);
//
// if(workspaceUploader==null)
// throw new Exception("Error when creating uploader, it is null!");
//
// sendMessage(response, workspaceUploader.getIdentifier());
// } else{
// //THE WORKSPACE HAS BEEN INSTANCIED ABOVE
// workspaceUploader = WorkspaceUploaderMng.uploadFile(user, scopeGroupId, request, workspaceUploader, request.getSession(), wa, itemName, inputStream, destinationFolder, contentType, isOverwrite, size);
//
// if(workspaceUploader==null)
// throw new Exception("Error when creating uploader, it is null!");
//
// sendMessage(response, workspaceUploader.getIdentifier());
// }
logger.debug("Archive content type: "+uploadItem.getContentType());
if (MimeTypeUtil.isZipContentType(uploadItem.getContentType())){ //UNZIP??
logger.debug("Unziping content");
//THE WORKSPACE HAS BEEN INSTANCIED ABOVE
workspaceUploader = WorkspaceUploaderMng.uploadArchive(storageHubWrapper, user, scopeGroupId, workspaceUploader, request, itemName, inputStream, destinationFolder, size);
if(workspaceUploader==null)
throw new Exception("Error when creating uploader, it is null!");
sendMessage(response, workspaceUploader.getIdentifier());
} else{
//THE WORKSPACE HAS BEEN INSTANCIED ABOVE. DEFAULT CASE IS UPLOADING A FILE
workspaceUploader = WorkspaceUploaderMng.uploadFile(storageHubWrapper, user, scopeGroupId, request, workspaceUploader, request.getSession(), itemName, inputStream, destinationFolder, contentType, isOverwrite, size);
if(workspaceUploader==null)
throw new Exception("Error when creating uploader, it is null!");
sendMessage(response, workspaceUploader.getIdentifier());
}
}
//saveWorkspaceUploaderInSession(workspaceUploader, request.getSession());

View File

@ -56,7 +56,6 @@ public class WorkspaceUploaderMng {
* @param workspaceUploader the workspace uploader
* @param httpSession the http session
* @param isOvewrite the is ovewrite
* @param wa the wa
* @param uploadFile the upload file
* @param itemName the item name
* @param destinationFolder the destination folder
@ -87,9 +86,9 @@ public class WorkspaceUploaderMng {
if(createdItem!=null){
WorkspaceUploadServletStream.printElapsedTime(startTime);
logger.debug("HL file: "+createdItem.getName() + " with id: "+createdItem.getId() + " uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.getFile().setItemId(createdItem.getId()); //SET HL ID
workspaceUploader.getFile().setParentId(createdItem.getParentId());//SET PARENT ID
logger.debug("StorageHub file: "+createdItem.getName() + " with id: "+createdItem.getId() + " uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.getFile().setItemId(createdItem.getId()); //SET STORAGEHUB ID
workspaceUploader.getFile().setParentId(createdItem.getParentId());//SET STORAGEHUB PARENT ID
//ADDING VERSION NAME
if(createdItem instanceof FileItem){
@ -106,7 +105,7 @@ public class WorkspaceUploaderMng {
}
}else
logger.debug("HL file: "+createdItem.getName() + " has not version");
logger.debug("StorageHub file: "+createdItem.getName() + " has not version");
workspaceUploader.setStatusDescription("File \""+createdItem.getName()+"\" uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.setUploadStatus(UPLOAD_STATUS.COMPLETED);
@ -170,66 +169,115 @@ public class WorkspaceUploaderMng {
}
/**
* Creates the workspace uploader archive.
*
* @param storageWrapper the storage wrapper
* @param currUser the curr user
* @param scopeGroupId the scope group id
* @param workspaceUploader the workspace uploader
* @param request the request
* @param uploadArchive the upload archive
* @param itemName the item name
* @param destinationFolder the destination folder
* @param totalBytes the total bytes
* @return the workspace uploader item
* @throws InternalErrorException the internal error exception
* @throws IOException Signals that an I/O exception has occurred.
*/
private static WorkspaceUploaderItem createWorkspaceUploaderArchive(StorageHubWrapper storageWrapper, final GCubeUser currUser, final String scopeGroupId, final WorkspaceUploaderItem workspaceUploader, final HttpServletRequest request, final InputStream uploadArchive, final String itemName, final WorkspaceFolder destinationFolder, final long totalBytes) throws InternalErrorException, IOException{
HttpSession httpSession = request.getSession();
logger.info("calling upload archive - [itemName: "+itemName+"]");
// /**
// * Creates the workspace uploader archive.
// *
// * @param currUser the curr user
// * @param scopeGroupId the scope group id
// * @param workspaceUploader the workspace uploader
// * @param request the request
// * @param uploadFile the upload file
// * @param itemName the item name
// * @param destinationFolder the destination folder
// * @param totalBytes the total bytes
// * @return the workspace uploader item
// * @throws InternalErrorException the internal error exception
// * @throws IOException Signals that an I/O exception has occurred.
// */
// private static WorkspaceUploaderItem createWorkspaceUploaderArchive(final GCubeUser currUser, final String scopeGroupId, final WorkspaceUploaderItem workspaceUploader, final HttpServletRequest request, final InputStream uploadFile, final String itemName, final WorkspaceFolder destinationFolder, final long totalBytes) throws InternalErrorException, IOException{
// HttpSession httpSession = request.getSession();
// logger.info("calling upload archive - [itemName: "+itemName+"]");
// try {
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.IN_PROGRESS);
// UnzipUtil.unzip(destinationFolder, uploadFile, itemName);
//
// logger.info("Archive "+itemName+ " imported correctly in "+destinationFolder.getPath());
// workspaceUploader.setStatusDescription("Archive "+itemName+ " imported correctly in "+destinationFolder.getPath());
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.COMPLETED);
//
// try {
WorkspaceItem createdItem = null;
try {
workspaceUploader.setUploadStatus(UPLOAD_STATUS.IN_PROGRESS);
workspaceUploader.setStatusDescription("Uploading "+itemName);
createdItem = storageWrapper.getWorkspace().uploadArchive(destinationFolder.getId(), uploadArchive, itemName);
if(createdItem!=null){
logger.debug("StorageHub"+createdItem.getName() + " with id: "+createdItem.getId() + " uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.getFile().setItemId(createdItem.getId()); //SET STORAGEHUB ID
workspaceUploader.getFile().setParentId(createdItem.getParentId());//SET STORAGEHUB PARENT ID
//ADDING VERSION NAME
if(createdItem instanceof FileItem){
FileItem file = (FileItem) createdItem;
WorkspaceVersion currVersion = file.getCurrentVersion();
if(currVersion!=null){
workspaceUploader.getFile().setVersionName(currVersion.getName());
logger.debug("StorageHub file: "+createdItem.getName() + " has version: "+currVersion.getName());
}
else{
//TODO NO VERSION
//logger.debug("StorageHub file: "+createdItem.getName() + " has no current version");
//workspaceUploader.getFile().setVersionName(file.getName());
}
}else
logger.debug("StorageHub file: "+createdItem.getName() + " has not version");
workspaceUploader.setStatusDescription("Archive \""+createdItem.getName()+"\" uploaded correctly in "+destinationFolder.getPath());
workspaceUploader.setUploadStatus(UPLOAD_STATUS.COMPLETED);
final boolean isOvewrite = false;
WorkspaceUploadServletStream.notifyUploadInSharedFolder(storageWrapper, currUser, scopeGroupId, request, httpSession, createdItem.getId(), createdItem.getParentId(), isOvewrite);
}else{
workspaceUploader.setStatusDescription("An error occurred during upload: \""+itemName + "\". Try again");
workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED);
}
try {
WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
} catch (Exception e1) {
logger.error("Error during WorkspaceUploaderItem session update: ",e1);
}
} catch (InternalErrorException e) {
logger.error("Error during upload: ",e);
workspaceUploader.setStatusDescription("An error occurred server-side during upload: "+itemName+". "+e.getMessage() +". Try again");
workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED);
try {
WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
} catch (Exception e1) {
logger.error("Error during WorkspaceUploaderItem session update: ",e1);
}
} catch (InsufficientPrivilegesException | ItemAlreadyExistException | WorkspaceFolderNotFoundException | WrongDestinationException e) {
logger.error("Error during file uploading: ",e);
workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED);
if (e instanceof InsufficientPrivilegesException){
String folderName = destinationFolder.getName();
workspaceUploader.setStatusDescription("You have not permission to upload in the folder: "+folderName);
}else{
workspaceUploader.setStatusDescription("An error occurred during upload: "+itemName+". "+e.getMessage());
}
try {
WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
} catch (Exception e1) {
logger.error("Error during WorkspaceUploaderItem session update: ",e1);
}
//IS unreachable
}catch(UploadCanceledException e){
logger.info("UploadCanceledException thrown by client..");
workspaceUploader.setStatusDescription("Aborted upload: "+itemName);
workspaceUploader.setUploadStatus(UPLOAD_STATUS.ABORTED);
try {
// WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
// } catch (Exception e1) {
// logger.error("Error during WorkspaceUploaderItem session update: ",e1);
// }
// } catch (InternalErrorException e) {
// logger.error("Error during uploading: ",e);
// workspaceUploader.setStatusDescription("An error occurred during upload: "+itemName+". "+e.getMessage());
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.FAILED);
// try {
// WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
// } catch (Exception e1) {
// logger.error("Error during WorkspaceUploaderItem session update: ",e1);
// }
// }catch(UploadCanceledException e){
// logger.info("UploadCanceledException thrown by client..");
// workspaceUploader.setStatusDescription("Aborted upload: "+itemName);
// workspaceUploader.setUploadStatus(UPLOAD_STATUS.ABORTED);
// try {
// WsUtil.putWorkspaceUploaderInSession(httpSession, workspaceUploader);
// } catch (Exception e1) {
// logger.error("Error during WorkspaceUploaderItem session update: ",e1);
// }
// }finally{
// try {
//// StreamUtils.deleteTempFile(uploadFile);
// WsUtil.setErasableWorkspaceUploaderInSession(request, workspaceUploader.getIdentifier());
// } catch (Exception e2) {
// // TODO: handle exception
// }
// }
// return workspaceUploader;
// }
// workspaceUploader.setErasable(true);
WsUtil.forceEraseWorkspaceUploaderInSession(httpSession, workspaceUploader);
} catch (Exception e1) {
logger.error("Error during WorkspaceUploaderItem session update: ",e1);
}
}finally{
try {
// StreamUtils.deleteTempFile(uploadFile);
WsUtil.setErasableWorkspaceUploaderInSession(request, workspaceUploader.getIdentifier());
} catch (Exception e2) {
logger.error("Error during setErasableWorkspaceUploaderInSession session update: ",e2);
}
}
return workspaceUploader;
}
/**
@ -261,30 +309,31 @@ public class WorkspaceUploaderMng {
}
// /**
// * Upload archive.
// *
// * @param currUser the curr user
// * @param scopeGroupId the scope group id
// * @param workspaceUploader the workspace uploader
// * @param request the request
// * @param itemName the item name
// * @param file the file
// * @param destinationFolder the destination folder
// * @param totalBytes the total bytes
// * @return the workspace uploader item
// * @throws Exception the exception
// */
// public static WorkspaceUploaderItem uploadArchive(GCubeUser currUser, String scopeGroupId, WorkspaceUploaderItem workspaceUploader, HttpServletRequest request, String itemName, InputStream file, WorkspaceFolder destinationFolder, long totalBytes) throws Exception {
//
// try {
// return createWorkspaceUploaderArchive(currUser, scopeGroupId, workspaceUploader, request, file, itemName, destinationFolder, totalBytes);
//
// } catch (Exception e) {
// logger.error("Error when uploading Archive to HL creation: ",e);
// throw new Exception("An error occurred during upload:: "+itemName+". Try again");
// }
// }
/**
* Upload archive.
*
* @param storageWrapper the storage wrapper
* @param currUser the curr user
* @param scopeGroupId the scope group id
* @param workspaceUploader the workspace uploader
* @param request the request
* @param itemName the item name
* @param file the file
* @param destinationFolder the destination folder
* @param totalBytes the total bytes
* @return the workspace uploader item
* @throws Exception the exception
*/
public static WorkspaceUploaderItem uploadArchive(StorageHubWrapper storageWrapper, GCubeUser currUser, String scopeGroupId, WorkspaceUploaderItem workspaceUploader, HttpServletRequest request, String itemName, InputStream file, WorkspaceFolder destinationFolder, long totalBytes) throws Exception {
try {
return createWorkspaceUploaderArchive(storageWrapper, currUser, scopeGroupId, workspaceUploader, request, file, itemName, destinationFolder, totalBytes);
} catch (Exception e) {
logger.error("Error when uploading Archive to HL creation: ",e);
throw new Exception("An error occurred during upload:: "+itemName+". Try again");
}
}
/**
* Upload file status.