diff --git a/Tranga/Connector.cs b/Tranga/Connector.cs index 8d31a0a..93ee2b0 100644 --- a/Tranga/Connector.cs +++ b/Tranga/Connector.cs @@ -1,4 +1,5 @@ -using System.Net; +using System.IO.Compression; +using System.Net; namespace Tranga; @@ -9,10 +10,16 @@ public abstract class Connector public abstract Publication[] GetPublications(string publicationTitle = ""); public abstract Chapter[] GetChapters(Publication publication); public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to? + internal abstract void DownloadImage(string url, string path); - internal void DownloadChapter(string url) + internal void DownloadChapterImage(string url, string outputFolder) { + string tempFolder = Path.GetTempFileName(); + File.Delete(tempFolder); + Directory.CreateDirectory(tempFolder); + DownloadImage(url, tempFolder); + ZipFile.CreateFromDirectory(tempFolder, $"{outputFolder}.cbz"); } internal class DownloadClient diff --git a/Tranga/Connectors/MangaDex.cs b/Tranga/Connectors/MangaDex.cs index 50fec60..f972c55 100644 --- a/Tranga/Connectors/MangaDex.cs +++ b/Tranga/Connectors/MangaDex.cs @@ -161,6 +161,18 @@ public class MangaDex : Connector imageFileNames.Add(imageFileNameObject!.GetValue()); foreach(string imageFileName in imageFileNames) - DownloadChapter($"{baseUrl}/{hash}/imageFileName"); + DownloadChapterImage($"{baseUrl}/{hash}/{imageFileName}", Path.Join(downloadLocation, publication.sortName)); + } + + 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); + } } } \ No newline at end of file