Skip to content

Commit

Permalink
SAK-30924 Drag and Drop upload has'Overwrite existing files' function…
Browse files Browse the repository at this point in the history
…ality. (sakaiproject#3975)

Added overwrite boolean for 'doDragDropUpload' method which checks if user
wants to overwrite files.
  • Loading branch information
buckett authored Mar 9, 2017
1 parent 3554237 commit 5f47491
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
6 changes: 5 additions & 1 deletion content/content-bundles/resources/types.properties
Original file line number Diff line number Diff line change
Expand Up @@ -594,4 +594,8 @@ zipdownlad.table=Table holds information about materials selected for downloadin
zipdownload.show1=Download
zipdownload.maxIndividualSize=File ''{0}'' exceeds the allowed size for a single file ({1} MB)
zipdownload.maxIndividualSizeInFolder=One file inside folder ''{0}'' exceeds the allowed size for a single file ({1} MB)
zipdownload.maxTotalSize=The files to download exceed the maximum allowed size ({0} MB)
zipdownload.maxTotalSize=The files to download exceed the maximum allowed size ({0} MB)

#SAK-30924
label.overwrite.warning=Warning: Existing files will be overwritten.
overwrite.text=Overwrite existing files
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ protected String buildUploadFilesContext(VelocityPortlet portlet, Context contex
String instr_uploads = rb.getFormattedMessage("instr.uploads", new String[]{ uploadMax });
context.put("instr_uploads", instr_uploads);

String uploadWarning = rb.getFormattedMessage("label.overwrite.warning");
context.put("label_overwrite_warning",uploadWarning);
String instr_dnd_uploads = rb.getFormattedMessage("instr.dnd.uploads", new String[]{ uploadMax });
context.put("instr_dnd_uploads", instr_dnd_uploads);

Expand Down Expand Up @@ -2025,6 +2027,8 @@ private synchronized void doDragDropUpload(HttpServletRequest request, HttpServl

String uploadFileName=null;
String collectionName=null;
String resourceId = null;
String overwrite = request.getParameter("overwrite");

String resourceGroup = toolSession.getAttribute("resources.request.create_wizard_collection_id").toString();

Expand Down Expand Up @@ -2074,8 +2078,25 @@ private synchronized void doDragDropUpload(HttpServletRequest request, HttpServl

if (collection!=null)
{
logger.debug("Adding resource "+uploadFileName+" in collection "+collection.getId());
resource = ContentHostingService.addResource(collection.getId(), Validator.escapeResourceName(basename),Validator.escapeResourceName(extension), ResourcesAction.MAXIMUM_ATTEMPTS_FOR_UNIQUENESS);
//get the resourceId by using collectionName and uploadFileName
resourceId = collectionName +"/"+ uploadFileName;
try{
//check if resource in collection exists
ContentHostingService.getResource(resourceId);
//if user has chosen to overwrite existing resource save the new copy
if(overwrite != null && overwrite.equals("true")){
resource = ContentHostingService.editResource(resourceId);
}
//if no overwrite then create a new resource in the collection
else{
resource = ContentHostingService.addResource(collection.getId(), Validator.escapeResourceName(basename),Validator.escapeResourceName(extension), ResourcesAction.MAXIMUM_ATTEMPTS_FOR_UNIQUENESS);
}
}
//if this is a new resource add to the collection.
catch(IdUnusedException idUnusedException) {
logger.debug("Adding resource "+uploadFileName+" in collection "+collection.getId());
resource = ContentHostingService.addResource(collection.getId(), Validator.escapeResourceName(basename),Validator.escapeResourceName(extension), ResourcesAction.MAXIMUM_ATTEMPTS_FOR_UNIQUENESS);
}
}
else
{
Expand All @@ -2084,9 +2105,24 @@ private synchronized void doDragDropUpload(HttpServletRequest request, HttpServl
//So I disable this method call, though it can be enabled again if desired.

//String resourceName = getUniqueFileName(uploadFileName, resourceGroup);

logger.debug("Adding resource "+uploadFileName+" in current folder ("+resourceGroup+")");
resource = ContentHostingService.addResource(resourceGroup, Validator.escapeResourceName(basename), Validator.escapeResourceName(extension), ResourcesAction.MAXIMUM_ATTEMPTS_FOR_UNIQUENESS);
resourceId = resourceGroup + uploadFileName;
try{
//check if resource exists
ContentHostingService.getResource(resourceId);
//if it does and overwrite is true save the latest copy
if(overwrite != null && overwrite.equals("true")){
resource = ContentHostingService.editResource(resourceId);
}
// if no overwrite then simply create a new resource
else{
resource = ContentHostingService.addResource(resourceGroup, Validator.escapeResourceName(basename), Validator.escapeResourceName(extension), ResourcesAction.MAXIMUM_ATTEMPTS_FOR_UNIQUENESS);
}
}
// if new resource then save
catch(IdUnusedException idUnusedException) {
logger.debug("Adding resource "+uploadFileName+" in current folder ("+resourceGroup+")");
resource = ContentHostingService.addResource(resourceGroup, Validator.escapeResourceName(basename), Validator.escapeResourceName(extension), ResourcesAction.MAXIMUM_ATTEMPTS_FOR_UNIQUENESS);
}
}

if (resource != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
<input type="hidden" name="sakai_csrf_token" value="$sakai_csrf_token" />
<div class="dz-default dz-message">$tlang.getString("label.dragZoneMessage")</div>
</form>
<div>
<input type="checkbox" name="overwrite" id="overwrite" checked="checked" value="true">$tlang.getString("overwrite.text")
</div>
<div id="fileUploaderDesc" class="instruction">
<p>$tlang.getString("label.dragDescription")</p>
<p>$instr_dnd_uploads</p>
Expand Down Expand Up @@ -178,6 +181,8 @@ Dropzone.options.fileUploader = {
});

this.on("sending", function(file, xhr, formData) {
var overwrite = document.querySelector("#overwrite").checked;
formData.append("overwrite",overwrite );
formData.append("fullPath",file.fullPath);
#if($model.isDropbox() || $model.isUserSite() || !$availability_is_enabled)
formData.append("hidden", false);
Expand Down

0 comments on commit 5f47491

Please sign in to comment.