From e37a6bf5a2cd4d8c2feee6264d6434180550569f Mon Sep 17 00:00:00 2001 From: Erik Stern Date: Wed, 27 Sep 2023 14:11:33 +0200 Subject: [PATCH 1/3] Report error if to-be-deleted file does not exist. --- src/main/java/de/tum/in/www1/artemis/service/FileService.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/de/tum/in/www1/artemis/service/FileService.java b/src/main/java/de/tum/in/www1/artemis/service/FileService.java index ac239e2b645d..d9208a6f57ee 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/FileService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/FileService.java @@ -768,6 +768,10 @@ public void schedulePathForDeletion(@Nullable Path path, long delayInMinutes) { log.info("Delete file {}", path); Files.delete(path); } + else { + log.error("Deleting the file {} did not work because it does not exist", path); + } + futures.remove(path); } catch (IOException e) { From 292834f9f2d168698ab8212b636c2097ff0fcd67 Mon Sep 17 00:00:00 2001 From: Erik Stern Date: Wed, 27 Sep 2023 14:24:30 +0200 Subject: [PATCH 2/3] Converting public path to actual path prior to scheduling deletion. --- .../artemis/domain/FileUploadSubmission.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java b/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java index 29bafff4bcb4..3a4263116c42 100644 --- a/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java +++ b/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java @@ -4,8 +4,11 @@ import javax.persistence.*; +import org.apache.commons.lang3.math.NumberUtils; + import com.fasterxml.jackson.annotation.JsonInclude; +import de.tum.in.www1.artemis.exception.FilePathParsingException; import de.tum.in.www1.artemis.service.FilePathService; import de.tum.in.www1.artemis.service.FileService; @@ -34,8 +37,25 @@ public String getSubmissionExerciseType() { @PostRemove public void onDelete() { if (filePath != null) { - fileService.schedulePathForDeletion(Path.of(filePath), 0); + Path actualPath = getActualPathForPublicPath(filePath); + fileService.schedulePathForDeletion(actualPath, 0); + } + } + + private Path getActualPathForPublicPath(String filePath) { + // Note: This method duplicates functionality from actualPathForPublicPath on FilePathService + // but to avoid this entity depending on the FilePathService service this is separate. + + final var splittedPath = filePath.split("/"); + final var shouldBeExerciseId = splittedPath.length >= 5 ? splittedPath[4] : null; + if (!NumberUtils.isCreatable(shouldBeExerciseId)) { + throw new FilePathParsingException("Unexpected String in upload file path. Should contain the exercise ID: " + shouldBeExerciseId); } + final var exerciseId = Long.parseLong(shouldBeExerciseId); + + Path submissionDirectory = FileUploadSubmission.buildFilePath(exerciseId, getId()); + Path fileName = Path.of(filePath).getFileName(); + return submissionDirectory.resolve(fileName); } public String getFilePath() { From 9197d31ec38329fd561e0c62d9dde728e84d18a6 Mon Sep 17 00:00:00 2001 From: Erik Stern Date: Wed, 27 Sep 2023 18:54:49 +0200 Subject: [PATCH 3/3] Use FilePathService service instead of custom method. --- .../artemis/domain/FileUploadSubmission.java | 22 ++----------------- .../www1/artemis/service/FilePathService.java | 6 ++--- 2 files changed, 5 insertions(+), 23 deletions(-) diff --git a/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java b/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java index 3a4263116c42..66f24f5ec12e 100644 --- a/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java +++ b/src/main/java/de/tum/in/www1/artemis/domain/FileUploadSubmission.java @@ -1,14 +1,12 @@ package de.tum.in.www1.artemis.domain; +import java.net.URI; import java.nio.file.Path; import javax.persistence.*; -import org.apache.commons.lang3.math.NumberUtils; - import com.fasterxml.jackson.annotation.JsonInclude; -import de.tum.in.www1.artemis.exception.FilePathParsingException; import de.tum.in.www1.artemis.service.FilePathService; import de.tum.in.www1.artemis.service.FileService; @@ -37,27 +35,11 @@ public String getSubmissionExerciseType() { @PostRemove public void onDelete() { if (filePath != null) { - Path actualPath = getActualPathForPublicPath(filePath); + Path actualPath = FilePathService.actualPathForPublicPath(URI.create(filePath)); fileService.schedulePathForDeletion(actualPath, 0); } } - private Path getActualPathForPublicPath(String filePath) { - // Note: This method duplicates functionality from actualPathForPublicPath on FilePathService - // but to avoid this entity depending on the FilePathService service this is separate. - - final var splittedPath = filePath.split("/"); - final var shouldBeExerciseId = splittedPath.length >= 5 ? splittedPath[4] : null; - if (!NumberUtils.isCreatable(shouldBeExerciseId)) { - throw new FilePathParsingException("Unexpected String in upload file path. Should contain the exercise ID: " + shouldBeExerciseId); - } - final var exerciseId = Long.parseLong(shouldBeExerciseId); - - Path submissionDirectory = FileUploadSubmission.buildFilePath(exerciseId, getId()); - Path fileName = Path.of(filePath).getFileName(); - return submissionDirectory.resolve(fileName); - } - public String getFilePath() { return filePath; } diff --git a/src/main/java/de/tum/in/www1/artemis/service/FilePathService.java b/src/main/java/de/tum/in/www1/artemis/service/FilePathService.java index 8281338bb1ee..191c8f9037ba 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/FilePathService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/FilePathService.java @@ -90,7 +90,7 @@ public Path actualPathForPublicPathOrThrow(URI publicPath) { * @param publicPath the public file url to convert * @return the actual path to that file in the local filesystem */ - public Path actualPathForPublicPath(URI publicPath) { + public static Path actualPathForPublicPath(URI publicPath) { // first extract the filename from the url String uriPath = publicPath.getPath(); Path path = Path.of(uriPath); @@ -129,7 +129,7 @@ public Path actualPathForPublicPath(URI publicPath) { return null; } - private Path actualPathForPublicAttachmentUnitFilePath(URI publicPath, String filename) { + private static Path actualPathForPublicAttachmentUnitFilePath(URI publicPath, String filename) { Path path = Path.of(publicPath.getPath()); if (!publicPath.toString().contains("/slide")) { String attachmentUnitId = path.getName(4).toString(); @@ -148,7 +148,7 @@ private Path actualPathForPublicAttachmentUnitFilePath(URI publicPath, String fi } } - private Path actualPathForPublicFileUploadExercisesFilePath(URI publicPath, String filename) { + private static Path actualPathForPublicFileUploadExercisesFilePath(URI publicPath, String filename) { Path path = Path.of(publicPath.getPath()); try { String expectedExerciseId = path.getName(3).toString();