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

View File

@ -155,25 +155,22 @@ public class MangaDex : Connector
return;
string baseUrl = result["baseUrl"]!.GetValue<string>();
string hash = result["chapter"]!["hash"].GetValue<string>();
JsonArray imageFileNamesObject = result["chapter"]!["data"]!.AsArray();
HashSet<string> imageFileNames = new();
foreach (JsonObject imageFileNameObject in imageFileNamesObject)
imageFileNames.Add(imageFileNameObject!.GetValue<string>());
foreach(string imageFileName in imageFileNames)
DownloadChapterImage($"{baseUrl}/{hash}/{imageFileName}", Path.Join(downloadLocation, publication.sortName));
string hash = result["chapter"]!["hash"]!.GetValue<string>();
JsonArray imageFileNames = result["chapter"]!["data"]!.AsArray();
HashSet<string> imageUrls = new();
foreach (JsonNode image in imageFileNames)
imageUrls.Add($"{baseUrl}/{hash}/{image!.GetValue<string>()}");
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<byte> 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);
}
}