Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File upload exercises: Fix an issue with old file submissions not being deleted #7276

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
krusche marked this conversation as resolved.
Show resolved Hide resolved
// 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down