WIP: Simplified logic on when to download single file vs when to zip folder

This commit is contained in:
grimsi
2022-07-24 20:52:29 +02:00
parent aa8fd0d613
commit 80e3ff05d5
@@ -98,19 +98,6 @@ public class FilesystemService {
Path path = Path.of(g.getPath());
if(!path.toFile().isDirectory()) return getFilenameWithExtension(path);
Optional<Path> optionalGameArchive;
try (Stream<Path> filesStream = Files.list(path)) {
optionalGameArchive = filesStream.filter(this::hasGameArchiveExtension).findFirst();
} catch (IOException e) {
log.error("Error while accessing folder:", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Error while accessing folder '%s'.".formatted(path));
}
if(optionalGameArchive.isPresent()) {
return getFilenameWithExtension(optionalGameArchive.get());
}
return getFilenameWithExtension(path) + ".zip";
}
@@ -242,7 +229,7 @@ public class FilesystemService {
Path path = Path.of(game.getPath());
if(path.toFile().isDirectory()) {
downloadFromFolder(path, outputStream);
downloadFilesAsZip(path, outputStream);
} else {
downloadFile(path, outputStream);
}
@@ -260,23 +247,6 @@ public class FilesystemService {
}
}
private void downloadFromFolder(Path path, OutputStream outputStream) {
Optional<Path> optionalGameArchive;
try (Stream<Path> filesStream = Files.list(path)) {
optionalGameArchive = filesStream.filter(this::hasGameArchiveExtension).findFirst();
} catch (IOException e) {
log.error("Error while accessing folder:", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Error while accessing folder '%s'.".formatted(path));
}
if(optionalGameArchive.isPresent()) {
downloadFile(optionalGameArchive.get(), outputStream);
} else {
downloadFilesAsZip(path, outputStream);
}
}
private void downloadFilesAsZip(Path path, OutputStream outputStream) {
ZipOutputStream zos = new ZipOutputStream(outputStream){{
def.setLevel(Deflater.NO_COMPRESSION);