Commit 100ff291 authored by Martin Tvarožek's avatar Martin Tvarožek
Browse files

fix: fix downloads and add rough logging

parent 26bb5a72
Loading
Loading
Loading
Loading
+31 −11
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ public class GameImporterService(
    };
    
    private static readonly string TempDirPath = Path.Join(Path.GetTempPath(), "GarrigueGamesImport");
    private const string ImgSubdirName = "img";
    
    private static readonly Regex ImageRegex = new(@"^image\/(jpeg|png|webp)$", RegexOptions.IgnoreCase);
    
@@ -169,13 +170,20 @@ public class GameImporterService(
        var targetZipPath = Path.Join(TempDirPath, $"{downloadId}.zip");
        var targetDirPath = Path.Join(TempDirPath, downloadId);
        
        Console.WriteLine($"Downloading game {details.Name.Iv}");
        
        var fileBytes = await kafeService.CustomDownloadAsync(downloadId);
        await File.WriteAllBytesAsync(targetZipPath, fileBytes);
        
        Console.WriteLine($"Unzipping archive {targetZipPath}");
        
        var result = await Unzip(targetZipPath, targetDirPath);
        if (result.IsFailure) return result;
        
        Console.WriteLine($"Downloading additional media");
        
        var images = await GetImagesDataAsync(details.Media);
        var thumbnailImg = $"{images.FirstOrDefault(x => x.Item2 == thumbnailId).Item2}.jpeg";
        
        var gameData = new GameDto
        {
@@ -185,8 +193,8 @@ public class GameImporterService(
            Name = details.Name.Iv,
            Description = details.Description.Iv,
            ExePath = GetLaunchExe(targetDirPath),
            ThumbnailPath = images.FirstOrDefault(x => x.Item2 == thumbnailId).Item2,
            GalleryImagePaths = images.Select(x => x.Item2).ToList(),
            ThumbnailPath = Path.Join(ImgSubdirName, thumbnailImg),
            GalleryImagePaths = images.Select(x => Path.Join(ImgSubdirName, $"{x.Item2}.jpeg")).ToList(),
            Tags = details.Tags.Select(x => x.Id).ToList()
        };
        
@@ -202,18 +210,32 @@ public class GameImporterService(
    /// <returns>A <see cref="Result"/> indicating success or failure.</returns>
    private async Task<Result> ProcessDownloadedZip(string source, GameDto data, ICollection<(byte[], string)> imageData)
    {
        Console.WriteLine($"Processing unzipped archive {source}");
        
        var targetDir = MoveToNewDirectory(source, data);
        var imgSubdirPath = Path.Join(targetDir, ImgSubdirName);
        
        foreach (var (imgBytes, imgPath) in imageData)
        if (!Directory.Exists(imgSubdirPath)) Directory.CreateDirectory(imgSubdirPath);
        
        foreach (var (imgBytes, imgId) in imageData)
        {
            await File.WriteAllBytesAsync(Path.Join(targetDir, imgPath), imgBytes);
            // We download JPEGs from KAFE, so we can force the file extension
            var imgPath = Path.Join(imgSubdirPath, $"{imgId}.jpeg");
            Console.WriteLine($"Writing image {imgPath}");
            
            await File.WriteAllBytesAsync(imgPath, imgBytes);
        }
        
        Console.WriteLine($"Creating data.json file");
        
        var jsonPath = Path.Join(targetDir, "data.json");
        var jsonString = JsonSerializer.Serialize(data);
        await File.WriteAllTextAsync(jsonPath, jsonString);
        
        Console.WriteLine($"Adding game ({data.GameId}) to database");
        await dbService.AddGameAsync(data.GameId, targetDir);
        
        Console.WriteLine($"Successfully added {data.Name}");
        return Result.Success();
    }
    
@@ -309,7 +331,7 @@ public class GameImporterService(
    /// Filters images from all game media and downloads their bytes.
    /// </summary>
    /// <param name="media">A collection of <see cref="GameMedia"/> objects.</param>
    /// <returns>A list of byte arrays and relative paths for images.</returns>
    /// <returns>A list of byte arrays and IDs for images.</returns>
    private async Task<List<(byte[], string)>> GetImagesDataAsync(ICollection<GameMedia> media)
    {
        List<(byte[], string)> images = [];
@@ -318,12 +340,10 @@ public class GameImporterService(
        {
            if (!ImageRegex.IsMatch(medium.MediaType)) continue;
            
            // imgData downloads a JPEG, we can enforce the extension
            var imgPath = $"img/{medium.AttachmentId}.jpeg";
            var imgData = await kafeService.GetImageBytesAsync(medium.AttachmentId, Resolution.Original);
            if (imgData == null) continue;
            
            images.Add((imgData, imgPath));
            images.Add((imgData, medium.AttachmentId));
        }

        return images;