From 4772ae075647a11555b0a8e6ddd23f97351a8667 Mon Sep 17 00:00:00 2001 From: glax Date: Thu, 25 May 2023 14:35:33 +0200 Subject: [PATCH] No unnecessary downloads of covers if they already exist --- Tranga/Connectors/MangaDex.cs | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index a61fb62..5dfd7dc 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -100,10 +100,8 @@ public class MangaDex : Connector string? coverUrl = GetCoverUrl(publicationId, posterId); string? coverCacheName = null; if (coverUrl is not null) - { - DownloadClient.RequestResult coverResult = downloadClient.MakeRequest(coverUrl, (byte)RequestType.AtHomeServer); - coverCacheName = SaveImage(coverUrl, coverResult.result); - } + coverCacheName = SaveImage(coverUrl); + string? author = GetAuthor(authorId); Dictionary linksDict = new(); @@ -300,13 +298,18 @@ public class MangaDex : Connector DownloadImage(publication.posterUrl, Path.Join(downloadLocation, publication.folderName, $"cover.{extension}"), this.downloadClient, (byte)RequestType.AtHomeServer); } - private string SaveImage(string url, Stream imageData) + private string SaveImage(string url) { string[] split = url.Split('/'); string filename = split[^1]; string saveImagePath = Path.Join(imageCachePath, filename); + + if (File.Exists(saveImagePath)) + return saveImagePath; + + DownloadClient.RequestResult coverResult = downloadClient.MakeRequest(url, (byte)RequestType.AtHomeServer); using MemoryStream ms = new(); - imageData.CopyTo(ms); + coverResult.result.CopyTo(ms); File.WriteAllBytes(saveImagePath, ms.ToArray()); return filename; }