From 9a01db710b6bdfac604684121e22ff30d8fee059 Mon Sep 17 00:00:00 2001 From: glax <--local> Date: Thu, 18 May 2023 17:21:06 +0200 Subject: [PATCH] Fixed illegal characters in filenames Fixed logic for downloading images (separation between Connector and inherited classes) --- Tranga/Connector.cs | 9 ++++++--- Tranga/Connectors/MangaDex.cs | 29 +++++++++++++---------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index fcb82cb..b859ba7 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -12,14 +12,17 @@ public abstract class Connector public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to? internal abstract void DownloadImage(string url, string path); - internal void DownloadChapterImage(string url, string outputFolder) + internal void DownloadChapter(string[] imageUrls, string outputFolderPath) { string tempFolder = Path.GetTempFileName(); File.Delete(tempFolder); Directory.CreateDirectory(tempFolder); + + int chapter = 0; + foreach(string imageUrl in imageUrls) + DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}")); - DownloadImage(url, tempFolder); - ZipFile.CreateFromDirectory(tempFolder, $"{outputFolder}.cbz"); + ZipFile.CreateFromDirectory(tempFolder, $"{outputFolderPath}.cbz"); } internal class DownloadClient diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 1dd6a7b..39150b0 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -155,25 +155,22 @@ public class MangaDex : Connector return; string baseUrl = result["baseUrl"]!.GetValue(); - string hash = result["chapter"]!["hash"].GetValue(); - JsonArray imageFileNamesObject = result["chapter"]!["data"]!.AsArray(); - HashSet imageFileNames = new(); - foreach (JsonObject imageFileNameObject in imageFileNamesObject) - imageFileNames.Add(imageFileNameObject!.GetValue()); - - foreach(string imageFileName in imageFileNames) - DownloadChapterImage($"{baseUrl}/{hash}/{imageFileName}", Path.Join(downloadLocation, publication.sortName)); + string hash = result["chapter"]!["hash"]!.GetValue(); + JsonArray imageFileNames = result["chapter"]!["data"]!.AsArray(); + HashSet imageUrls = new(); + foreach (JsonNode image in imageFileNames) + imageUrls.Add($"{baseUrl}/{hash}/{image!.GetValue()}"); + + string fileName = string.Concat(publication.sortName.Split(Path.GetInvalidFileNameChars())); + + DownloadChapter(imageUrls.ToArray(), Path.Join(downloadLocation, fileName)); } internal override void DownloadImage(string url, string path) { - DownloadClient.RequestResult requestResult = _downloadClient.GetPage(url); - FileStream fs = new FileStream(path, FileMode.CreateNew); - Span buffer = new(); - while (requestResult.result.CanRead) - { - _ = requestResult.result.Read(buffer); - fs.Write(buffer); - } + DownloadClient.RequestResult requestResult = _downloadClient.MakeRequest(url); + byte[] buffer = new byte[requestResult.result.Length]; + requestResult.result.ReadExactly(buffer, 0, buffer.Length); + File.WriteAllBytes(path, buffer); } } \ No newline at end of file