From 6b814d24a47eb0315041548d30116462ff978458 Mon Sep 17 00:00:00 2001 From: grimsi <9295182+grimsi@users.noreply.github.com> Date: Mon, 18 Jul 2022 19:28:00 +0200 Subject: [PATCH] Implemented endpoint to get images --- .../de/grimsi/gameyfin/entities/Company.java | 2 - .../gameyfin/rest/GameyfinDevController.java | 39 ++++--------------- .../gameyfin/service/FilesystemService.java | 18 ++++++++- 3 files changed, 23 insertions(+), 36 deletions(-) diff --git a/src/main/java/de/grimsi/gameyfin/entities/Company.java b/src/main/java/de/grimsi/gameyfin/entities/Company.java index 0c26bbc..95a2e20 100644 --- a/src/main/java/de/grimsi/gameyfin/entities/Company.java +++ b/src/main/java/de/grimsi/gameyfin/entities/Company.java @@ -1,13 +1,11 @@ package de.grimsi.gameyfin.entities; -import com.fasterxml.jackson.annotation.JsonIgnore; import lombok.*; import org.hibernate.Hibernate; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; -import javax.persistence.Version; import java.util.Objects; @Entity diff --git a/src/main/java/de/grimsi/gameyfin/rest/GameyfinDevController.java b/src/main/java/de/grimsi/gameyfin/rest/GameyfinDevController.java index e58e5f5..2ed9d6b 100644 --- a/src/main/java/de/grimsi/gameyfin/rest/GameyfinDevController.java +++ b/src/main/java/de/grimsi/gameyfin/rest/GameyfinDevController.java @@ -1,21 +1,16 @@ package de.grimsi.gameyfin.rest; -import com.igdb.proto.Igdb; -import de.grimsi.gameyfin.dto.GameDto; import de.grimsi.gameyfin.entities.DetectedGame; import de.grimsi.gameyfin.entities.UnmappableFile; -import de.grimsi.gameyfin.igdb.IgdbWrapper; import de.grimsi.gameyfin.service.FilesystemService; import de.grimsi.gameyfin.service.GameService; -import de.grimsi.gameyfin.util.ProtobufUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; +import org.springframework.core.io.Resource; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; -import org.springframework.web.server.ResponseStatusException; import java.nio.file.Path; import java.util.List; @@ -24,37 +19,12 @@ import java.util.Map; @RestController public class GameyfinDevController { - @Autowired - private IgdbWrapper igdbWrapper; - @Autowired private FilesystemService filesystemService; @Autowired private GameService gameService; - @GetMapping(value = "/dev/findGameByTitle/{title}", produces = MediaType.APPLICATION_JSON_VALUE) - public GameDto findGameByTitle(@PathVariable("title") String title) { - Igdb.Game game = igdbWrapper.searchForGameByTitle(title) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find game with title: \"%s\"".formatted(title))); - - return GameDto.builder() - .name(game.getName()) - .releaseDate(ProtobufUtils.toInstant(game.getFirstReleaseDate())) - .build(); - } - - @GetMapping(value = "/dev/getGameById/{id}", produces = MediaType.APPLICATION_JSON_VALUE) - public GameDto findGameByTitle(@PathVariable("id") Long id) { - Igdb.Game game = igdbWrapper.getGameById(id) - .orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find game with id: %d".formatted(id))); - - return GameDto.builder() - .name(game.getName()) - .releaseDate(ProtobufUtils.toInstant(game.getFirstReleaseDate())) - .build(); - } - @GetMapping(value = "/dev/gameFiles", produces = MediaType.APPLICATION_JSON_VALUE) public List getAllGameFiles() { return filesystemService.getGameFiles().stream().map(Path::toString).toList(); @@ -65,12 +35,17 @@ public class GameyfinDevController { return gameService.getAllDetectedGames(); } + @GetMapping(value = "/dev/images/{imageId}", produces = MediaType.IMAGE_PNG_VALUE) + public Resource getCoverImageForGame(@PathVariable String imageId) { + return filesystemService.getImage(imageId); + } + @GetMapping(value = "/dev/scan", produces = MediaType.APPLICATION_JSON_VALUE) public void scanLibrary() { filesystemService.scanGameLibrary(); } - @GetMapping(value = "/dev/downloadCovers") + @GetMapping(value = "/dev/cache/download") public void downloadCovers() { filesystemService.downloadGameCovers(); filesystemService.downloadGameScreenshots(); diff --git a/src/main/java/de/grimsi/gameyfin/service/FilesystemService.java b/src/main/java/de/grimsi/gameyfin/service/FilesystemService.java index 38ae040..48be200 100644 --- a/src/main/java/de/grimsi/gameyfin/service/FilesystemService.java +++ b/src/main/java/de/grimsi/gameyfin/service/FilesystemService.java @@ -7,12 +7,15 @@ import de.grimsi.gameyfin.entities.UnmappableFile; import de.grimsi.gameyfin.igdb.IgdbApiProperties; import de.grimsi.gameyfin.igdb.IgdbWrapper; import de.grimsi.gameyfin.mapper.GameMapper; +import de.grimsi.gameyfin.repositories.CompanyRepository; import de.grimsi.gameyfin.repositories.DetectedGameRepository; import de.grimsi.gameyfin.repositories.UnmappableFileRepository; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FilenameUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.Resource; import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.buffer.DataBufferUtils; import org.springframework.http.HttpStatus; @@ -29,6 +32,7 @@ import reactor.core.publisher.Flux; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.Collections; import java.util.List; @@ -184,6 +188,16 @@ public class FilesystemService { log.info("Downloaded {} company logos in {} seconds.", downloadCount, (int) stopWatch.getTotalTimeSeconds()); } + public Resource getImage(String imageId) { + String filename = "%s.png".formatted(imageId); + + try { + return new ByteArrayResource(Files.readAllBytes(Paths.get("%s/%s".formatted(cacheFolderPath, filename)))); + } catch (IOException e) { + throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Could not find image file %s".formatted(filename)); + } + } + private String getFilename(Path p) { return FilenameUtils.getBaseName(p.toString()); } @@ -201,9 +215,9 @@ public class FilesystemService { entityToImageIds.entrySet().parallelStream().forEach(entry -> entry.getValue().forEach(imageId -> { - if(!StringUtils.hasText(imageId)) return; + if (!StringUtils.hasText(imageId)) return; - String imgFileName = "%s.jpg".formatted(imageId); + String imgFileName = "%s.png".formatted(imageId); String imgUrl = "t_%s/%s".formatted(imageSize, imgFileName); if (Files.exists(Path.of(cacheFolderPath, imgFileName))) {