From 6346cfc4e9537256cf1b94b4a0ae241626d1fe46 Mon Sep 17 00:00:00 2001 From: Erik <53352472+theblobinthesky@users.noreply.github.com> Date: Tue, 3 Oct 2023 12:00:45 +0200 Subject: [PATCH] File upload exercises: Fix an issue with old file submissions not being deleted (#7276) --- .../de/tum/in/www1/artemis/domain/FileUploadSubmission.java | 4 +++- .../de/tum/in/www1/artemis/service/FilePathService.java | 6 +++--- .../java/de/tum/in/www1/artemis/service/FileService.java | 4 ++++ 3 files changed, 10 insertions(+), 4 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 29bafff4bcb4..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,5 +1,6 @@ package de.tum.in.www1.artemis.domain; +import java.net.URI; import java.nio.file.Path; import javax.persistence.*; @@ -34,7 +35,8 @@ public String getSubmissionExerciseType() { @PostRemove public void onDelete() { if (filePath != null) { - fileService.schedulePathForDeletion(Path.of(filePath), 0); + Path actualPath = FilePathService.actualPathForPublicPath(URI.create(filePath)); + fileService.schedulePathForDeletion(actualPath, 0); } } 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(); 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 2226bf6962eb..814b47092fd6 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 @@ -791,6 +791,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) {