Skip to content

Commit df9350d

Browse files
committed
Change submission upload replaceFile parameter to UUID
This synchronizes the /workspaceitems with the /edititems, where we don't have a clear file order
1 parent 7f2764e commit df9350d

2 files changed

Lines changed: 13 additions & 33 deletions

File tree

dspace-server-webapp/src/main/java/org/dspace/app/rest/submit/SubmissionService.java

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ public DataDuplicateDetection getDataDuplicateDetection(Context context, InProgr
376376
* Utility method used by the {@link WorkspaceItemRestRepository} and
377377
* {@link WorkflowItemRestRepository} to deal with the upload in an inprogress
378378
* submission
379-
*
379+
*
380380
* @param context DSpace Context Object
381381
* @param request the http request containing the upload request
382382
* @param wsi the inprogress submission current rest representation
@@ -424,7 +424,7 @@ public List<ErrorRest> uploadFileToInprogressSubmission(Context context, HttpSer
424424
UploadableStep uploadableStep = (UploadableStep) stepInstanceAndCfg[0];
425425
Bitstream originalFile = null;
426426
if (uploadableStep instanceof UploadStep) {
427-
originalFile = findOriginalFile(request, (UploadStep) uploadableStep, source);
427+
originalFile = findOriginalFile(request);
428428
}
429429
Bitstream newFile;
430430
ErrorRest err;
@@ -459,7 +459,7 @@ public List<ErrorRest> uploadFileToInprogressSubmission(Context context, HttpSer
459459
* Utility method used by the {@link WorkspaceItemRestRepository} and
460460
* {@link WorkflowItemRestRepository} to deal with the patch of an inprogress
461461
* submission
462-
*
462+
*
463463
* @param context DSpace Context Object
464464
* @param request the http request
465465
* @param source the current inprogress submission
@@ -538,27 +538,15 @@ public void evaluatePatchToInprogressSubmission(Context context, HttpServletRequ
538538
}
539539

540540
/**
541-
* If {@code request} has parameter 'replaceFile', returns the file at index 'replaceFile'
542-
* from the list of uploaded files in {@code submission}.
541+
* If {@code request} has parameter 'replaceFile', interpret it as a Bitstream UUID
543542
* Otherwise, returns null.
544543
*/
545-
private Bitstream findOriginalFile(HttpServletRequest request, UploadStep uploadStep,
546-
InProgressSubmission submission) throws SQLException {
544+
private Bitstream findOriginalFile(HttpServletRequest request) throws SQLException {
547545
String replaceFile = request.getParameter("replaceFile");
548546
if (replaceFile == null) {
549547
return null;
550548
}
551-
int originalFileIndex;
552-
try {
553-
originalFileIndex = Integer.parseInt(replaceFile);
554-
} catch (NumberFormatException e) {
555-
throw new DSpaceBadRequestException(e.getMessage(), e);
556-
}
557-
List<UploadBitstreamRest> files = uploadStep.getData(this, submission, null).getFiles();
558-
if (originalFileIndex < 0 || originalFileIndex >= files.size()) {
559-
throw new DSpaceBadRequestException("Provided file index is out of bounds");
560-
}
561-
UUID uuid = files.get(originalFileIndex).getUuid();
549+
UUID uuid = UUID.fromString(replaceFile);
562550
return bitstreamService.find(new Context(), uuid);
563551
}
564552

dspace-server-webapp/src/test/java/org/dspace/app/rest/WorkspaceItemRestRepositoryIT.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10820,11 +10820,10 @@ public void uploadAndReplaceTest() throws Exception {
1082010820
context.restoreAuthSystemState();
1082110821

1082210822
String token = getAuthToken(admin.getEmail(), password);
10823-
int oldFileIndex = 0;
1082410823
// Upload new file, to replace old one
1082510824
getClient(token).perform(multipart("/api/submission/workspaceitems/" + workspaceItem.getID())
1082610825
.file(newFile)
10827-
.param("replaceFile", Integer.toString(oldFileIndex))
10826+
.param("replaceFile", originalBitstream.getID().toString())
1082810827
.param("replaceName", "true"))
1082910828
.andExpect(status().isCreated())
1083010829
.andExpect(jsonPath("$.sections.upload.files[0].metadata['dc.title'][0].value",
@@ -10890,16 +10889,10 @@ public void uploadAndReplaceBadParamTest() throws Exception {
1089010889
.file(newFile)
1089110890
.param("replaceFile", bitstream.getName()))
1089210891
.andExpect(status().isBadRequest());
10893-
// UUID is not supported
10894-
getClient(token).perform(multipart("/api/submission/workspaceitems/" + workspaceItem.getID())
10895-
.file(newFile)
10896-
.param("replaceFile", bitstream.getID().toString()))
10897-
.andExpect(status().isBadRequest());
10898-
// Parameter cannot be out of bounds
10899-
int fileIndexOutOfBounds = 1;
10892+
// Integer is not supported
1090010893
getClient(token).perform(multipart("/api/submission/workspaceitems/" + workspaceItem.getID())
1090110894
.file(newFile)
10902-
.param("replaceFile", Integer.toString(fileIndexOutOfBounds)))
10895+
.param("replaceFile", "0"))
1090310896
.andExpect(status().isBadRequest());
1090410897
}
1090510898

@@ -10955,8 +10948,9 @@ public void uploadAndReplaceForbiddenTest() throws Exception {
1095510948
.withIssueDate("2017-10-17")
1095610949
.build();
1095710950
// Create file that will be replaced
10951+
Bitstream originalFile;
1095810952
try (InputStream is = IOUtils.toInputStream("Test", CharEncoding.UTF_8)) {
10959-
BitstreamBuilder.createBitstream(context, workspaceItem.getItem(), is)
10953+
originalFile = BitstreamBuilder.createBitstream(context, workspaceItem.getItem(), is)
1096010954
.withName("Bitstream")
1096110955
.withDescription("description")
1096210956
.withMimeType("text/plain")
@@ -10969,11 +10963,10 @@ public void uploadAndReplaceForbiddenTest() throws Exception {
1096910963
context.restoreAuthSystemState();
1097010964

1097110965
String token = getAuthToken(eperson.getEmail(), password);
10972-
int oldFileIndex = 0;
1097310966
// Upload new file, to replace old one
1097410967
getClient(token).perform(multipart("/api/submission/workspaceitems/" + workspaceItem.getID())
1097510968
.file(newFile)
10976-
.param("replaceFile", Integer.toString(oldFileIndex)))
10969+
.param("replaceFile", originalFile.getID().toString()))
1097710970
.andExpect(status().isForbidden());
1097810971
}
1097910972

@@ -11009,11 +11002,10 @@ public void uploadAndReplaceNoWriteRightsTest() throws Exception {
1100911002
context.restoreAuthSystemState();
1101011003

1101111004
String token = getAuthToken(eperson.getEmail(), password);
11012-
int oldFileIndex = 0;
1101311005
// Upload new file, to replace old one
1101411006
getClient(token).perform(multipart("/api/submission/workspaceitems/" + workspaceItem.getID())
1101511007
.file(newFile)
11016-
.param("replaceFile", Integer.toString(oldFileIndex)))
11008+
.param("replaceFile", originalFile.getID().toString()))
1101711009
.andExpect(status().isForbidden());
1101811010
}
1101911011

0 commit comments

Comments
 (0)