Implemented endpoint to get images

This commit is contained in:
grimsi
2022-07-18 19:28:00 +02:00
parent f7f989e3c9
commit 6b814d24a4
3 changed files with 23 additions and 36 deletions
@@ -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
@@ -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<String> 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();
@@ -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))) {