Added game file size

This commit is contained in:
grimsi
2022-07-24 23:17:11 +02:00
parent f2197a3bd4
commit b86544b22a
9 changed files with 110 additions and 38 deletions
@@ -80,6 +80,9 @@ public class DetectedGame {
@Column(nullable = false)
private String path;
@Column(nullable = false)
private long diskSize;
@Column(columnDefinition = "boolean default false")
private boolean confirmedMatch;
@@ -3,11 +3,21 @@ package de.grimsi.gameyfin.mapper;
import com.igdb.proto.Igdb;
import de.grimsi.gameyfin.dto.GameOverviewDto;
import de.grimsi.gameyfin.entities.DetectedGame;
import de.grimsi.gameyfin.service.LibraryService;
import de.grimsi.gameyfin.util.ProtobufUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
@Slf4j
public class GameMapper {
public static DetectedGame toDetectedGame(Igdb.Game g, Path path) {
@@ -37,6 +47,7 @@ public class GameMapper {
.themes(ThemeMapper.toThemes(g.getThemesList()))
.playerPerspectives(PlayerPerspectiveMapper.toPlayerPerspectives(g.getPlayerPerspectivesList()))
.path(path.toString())
.diskSize(calculateDiskSize(path))
.build();
}
@@ -63,4 +74,16 @@ public class GameMapper {
private static int getMaxPlayers(List<Igdb.MultiplayerMode> modes) {
return modes.stream().mapToInt(Igdb.MultiplayerMode::getOnlinecoopmax).max().orElse(0);
}
private static long calculateDiskSize(Path path) {
try(Stream<Path> pathStream = Files.walk(path)) {
return pathStream
.filter(p -> p.toFile().isFile())
.mapToLong(p -> p.toFile().length())
.sum();
} catch (IOException e) {
log.error("Unable to calculate size for path '{}'.", path);
return -1;
}
}
}
@@ -13,6 +13,7 @@ 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.data.repository.core.RepositoryCreationException;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
@@ -157,6 +158,7 @@ public class DownloadService {
Files.copy(path, outputStream);
} catch (IOException e) {
log.error("Error while downloading file:", e);
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Could not load file '%s'.".formatted(path));
}
}