2
0

Added Image download and compression to archive to Connector.

This commit is contained in:
glax 2023-05-18 16:42:00 +02:00
parent c3cb4d6e08
commit b54ac730b7
2 changed files with 22 additions and 3 deletions

View File

@ -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

View File

@ -161,6 +161,18 @@ public class MangaDex : Connector
imageFileNames.Add(imageFileNameObject!.GetValue<string>());
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<byte> buffer = new();
while (requestResult.result.CanRead)
{
_ = requestResult.result.Read(buffer);
fs.Write(buffer);
}
}
}