Skip to content

Commit

Permalink
fix upload on existing exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-christl committed Sep 29, 2023
1 parent 052d639 commit c211afd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ private void setUpDragAndDropQuestionForImport(DragAndDropQuestion dndQuestion,
}
else {
// A new file got uploaded, everything got already verified at this point
quizExerciseService.saveDndDragItemPicture(dragItem, fileMap, null);
quizExerciseService.saveDndDragItemPicture(dragItem, fileMap);
}
}
for (DragAndDropMapping dragAndDropMapping : dndQuestion.getCorrectMappings()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public void handleDndQuizFileCreation(QuizExercise quizExercise, List<MultipartF
private void handleDndQuizDragItemsCreation(DragAndDropQuestion dragAndDropQuestion, Map<String, MultipartFile> fileMap) throws IOException {
for (var dragItem : dragAndDropQuestion.getDragItems()) {
if (dragItem.getPictureFilePath() != null) {
saveDndDragItemPicture(dragItem, fileMap, null);
saveDndDragItemPicture(dragItem, fileMap);
}
}
}
Expand Down Expand Up @@ -306,7 +306,7 @@ private void handleDndQuestionUpdate(DragAndDropQuestion dragAndDropQuestion, Se
String newDragItemPath = dragItem.getPictureFilePath();
if (dragItem.getPictureFilePath() != null && !oldPaths.contains(newDragItemPath)) {
// Path changed and file was provided
saveDndDragItemPicture(dragItem, fileMap, questionUpdate.getId());
saveDndDragItemPicture(dragItem, fileMap);
}
}
}
Expand Down Expand Up @@ -360,30 +360,29 @@ public void saveDndQuestionBackground(DragAndDropQuestion question, Map<String,
/**
* Saves the picture of a drag item without saving the drag item itself
*
* @param dragItem the drag item
* @param files all provided files
* @param questionId the id of the question, null on creation
* @param dragItem the drag item
* @param files all provided files
*/
public void saveDndDragItemPicture(DragItem dragItem, Map<String, MultipartFile> files, @Nullable Long questionId) throws IOException {
public void saveDndDragItemPicture(DragItem dragItem, Map<String, MultipartFile> files) throws IOException {
MultipartFile file = files.get(dragItem.getPictureFilePath());
if (file == null) {
// Should not be reached as the file is validated before
throw new BadRequestAlertException("The file " + dragItem.getPictureFilePath() + " was not provided", ENTITY_NAME, null);
}

dragItem.setPictureFilePath(saveDragAndDropImage(FilePathService.getDragItemFilePath(), file, questionId).toString());
dragItem.setPictureFilePath(saveDragAndDropImage(FilePathService.getDragItemFilePath(), file, null).toString());
}

/**
* Saves an image for an DragAndDropQuestion. Either a background image or a drag item image.
*
* @return the public path of the saved image
*/
private URI saveDragAndDropImage(Path basePath, MultipartFile file, @Nullable Long questionId) throws IOException {
String clearFileName = FileService.sanitizeFilename(FilenameUtils.getExtension(Objects.requireNonNull(file.getOriginalFilename())));
Path savePath = fileService.generateFilePath("dnd_image_", clearFileName, basePath);
private URI saveDragAndDropImage(Path basePath, MultipartFile file, @Nullable Long entityId) throws IOException {
String clearFileExtension = FileService.sanitizeFilename(FilenameUtils.getExtension(Objects.requireNonNull(file.getOriginalFilename())));
Path savePath = fileService.generateFilePath("dnd_image_", clearFileExtension, basePath);
FileUtils.copyToFile(file.getInputStream(), savePath.toFile());
return filePathService.publicPathForActualPathOrThrow(savePath, questionId);
return filePathService.publicPathForActualPathOrThrow(savePath, entityId);
}

/**
Expand Down

0 comments on commit c211afd

Please sign in to comment.