This commit is contained in:
Francesco Mangiacrapa 2024-08-01 12:46:24 +02:00
parent 284f2e5dca
commit 106dfc3a2b
8 changed files with 204 additions and 94 deletions

View File

@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
## [v2.1.1-SNAPSHOT] - 2024-07-29
- [#27898] Updated `uploadFile` and `uploadArchive` methods. They include the `fileSize` parameter
- [#27934] Updated the data structure to handle asynchronous uploading of multiple files
## [v2.1.0] - 2022-05-03

View File

@ -32,6 +32,8 @@ public class ConstantsWorkspaceUploader {
public static final String UPLOAD_FORM_ELEMENT = "uploadFormElement";
public static final String CLIENT_UPLOAD_KEYS = "client_upload_keys";
public static final String CLIENT_UPLOAD_FILEOBJ_FILENAME = "filename";
public static final String CLIENT_UPLOAD_FILEOBJ_FILESIZE= "filesize";
public static final String CANCEL_UPLOAD = "cancel_upload";
public static final String JSON_CLIENT_KEYS = "ClientKeys";

View File

@ -15,11 +15,15 @@ import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem
import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem.UPLOAD_STATUS;
import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteHandler;
@ -88,37 +92,45 @@ public class MultipleDilaogUpload extends DialogUpload {
* @param parentId the parent id
* @return the list
*/
public void generateFakeUploaders(String filesSelected, String parentId) {
public void generateFakeUploaders(JSONArray filesSelected, String parentId) {
if (filesSelected == null || filesSelected.isEmpty())
if (filesSelected == null || filesSelected.size() == 0)
return;
String[] files = filesSelected.split(FILE_DELEMITER);
// String[] files = filesSelected.split(FILE_DELEMITER);
// NORMALIZE FILE NAMES
for (int i = 0; i < files.length; i++) {
String normalizedFileName = files[i];
for (int i = 0; i < filesSelected.size(); i++) {
JSONObject fileObject = (JSONObject) filesSelected.get(i);
GWT.log("checking filename for: " + fileObject.getJavaScriptObject().toString());
String normalizedFileName = fileObject.get("filename").isString().stringValue();
if (normalizedFileName.contains("\\")) {
files[i] = normalizedFileName.substring(normalizedFileName.lastIndexOf("\\") + 1); // remove C:\fakepath
// if exists
String sanitizedValue = normalizedFileName.substring(normalizedFileName.lastIndexOf("\\") + 1); // remove
// C:\fakepath
// if exists
JSONValue jsonValue = JSONParser.parseStrict(sanitizedValue);
fileObject.put("filename", jsonValue);
filesSelected.set(i, fileObject);
}
}
GWT.log("generating fake uploaders on: " + Arrays.asList(files.toString()));
fakeUploaders = new ArrayList<WorkspaceUploaderItem>(files.length);
for (int i = 0; i < files.length; i++) {
fakeUploaders = new ArrayList<WorkspaceUploaderItem>(filesSelected.size());
for (int i = 0; i < filesSelected.size(); i++) {
JSONObject fileObject = (JSONObject) filesSelected.get(i);
WorkspaceUploaderItem fakeItem = new WorkspaceUploaderItem();
fakeItem.setClientUploadKey(GenerateUUID.get());
fakeItem.setUploadStatus(UPLOAD_STATUS.WAIT);
WorkspaceUploadFile fakeFile = new WorkspaceUploadFile();
fakeFile.setFileName(files[i]);
String fileName = fileObject.get("filename").isString().stringValue();
Long fileSize = (long) fileObject.get("size").isNumber().doubleValue();
fakeFile.setFileName(fileName);
fakeFile.setFileSize(fileSize);
fakeFile.setParentId(parentId);
fakeItem.setFile(fakeFile);
fakeUploaders.add(fakeItem);
}
GWT.log("fakeUploaders generated: " + fakeUploaders.toString());
// return fakeUploaders;
}
/*
@ -198,17 +210,20 @@ public class MultipleDilaogUpload extends DialogUpload {
return;
}
String[] files = null;
GWT.log("Current Uploader has id: " + fileUploadID);
String filesSelected = getFilesSelected(fileUploadID, FILE_DELEMITER);
GWT.log("getFilesSelected: " + filesSelected);
files = filesSelected.split(FILE_DELEMITER);
JavaScriptObject filesSelected = getFilesSelected(fileUploadID);
if (isLimitExceeded(files.length))
JSONArray jsonArray = new JSONArray(filesSelected);
if(jsonArray!=null)
GWT.log("sono array");
GWT.log("getFilesSelected: " + jsonArray);
if (isLimitExceeded(jsonArray.size()))
return;
// GENERATE NEW UPLOADERS
generateFakeUploaders(filesSelected, parentIdentifier);
generateFakeUploaders(jsonArray, parentIdentifier);
GWT.log(fakeUploaders.toString());
createJsonKeyForFiles();
GWT.log(jsonKeys);
@ -279,7 +294,11 @@ public class MultipleDilaogUpload extends DialogUpload {
for (int i = 0; i < fakeUploaders.size(); i++) {
WorkspaceUploaderItem file = fakeUploaders.get(i);
JSONObject obj = new JSONObject();
obj.put(file.getClientUploadKey(), new JSONString(file.getFile().getFileName()));
JSONObject fileObject = new JSONObject();
//Feature #27934
fileObject.put(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILENAME, new JSONString(file.getFile().getFileName()));
fileObject.put(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILESIZE, new JSONNumber(file.getFile().getFileSize()));
obj.put(file.getClientUploadKey(), fileObject);
jsonArray.set(i, obj);
}
@ -352,16 +371,20 @@ public class MultipleDilaogUpload extends DialogUpload {
* @param fileDelimiter the file delimiter
* @return the files selected
*/
public static native String getFilesSelected(final String tagId, final String fileDelimiter) /*-{
public static native JavaScriptObject getFilesSelected(final String tagId) /*-{
var count = $wnd.$("#" + tagId)[0].files.length;
console.log(count);
var out = "";
console.log("number of files: "+count);
var outputs = [];
for (i = 0; i < count; i++) {
var file = $wnd.$("#" + tagId)[0].files[i];
// out += file.name + fileDelimiter + file.size + fileDelimiter;
out += file.name + fileDelimiter;
//out += file.name + fileDelimiter + file.size + fileDelimiter;
var the_file = { "filename": file.name, "size": file.size };
//var jsonString = JSON.stringify(the_file);
//console.log("the file is: "+jsonString);
outputs.push(the_file);
}
return out;
console.log("returning outputs: "+outputs);
return outputs;
}-*/;
}

View File

@ -14,24 +14,25 @@ import org.gcube.portlets.widgets.workspaceuploader.shared.WorkspaceUploaderItem
import com.google.gwt.core.client.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONNumber;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;
import com.google.gwt.user.client.Window;
/**
* The Class WorkspaceFieldsUpload.
*
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it
* Oct 20, 2015
* @author Francesco Mangiacrapa francesco.mangiacrapa@isti.cnr.it Oct 20, 2015
*/
public class WorkspaceFieldsUploadManager {
//FORM FIELD TO UPLOAD INTO WORKSPACE
// FORM FIELD TO UPLOAD INTO WORKSPACE
private String jsonKeys;
public static final String FILE_DELEMITER = ";";
private List<WorkspaceUploaderItem> fakeUploaders = new ArrayList<WorkspaceUploaderItem>();
public WorkspaceFieldsUploadManager() {
}
@ -41,12 +42,10 @@ public class WorkspaceFieldsUploadManager {
* @param numbOfFiles the numb of files
* @return true, if is limit exceeded
*/
public boolean isLimitExceeded(int numbOfFiles){
public boolean isLimitExceeded(int numbOfFiles) {
if (numbOfFiles > ConstantsWorkspaceUploader.LIMIT_UPLOADS) {
Window.alert("Multiple upload limit is "
+ ConstantsWorkspaceUploader.LIMIT_UPLOADS
+ " files");
Window.alert("Multiple upload limit is " + ConstantsWorkspaceUploader.LIMIT_UPLOADS + " files");
return true;
}
@ -56,18 +55,18 @@ public class WorkspaceFieldsUploadManager {
/**
* Adds the new submit to monitor.
*/
public void addNewSubmitToMonitor(){
public void addNewSubmitToMonitor() {
GWT.log("addNewSubmitToMonitor...");
int queueIndex = UploaderMonitor.getInstance().newQueue();
for (final WorkspaceUploaderItem workspaceUploaderItem : fakeUploaders) {
UploaderMonitor.getInstance().addNewUploaderToMonitorPanel(workspaceUploaderItem, workspaceUploaderItem.getFile().getFileName());
UploaderMonitor.getInstance().addNewUploaderToMonitorPanel(workspaceUploaderItem,
workspaceUploaderItem.getFile().getFileName());
UploaderMonitor.getInstance().addNewUploaderToQueue(queueIndex, workspaceUploaderItem);
// UploaderMonitor.getInstance().pollWorkspaceUploader(workspaceUploaderItem);
}
UploaderMonitor.getInstance().doStartPollingQueue(queueIndex);
}
/**
* Creates the json key for files.
*
@ -84,12 +83,20 @@ public class WorkspaceFieldsUploadManager {
for (int i = 0; i < fakeUploaders.size(); i++) {
WorkspaceUploaderItem file = fakeUploaders.get(i);
JSONObject obj = new JSONObject();
obj.put(file.getClientUploadKey(), new JSONString(file.getFile().getFileName()));
JSONObject fileObject = new JSONObject();
// Feature #27934
fileObject.put(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILENAME,
new JSONString(file.getFile().getFileName()));
fileObject.put(ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILESIZE,
new JSONNumber(file.getFile().getFileSize()));
obj.put(file.getClientUploadKey(), fileObject);
// obj.put(file.getClientUploadKey(), new
// JSONString(file.getFile().getFileName()));
jsonArray.set(i, obj);
}
jsonKeys = productObj.toString();
GWT.log("updated jsonKeys: "+jsonKeys);
GWT.log("updated jsonKeys: " + jsonKeys);
} catch (Exception e) {
GWT.log("error " + e.getMessage());
jsonKeys = null;
@ -100,44 +107,48 @@ public class WorkspaceFieldsUploadManager {
* Generate new upload client keys.
*
* @param filesSelected the files selected
* @param parentId the parent id
* @param parentId the parent id
* @return the list
*/
public void generateFakeUploaders(String filesSelected, String parentId) {
public void generateFakeUploaders(JSONArray filesSelected, String parentId) {
if(filesSelected==null || filesSelected.isEmpty())
if (filesSelected == null || filesSelected.size() == 0)
return;
String[] files = filesSelected.split(FILE_DELEMITER);
// NORMALIZE FILE NAMES
for (int i = 0; i < files.length; i++) {
String normalizedFileName = files[i];
for (int i = 0; i < filesSelected.size(); i++) {
JSONObject fileObject = (JSONObject) filesSelected.get(i);
GWT.log("checking filename for: " + fileObject.getJavaScriptObject().toString());
String normalizedFileName = fileObject.get("filename").isString().stringValue();
if (normalizedFileName.contains("\\")) {
files[i] = normalizedFileName.substring(normalizedFileName.lastIndexOf("\\") + 1); // remove
// C:\fakepath\
// if
// exists
String sanitizedValue = normalizedFileName.substring(normalizedFileName.lastIndexOf("\\") + 1); // remove
// C:\fakepath
// if exists
JSONValue jsonValue = JSONParser.parseStrict(sanitizedValue);
fileObject.put("filename", jsonValue);
filesSelected.set(i, fileObject);
}
}
GWT.log("generating fake uploaders on: "+Arrays.asList(files).toString());
fakeUploaders = new ArrayList<WorkspaceUploaderItem>(files.length);
for (int i = 0; i < files.length; i++) {
fakeUploaders = new ArrayList<WorkspaceUploaderItem>(filesSelected.size());
for (int i = 0; i < filesSelected.size(); i++) {
JSONObject fileObject = (JSONObject) filesSelected.get(i);
WorkspaceUploaderItem fakeItem = new WorkspaceUploaderItem();
fakeItem.setClientUploadKey(GenerateUUID.get());
fakeItem.setUploadStatus(UPLOAD_STATUS.WAIT);
WorkspaceUploadFile fakeFile = new WorkspaceUploadFile();
fakeFile.setFileName(files[i]);
String fileName = fileObject.get("filename").isString().stringValue();
Long fileSize = (long) fileObject.get("size").isNumber().doubleValue();
fakeFile.setFileName(fileName);
fakeFile.setFileSize(fileSize);
fakeFile.setParentId(parentId);
fakeItem.setFile(fakeFile);
fakeUploaders.add(fakeItem);
}
GWT.log("fakeUploaders generated: "+fakeUploaders.toString());
GWT.log("fakeUploaders generated: " + fakeUploaders.toString());
}
/**
* @return the jsonKeys
*/

View File

@ -14,7 +14,9 @@ import org.gcube.portlets.widgets.workspaceuploader.client.WorkspaceUploaderList
import org.gcube.portlets.widgets.workspaceuploader.client.uploader.DialogUpload.UPLOAD_TYPE;
import org.gcube.portlets.widgets.workspaceuploader.client.uploader.WorkspaceFieldsUploadManager;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
@ -252,9 +254,10 @@ public class MultipleDNDUpload extends LayoutPanel implements HasWorskpaceUpload
* @param uploadUUID
* the upload uuid
*/
private void generateFakeUploaders(String filesSelected, String parentId, String uploadUUID) {
private void generateFakeUploaders(JavaScriptObject filesSelected, String parentId, String uploadUUID) {
WorkspaceFieldsUploadManager field = getFieldsUploadManager(uploadUUID);
field.generateFakeUploaders(filesSelected, parentId);
JSONArray jsonArray = new JSONArray(filesSelected);
field.generateFakeUploaders(jsonArray, parentId);
}
@ -607,14 +610,15 @@ public class MultipleDNDUpload extends LayoutPanel implements HasWorskpaceUpload
console.log("# of file/s: " + files.length);
var filesSelected = "";
var arrayFileSelected = [];
for (i = 0; i < files.length; i++) {
filesSelected += files[i].name + fileDelimiter;
var file = files[i];
var the_file = { "filename": file.name, "size": file.size };
arrayFileSelected.push(the_file);
}
console.log("filesSelected: " + filesSelected);
console.log("Object arrayFileSelected: " + arrayFileSelected);
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::generateFakeUploaders(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)(filesSelected,idfolder,uploadUUID);
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::generateFakeUploaders(Lcom/google/gwt/core/client/JavaScriptObject;Ljava/lang/String;Ljava/lang/String;)(arrayFileSelected,idfolder,uploadUUID);
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::createJsonKeyForFiles(Ljava/lang/String;)(uploadUUID);
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::updateJsonKeys(Ljava/lang/String;)(uploadUUID);
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::setCurrentJsonKeValue(Ljava/lang/String;)(uploadUUID);
@ -657,10 +661,9 @@ public class MultipleDNDUpload extends LayoutPanel implements HasWorskpaceUpload
var numFolder = 0;
for (var i = 0; i < files.length; i++) {
var file = files[i];
var fileSelected = files[i].name + fileDelimiter;
if (!isFolder(file)) {
console.log("fileSelected: " + fileSelected);
console.log("files: " + files);
console.log("fileSelected: " + file.name);
//console.log("files: " + files);
formdata.append("isOverwrite", true);
formdata.append('uploadFormElement', file);
}else{
@ -668,24 +671,27 @@ public class MultipleDNDUpload extends LayoutPanel implements HasWorskpaceUpload
}
}
xhr.send(formdata);
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::addNewSubmitToMonitor(Ljava/lang/String;)(uploadUUID);
if(numFolder>0){
var msg;
if(numFolder==files.length){
msg = "Is not possible to upload a folder";
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::showAlert(Ljava/lang/String;)(msg);
return;
}
// xhr.send(formdata);
// instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::addNewSubmitToMonitor(Ljava/lang/String;)(uploadUUID);
//
// if(numFolder>0){
// var msg;
// if(numFolder==files.length){
// msg = "Is not possible to upload a folder";
// instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::showAlert(Ljava/lang/String;)(msg);
// return;
// }
//
// var msg = "Ignored ";
// msg += numFolder > 1? numFolder+" folders": numFolder+" folder";
// msg+= " during upload";
// console.log(msg);
// instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::showAlert(Ljava/lang/String;)(msg);
// }
// instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::reset()();
var msg = "Ignored ";
msg += numFolder > 1? numFolder+" folders": numFolder+" folder";
msg+= " during upload";
console.log(msg);
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::showAlert(Ljava/lang/String;)(msg);
}
instance.@org.gcube.portlets.widgets.workspaceuploader.client.uploader.dragdrop.MultipleDNDUpload::reset()();
});
// Tells the browser that we *can* drop on this target
// addEventHandler(drop, 'dragover', cancel);

View File

@ -9,7 +9,6 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -25,7 +24,6 @@ import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileItemHeaders;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.FileUploadBase;
@ -540,7 +538,7 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
"HttpServletRequest " + FileUploadBase.CONTENT_LENGTH + "/size is: " + requestContentLenght);
Long fileSize = null;
FileItemHeaders itemHeaders = uploadItem.getHeaders();
/*FileItemHeaders itemHeaders = uploadItem.getHeaders();
if (itemHeaders != null) {
Iterator<String> headerNames = uploadItem.getHeaders().getHeaderNames();
while(headerNames.hasNext()) {
@ -556,9 +554,9 @@ public class WorkspaceUploadServletStream extends HttpServlet implements Servlet
logger.warn("Error on getting fileSize", e);
}
}
}
}*/
logger.info("File " + FileUploadBase.CONTENT_LENGTH + "/size is: " + fileSize);
logger.info("File size is: " + workspaceUploader.getFile().getFileSize());
if(fileSize==null)
fileSize = requestContentLenght;

View File

@ -0,0 +1,58 @@
package org.gcube.portlets.widgets.workspaceuploader.shared;
import java.io.Serializable;
import org.gcube.portlets.widgets.workspaceuploader.client.ConstantsWorkspaceUploader;
import com.fasterxml.jackson.annotation.JsonProperty;
public class UpladProperties implements Serializable {
/**
*
*/
private static final long serialVersionUID = -7226677146789586756L;
@JsonProperty(value = ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILENAME)
String filename;
@JsonProperty(value = ConstantsWorkspaceUploader.CLIENT_UPLOAD_FILEOBJ_FILESIZE)
Long filesize;
public UpladProperties() {
}
public UpladProperties(String filename, Long filesize) {
super();
this.filename = filename;
this.filesize = filesize;
}
public String getFilename() {
return filename;
}
public Long getFilesize() {
return filesize;
}
public void setFilename(String filename) {
this.filename = filename;
}
public void setFilesize(Long filesize) {
this.filesize = filesize;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("UpladProperties [filename=");
builder.append(filename);
builder.append(", filesize=");
builder.append(filesize);
builder.append("]");
return builder.toString();
}
}

View File

@ -22,6 +22,7 @@ public class WorkspaceUploadFile implements Serializable{
private String fileName;
private String itemId;
private String versionName;
private Long fileSize;
/**
* Instantiates a new workspace upload file.
@ -129,6 +130,20 @@ public class WorkspaceUploadFile implements Serializable{
this.fileName = fileName;
}
public Long getFileSize() {
return fileSize;
}
public void setFileSize(Long fileSize) {
this.fileSize = fileSize;
}
/*
* (non-Javadoc)
*
@ -145,12 +160,8 @@ public class WorkspaceUploadFile implements Serializable{
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("WorkspaceUploadFile [parentId=");
builder.append(parentId);
@ -160,10 +171,10 @@ public class WorkspaceUploadFile implements Serializable{
builder.append(itemId);
builder.append(", versionName=");
builder.append(versionName);
builder.append(", fileSize=");
builder.append(fileSize);
builder.append("]");
return builder.toString();
}
}