From c211afd067288448466464218b934c653230de56 Mon Sep 17 00:00:00 2001 From: Julian Christl Date: Fri, 29 Sep 2023 22:41:27 +0200 Subject: [PATCH] fix upload on existing exercise --- .../service/QuizExerciseImportService.java | 2 +- .../artemis/service/QuizExerciseService.java | 21 +++++++++---------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseImportService.java b/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseImportService.java index 55f12a4e8ae2..4ed2b030a4e4 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseImportService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseImportService.java @@ -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()) { diff --git a/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseService.java b/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseService.java index b1c5c634074f..77270291dec6 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/QuizExerciseService.java @@ -241,7 +241,7 @@ public void handleDndQuizFileCreation(QuizExercise quizExercise, List fileMap) throws IOException { for (var dragItem : dragAndDropQuestion.getDragItems()) { if (dragItem.getPictureFilePath() != null) { - saveDndDragItemPicture(dragItem, fileMap, null); + saveDndDragItemPicture(dragItem, fileMap); } } } @@ -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); } } } @@ -360,18 +360,17 @@ public void saveDndQuestionBackground(DragAndDropQuestion question, Map files, @Nullable Long questionId) throws IOException { + public void saveDndDragItemPicture(DragItem dragItem, Map 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()); } /** @@ -379,11 +378,11 @@ public void saveDndDragItemPicture(DragItem dragItem, Map * * @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); } /**