fix: extract shared deleteDirectory utility to eliminate duplication
Build & Deploy / build-and-deploy (push) Successful in 55s
Build & Deploy / build-and-deploy (push) Successful in 55s
- Created FileUtils.deleteDirectory() for recursive directory deletion - GameService.delete() and UploadResource.deleteDir() now delegate to it - Removed stale BasicFileAttributes import from GameService
This commit is contained in:
@@ -0,0 +1,28 @@
|
|||||||
|
package com.dostalgia;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.*;
|
||||||
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
|
|
||||||
|
/** Shared file-system utilities. */
|
||||||
|
public final class FileUtils {
|
||||||
|
private FileUtils() {}
|
||||||
|
|
||||||
|
/** Recursively delete a directory tree (walk in reverse order so dirs are empty when deleted). */
|
||||||
|
public static void deleteDirectory(final Path dir) throws IOException {
|
||||||
|
if (!Files.exists(dir)) return;
|
||||||
|
Files.walkFileTree(dir, new SimpleFileVisitor<>() {
|
||||||
|
@Override
|
||||||
|
public FileVisitResult visitFile(final Path f, final BasicFileAttributes a) throws IOException {
|
||||||
|
Files.delete(f);
|
||||||
|
return FileVisitResult.CONTINUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FileVisitResult postVisitDirectory(final Path d, final IOException e) throws IOException {
|
||||||
|
Files.delete(d);
|
||||||
|
return FileVisitResult.CONTINUE;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,13 +12,10 @@ import org.eclipse.microprofile.config.inject.ConfigProperty;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.nio.file.FileVisitResult;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.NoSuchFileException;
|
import java.nio.file.NoSuchFileException;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.SimpleFileVisitor;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
@@ -122,21 +119,7 @@ public class GameService implements GameStore {
|
|||||||
|
|
||||||
/** Delete a game and all its files. */
|
/** Delete a game and all its files. */
|
||||||
public void delete(String id) throws IOException {
|
public void delete(String id) throws IOException {
|
||||||
Path dir = gameDir(id);
|
FileUtils.deleteDirectory(gameDir(id));
|
||||||
if (Files.exists(dir)) {
|
|
||||||
Files.walkFileTree(dir, new SimpleFileVisitor<>() {
|
|
||||||
@Override
|
|
||||||
public @Nonnull FileVisitResult visitFile(@Nonnull Path f, @Nonnull BasicFileAttributes a) throws IOException {
|
|
||||||
Files.delete(f);
|
|
||||||
return FileVisitResult.CONTINUE;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public @Nonnull FileVisitResult postVisitDirectory(@Nonnull Path d, IOException e) throws IOException {
|
|
||||||
Files.delete(d);
|
|
||||||
return FileVisitResult.CONTINUE;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// Clean up old flat .jsdos locations (pre-game-dir layout — backward compat)
|
// Clean up old flat .jsdos locations (pre-game-dir layout — backward compat)
|
||||||
Files.deleteIfExists(gamesDir.resolve(id + ".jsdos"));
|
Files.deleteIfExists(gamesDir.resolve(id + ".jsdos"));
|
||||||
Files.deleteIfExists(gamesDir.resolve(id + ".setup.jsdos"));
|
Files.deleteIfExists(gamesDir.resolve(id + ".setup.jsdos"));
|
||||||
|
|||||||
@@ -11,16 +11,12 @@ import org.jboss.resteasy.reactive.RestForm;
|
|||||||
import org.jboss.resteasy.reactive.multipart.FileUpload;
|
import org.jboss.resteasy.reactive.multipart.FileUpload;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.FileVisitResult;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.SimpleFileVisitor;
|
|
||||||
import java.nio.file.StandardCopyOption;
|
import java.nio.file.StandardCopyOption;
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
import jakarta.annotation.Nonnull;
|
|
||||||
|
|
||||||
@jakarta.ws.rs.Path("/api/upload")
|
@jakarta.ws.rs.Path("/api/upload")
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
@@ -221,18 +217,6 @@ public class UploadResource {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void deleteDir(Path dir) throws IOException {
|
private void deleteDir(Path dir) throws IOException {
|
||||||
if (!Files.exists(dir)) return;
|
FileUtils.deleteDirectory(dir);
|
||||||
Files.walkFileTree(dir, new SimpleFileVisitor<>() {
|
|
||||||
@Override
|
|
||||||
public @Nonnull FileVisitResult visitFile(@Nonnull Path f, @Nonnull BasicFileAttributes a) throws IOException {
|
|
||||||
Files.delete(f);
|
|
||||||
return FileVisitResult.CONTINUE;
|
|
||||||
}
|
|
||||||
@Override
|
|
||||||
public @Nonnull FileVisitResult postVisitDirectory(@Nonnull Path d, IOException e) throws IOException {
|
|
||||||
Files.delete(d);
|
|
||||||
return FileVisitResult.CONTINUE;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user