Merged HEAD (5.2) to 5.2.N (5.2.1)

126370 jkaabimofrad: Merged FILE-FOLDER-API (5.2.0) to HEAD (5.2)
      119801 jkaabimofrad: RA-637: Added autoRename form data for the multipart upload, so, on name clash, the API will try to auto­rename the file with an integer counter.


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@126715 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Ancuta Morarasu
2016-05-11 10:49:47 +00:00
parent f9a802acee
commit 935c4cc5b0
3 changed files with 108 additions and 32 deletions

View File

@@ -1158,6 +1158,7 @@ public class NodesImpl implements Nodes
String fileName = null;
Content content = null;
boolean autoRename = false;
boolean overwrite = false; // If a fileName clashes for a versionable file
for (FormData.FormField field : formData.getFields())
@@ -1176,9 +1177,13 @@ public class NodesImpl implements Nodes
}
break;
case "overwrite":
overwrite = Boolean.valueOf(field.getValue());
case "autorename":
autoRename = Boolean.valueOf(field.getValue());
break;
// case "overwrite":
// overwrite = Boolean.valueOf(field.getValue());
// break;
}
}
@@ -1204,20 +1209,25 @@ public class NodesImpl implements Nodes
if (existingFile != null)
{
// File already exists, decide what to do
if (overwrite && nodeService.hasAspect(existingFile, ContentModel.ASPECT_VERSIONABLE))
if (autoRename)
{
// Upload component was configured to overwrite files if name clashes
write(existingFile, content, fileName, false, true);
// Extract the metadata (The overwrite policy controls
// which if any parts of the document's properties are updated from this)
extractMetadata(existingFile);
// Do not clean formData temp files to
// allow for retries. Temp files will be deleted later
// when GC call DiskFileItem#finalize() method or by temp file cleaner.
return createUploadResponse(parentNodeRef, existingFile);
fileName = findUniqueName(parentNodeRef, fileName);
}
// TODO uncomment when we decide on uploading a new version vs overwriting
// else if (overwrite && nodeService.hasAspect(existingFile, ContentModel.ASPECT_VERSIONABLE))
// {
// // Upload component was configured to overwrite files if name clashes
// write(existingFile, content, fileName, false, true);
//
// // Extract the metadata (The overwrite policy controls
// // which if any parts of the document's properties are updated from this)
// extractMetadata(existingFile);
//
// // Do not clean formData temp files to
// // allow for retries. Temp files will be deleted later
// // when GC call DiskFileItem#finalize() method or by temp file cleaner.
// return createUploadResponse(parentNodeRef, existingFile);
// }
else
{
throw new ConstraintViolatedException(fileName + " already exists.");
@@ -1395,6 +1405,46 @@ public class NodesImpl implements Nodes
}
}
/**
* Creates a unique file name, if the upload component was configured to
* find a new unique name for clashing filenames.
*
* @param parentNodeRef the parent node
* @param fileName the original fileName
* @return a new file name
*/
private String findUniqueName(NodeRef parentNodeRef, String fileName)
{
int counter = 1;
String tmpFilename;
NodeRef existingFile;
do
{
int dotIndex = fileName.lastIndexOf('.');
if (dotIndex == 0)
{
// File didn't have a proper 'name' instead it
// had just a suffix and started with a ".", create "1.txt"
tmpFilename = counter + fileName;
}
else if (dotIndex > 0)
{
// Filename contained ".", create "fileName-1.txt"
tmpFilename = fileName.substring(0, dotIndex) + "-" + counter + fileName.substring(dotIndex);
}
else
{
// Filename didn't contain a dot at all, create "fileName-1"
tmpFilename = fileName + "-" + counter;
}
existingFile = nodeService.getChildByName(parentNodeRef, ContentModel.ASSOC_CONTAINS, tmpFilename);
counter++;
} while (existingFile != null);
return tmpFilename;
}
/**
* Helper to create a QName from either a fully qualified or short-name QName string
*