Fixed illegal characters in filenames

Fixed logic for downloading images (separation between Connector and inherited classes)
This commit is contained in:
glax 2023-05-18 17:21:06 +02:00
parent 93bb8ef6ee
commit 9a01db710b
2 changed files with 19 additions and 19 deletions

View File

@ -12,14 +12,17 @@ public abstract class Connector
public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to? public abstract void DownloadChapter(Publication publication, Chapter chapter); //where to?
internal abstract void DownloadImage(string url, string path); 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(); string tempFolder = Path.GetTempFileName();
File.Delete(tempFolder); File.Delete(tempFolder);
Directory.CreateDirectory(tempFolder); Directory.CreateDirectory(tempFolder);
DownloadImage(url, tempFolder); int chapter = 0;
ZipFile.CreateFromDirectory(tempFolder, $"{outputFolder}.cbz"); foreach(string imageUrl in imageUrls)
DownloadImage(imageUrl, Path.Join(tempFolder, $"{chapter++}"));
ZipFile.CreateFromDirectory(tempFolder, $"{outputFolderPath}.cbz");
} }
internal class DownloadClient internal class DownloadClient

View File

@ -155,25 +155,22 @@ public class MangaDex : Connector
return; return;
string baseUrl = result["baseUrl"]!.GetValue<string>(); string baseUrl = result["baseUrl"]!.GetValue<string>();
string hash = result["chapter"]!["hash"].GetValue<string>(); string hash = result["chapter"]!["hash"]!.GetValue<string>();
JsonArray imageFileNamesObject = result["chapter"]!["data"]!.AsArray(); JsonArray imageFileNames = result["chapter"]!["data"]!.AsArray();
HashSet<string> imageFileNames = new(); HashSet<string> imageUrls = new();
foreach (JsonObject imageFileNameObject in imageFileNamesObject) foreach (JsonNode image in imageFileNames)
imageFileNames.Add(imageFileNameObject!.GetValue<string>()); imageUrls.Add($"{baseUrl}/{hash}/{image!.GetValue<string>()}");
foreach(string imageFileName in imageFileNames) string fileName = string.Concat(publication.sortName.Split(Path.GetInvalidFileNameChars()));
DownloadChapterImage($"{baseUrl}/{hash}/{imageFileName}", Path.Join(downloadLocation, publication.sortName));
DownloadChapter(imageUrls.ToArray(), Path.Join(downloadLocation, fileName));
} }
internal override void DownloadImage(string url, string path) internal override void DownloadImage(string url, string path)
{ {
DownloadClient.RequestResult requestResult = _downloadClient.GetPage(url); DownloadClient.RequestResult requestResult = _downloadClient.MakeRequest(url);
FileStream fs = new FileStream(path, FileMode.CreateNew); byte[] buffer = new byte[requestResult.result.Length];
Span<byte> buffer = new(); requestResult.result.ReadExactly(buffer, 0, buffer.Length);
while (requestResult.result.CanRead) File.WriteAllBytes(path, buffer);
{
_ = requestResult.result.Read(buffer);
fs.Write(buffer);
}
} }
} }