Merge branch 'release-1.2.4' into gh33_ImproveTestCoverage

This commit is contained in:
Simon
2022-10-21 19:27:50 +03:00
committed by GitHub
12 changed files with 114 additions and 14 deletions
@@ -5,11 +5,16 @@ import de.grimsi.gameyfin.entities.DetectedGame;
import de.grimsi.gameyfin.service.DownloadService;
import de.grimsi.gameyfin.service.GameService;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
@@ -44,17 +49,33 @@ public class GamesController {
return gameService.getAllMappings();
}
@GetMapping(value="/game/{slug}/download")
@GetMapping(value = "/game/{slug}/download")
public ResponseEntity<StreamingResponseBody> downloadGameFiles(@PathVariable String slug) {
DetectedGame game = gameService.getDetectedGame(slug);
String downloadFileName = downloadService.getDownloadFileName(game);
long downloadFileSize = downloadService.getDownloadFileSize(game);
HttpHeaders headers = new HttpHeaders();
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"%s\"".formatted(downloadFileName));
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
headers.add(HttpHeaders.PRAGMA, "no-cache");
headers.add(HttpHeaders.EXPIRES, "0");
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
if (downloadFileSize > 0) {
headers.setContentLength(downloadFileSize);
}
return ResponseEntity
.ok()
.header("Content-Disposition", "attachment; filename=\"%s\"".formatted(downloadFileName))
.headers(headers)
.body(out -> downloadService.sendGamefilesToClient(game, out));
}
@GetMapping(value = "/game/{slug}/refresh", produces = MediaType.APPLICATION_JSON_VALUE)
public DetectedGame refreshGame(@PathVariable String slug) {
return gameService.refreshGame(slug);
}
}
@@ -6,6 +6,7 @@ import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.ClientAbortException;
import org.apache.commons.io.FileUtils;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Service;
import org.springframework.util.StopWatch;
@@ -34,6 +35,23 @@ public class DownloadService {
return getFilenameWithExtension(path) + ".zip";
}
public long getDownloadFileSize(DetectedGame game) {
Path path = Path.of(game.getPath());
try {
if (!path.toFile().isDirectory()) {
long fileSize = filesystemService.getSizeOnDisk(path);
log.info("Calculated file size for {} ({} MB).", path, Math.divideExact(fileSize, 1000000L));
return fileSize;
} else {
// return zero since we cannot set content length for ZipOutputStreams that are used to archive directories
return 0;
}
} catch (IOException e) {
throw new DownloadAbortedException();
}
}
public Resource sendImageToClient(String imageId) {
String filename = "%s.png".formatted(imageId);
return filesystemService.getFileFromCache(filename);
@@ -78,6 +96,7 @@ public class DownloadService {
}
private void sendGamefilesAsZipToClient(Path path, OutputStream outputStream) {
log.info("Archiving game path {} for download...", path);
ZipOutputStream zos = new ZipOutputStream(outputStream) {{
def.setLevel(Deflater.NO_COMPRESSION);
}};
@@ -87,6 +106,7 @@ public class DownloadService {
@SneakyThrows
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
zos.putNextEntry(new ZipEntry(path.relativize(file).toString()));
log.debug("Adding file {} to archive...", file);
Files.copy(file, zos);
zos.closeEntry();
@@ -9,6 +9,7 @@ import de.grimsi.gameyfin.mapper.GameMapper;
import de.grimsi.gameyfin.repositories.DetectedGameRepository;
import de.grimsi.gameyfin.repositories.UnmappableFileRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.web.server.ResponseStatusException;
@@ -20,6 +21,7 @@ import java.util.Optional;
import java.util.stream.Collectors;
@RequiredArgsConstructor
@Slf4j
@Service
public class GameService {
@@ -87,6 +89,17 @@ public class GameService {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Path '%s' not in database".formatted(path));
}
public DetectedGame refreshGame(String slug) {
Optional<DetectedGame> optionalDetectedGame = detectedGameRepository.findById(slug);
if (optionalDetectedGame.isPresent()) {
log.info("Refreshing game with slug '{}'", slug);
return mapDetectedGame(optionalDetectedGame.get(), slug);
}
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Game with slug '%s' not found in database".formatted(slug));
}
private DetectedGame mapUnmappableFile(UnmappableFile unmappableFile, String slug) {
Igdb.Game igdbGame = igdbWrapper.getGameBySlug(slug)
.orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Game with slug '%s' does not exist on IGDB.".formatted(slug)));
@@ -106,8 +119,6 @@ public class GameService {
DetectedGame game = gameMapper.toDetectedGame(igdbGame, Path.of(existingGame.getPath()));
game = detectedGameRepository.save(game);
detectedGameRepository.deleteById(existingGame.getSlug());
return game;
}
}