Fix #468 failed downloads (Task cancelled) results in empty chapter archive
Some checks are pending
Docker Image CI / build (push) Waiting to run

This commit is contained in:
2025-10-10 18:23:27 +02:00
parent f72fdf54f1
commit 3ff44339b5
5 changed files with 20 additions and 23 deletions

View File

@@ -84,4 +84,10 @@ public abstract class MangaConnector(string name, string[] supportedLanguages, s
return filename.CleanNameForWindows(); return filename.CleanNameForWindows();
} }
public async Task<Stream?> DownloadImage(string imageUrl, CancellationToken ct)
{
HttpResponseMessage requestResult = await downloadClient.MakeRequest(imageUrl, RequestType.MangaImage, cancellationToken: ct);
return requestResult.IsSuccessStatusCode ? await requestResult.Content.ReadAsStreamAsync(ct) : null;
}
} }

View File

@@ -2,5 +2,6 @@
public interface IDownloadClient public interface IDownloadClient
{ {
internal Task<HttpResponseMessage> MakeRequest(string url, RequestType requestType, string? referrer = null); internal Task<HttpResponseMessage> MakeRequest(string url, RequestType requestType, string? referrer = null,
CancellationToken? cancellationToken = null);
} }

View File

@@ -12,7 +12,7 @@ public class FlareSolverrDownloadClient(HttpClient client) : IDownloadClient
{ {
private ILog Log { get; } = LogManager.GetLogger(typeof(FlareSolverrDownloadClient)); private ILog Log { get; } = LogManager.GetLogger(typeof(FlareSolverrDownloadClient));
public async Task<HttpResponseMessage> MakeRequest(string url, RequestType requestType, string? referrer = null) public async Task<HttpResponseMessage> MakeRequest(string url, RequestType requestType, string? referrer = null, CancellationToken? cancellationToken = null)
{ {
Log.Debug($"Using {typeof(FlareSolverrDownloadClient).FullName} for {url}"); Log.Debug($"Using {typeof(FlareSolverrDownloadClient).FullName} for {url}");
if(referrer is not null) if(referrer is not null)
@@ -46,7 +46,7 @@ public class FlareSolverrDownloadClient(HttpClient client) : IDownloadClient
HttpResponseMessage? response; HttpResponseMessage? response;
try try
{ {
response = await client.SendAsync(requestMessage); response = await client.SendAsync(requestMessage, cancellationToken ?? CancellationToken.None);
} }
catch (HttpRequestException e) catch (HttpRequestException e)
{ {
@@ -71,7 +71,7 @@ public class FlareSolverrDownloadClient(HttpClient client) : IDownloadClient
return response; return response;
} }
string responseString = response.Content.ReadAsStringAsync().Result; string responseString = await response.Content.ReadAsStringAsync(cancellationToken ?? CancellationToken.None);
JObject responseObj = JObject.Parse(responseString); JObject responseObj = JObject.Parse(responseString);
if (!IsInCorrectFormat(responseObj, out string? reason)) if (!IsInCorrectFormat(responseObj, out string? reason))
{ {

View File

@@ -14,7 +14,7 @@ internal class HttpDownloadClient : IDownloadClient
private static readonly FlareSolverrDownloadClient FlareSolverrDownloadClient = new(Client); private static readonly FlareSolverrDownloadClient FlareSolverrDownloadClient = new(Client);
private ILog Log { get; } = LogManager.GetLogger(typeof(HttpDownloadClient)); private ILog Log { get; } = LogManager.GetLogger(typeof(HttpDownloadClient));
public async Task<HttpResponseMessage> MakeRequest(string url, RequestType requestType, string? referrer = null) public async Task<HttpResponseMessage> MakeRequest(string url, RequestType requestType, string? referrer = null, CancellationToken? cancellationToken = null)
{ {
Log.Debug($"Using {typeof(HttpDownloadClient).FullName} for {url}"); Log.Debug($"Using {typeof(HttpDownloadClient).FullName} for {url}");
HttpRequestMessage requestMessage = new(HttpMethod.Get, url); HttpRequestMessage requestMessage = new(HttpMethod.Get, url);
@@ -24,7 +24,7 @@ internal class HttpDownloadClient : IDownloadClient
try try
{ {
HttpResponseMessage response = await Client.SendAsync(requestMessage); HttpResponseMessage response = await Client.SendAsync(requestMessage, cancellationToken ?? CancellationToken.None);
Log.Debug($"Request {url} returned {(int)response.StatusCode} {response.StatusCode}"); Log.Debug($"Request {url} returned {(int)response.StatusCode} {response.StatusCode}");
if(response.IsSuccessStatusCode) if(response.IsSuccessStatusCode)
return response; return response;

View File

@@ -102,18 +102,19 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
{ {
try try
{ {
if (DownloadImage(imageUrl) is not { } stream) if (await mangaConnector.DownloadImage(imageUrl, CancellationToken) is not { } stream)
{ {
Log.Error($"Failed to download image: {imageUrl}"); Log.Error($"Failed to download image: {imageUrl}");
return []; return [];
} }
else else
images.Add(stream); images.Add(await ProcessImage(stream, CancellationToken));
} }
catch (Exception ex) catch (Exception ex)
{ {
Log.Error(ex); Log.Error(ex);
images.ForEach(i => i.Dispose()); images.ForEach(i => i.Dispose());
return [];
} }
} }
@@ -186,7 +187,7 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
}; };
private async Task<bool> AllDownloadsFinished() => (await StartNewChapterDownloadsWorker.GetMissingChapters(DbContext, CancellationToken)).Count == 0; private async Task<bool> AllDownloadsFinished() => (await StartNewChapterDownloadsWorker.GetMissingChapters(DbContext, CancellationToken)).Count == 0;
private Stream ProcessImage(Stream imageStream) private async Task<Stream> ProcessImage(Stream imageStream, CancellationToken? cancellationToken = null)
{ {
Log.Debug("Processing image"); Log.Debug("Processing image");
imageStream.Position = 0; imageStream.Position = 0;
@@ -199,11 +200,11 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
MemoryStream processedImage = new (); MemoryStream processedImage = new ();
try try
{ {
using Image image = Image.Load(imageStream); using Image image = await Image.LoadAsync(imageStream, cancellationToken ?? CancellationToken.None);
Log.Debug("Image loaded"); Log.Debug("Image loaded");
if (Tranga.Settings.BlackWhiteImages) if (Tranga.Settings.BlackWhiteImages)
image.Mutate(i => i.ApplyProcessor(new AdaptiveThresholdProcessor())); image.Mutate(i => i.ApplyProcessor(new AdaptiveThresholdProcessor()));
image.SaveAsJpeg(processedImage, new JpegEncoder() await image.SaveAsJpegAsync(processedImage, new JpegEncoder()
{ {
Quality = Tranga.Settings.ImageCompression Quality = Tranga.Settings.ImageCompression
}); });
@@ -225,7 +226,7 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
{ {
Log.Error(e); Log.Error(e);
} }
imageStream.CopyTo(processedImage); await imageStream.CopyToAsync(processedImage);
processedImage.Position = 0; processedImage.Position = 0;
return processedImage; return processedImage;
} }
@@ -286,16 +287,5 @@ public class DownloadChapterFromMangaconnectorWorker(MangaConnectorId<Chapter> c
Log.Debug($"Copied cover from {fullCoverPath} to {newFilePath}"); Log.Debug($"Copied cover from {fullCoverPath} to {newFilePath}");
} }
private Stream? DownloadImage(string imageUrl)
{
HttpDownloadClient downloadClient = new();
HttpResponseMessage requestResult = downloadClient.MakeRequest(imageUrl, RequestType.MangaImage).Result;
if ((int)requestResult.StatusCode < 200 || (int)requestResult.StatusCode >= 300)
return null;
return ProcessImage(requestResult.Content.ReadAsStream());
}
public override string ToString() => $"{base.ToString()} {_mangaConnectorIdId}"; public override string ToString() => $"{base.ToString()} {_mangaConnectorIdId}";
} }