Skip to content

Commit eeef5e4

Browse files
committed
Add upload engine backup error handling
1 parent 426d39c commit eeef5e4

File tree

5 files changed

+81
-23
lines changed

5 files changed

+81
-23
lines changed

SmartImage.Lib 3/Engines/Impl/Upload/BaseUploadEngine.cs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ protected BaseUploadEngine(string s)
2525

2626
// public static BaseUploadEngine Default { get; } = new LitterboxEngine();
2727

28-
public abstract Task<Url> UploadFileAsync(string file, CancellationToken ct = default);
28+
public abstract Task<BaseUploadResponse> UploadFileAsync(string file, CancellationToken ct = default);
2929

3030
public long Size { get; set; }
3131

@@ -37,6 +37,11 @@ private protected bool IsFileSizeValid(string file)
3737
return !b;
3838
}
3939

40+
protected bool Paranoid { get; set; }
41+
42+
protected abstract Task<BaseUploadResponse> VerifyResultAsync(IFlurlResponse response,
43+
CancellationToken ct = default);
44+
4045
protected void Verify(string file)
4146
{
4247
if (string.IsNullOrWhiteSpace(file)) {
@@ -56,22 +61,46 @@ protected void Verify(string file)
5661

5762
public abstract class BaseCatboxEngine : BaseUploadEngine
5863
{
59-
public override async Task<Url> UploadFileAsync(string file, CancellationToken ct = default)
64+
public override async Task<BaseUploadResponse> UploadFileAsync(string file, CancellationToken ct = default)
6065
{
6166
Verify(file);
6267

63-
using var response = await EndpointUrl
64-
.PostMultipartAsync(mp =>
65-
mp.AddFile("fileToUpload", file)
66-
.AddString("reqtype", "fileupload")
67-
.AddString("time", "1h")
68-
, ct);
68+
var response = await EndpointUrl
69+
.PostMultipartAsync(mp =>
70+
mp.AddFile("fileToUpload", file)
71+
.AddString("reqtype", "fileupload")
72+
.AddString("time", "1h")
73+
, ct);
74+
75+
return await VerifyResultAsync(response, ct).ConfigureAwait(false);
76+
}
6977

78+
protected override async Task<BaseUploadResponse> VerifyResultAsync(
79+
IFlurlResponse response, CancellationToken ct = default)
80+
{
7081
var responseMessage = response.ResponseMessage;
7182

72-
var content = await responseMessage.Content.ReadAsStringAsync(ct);
73-
74-
return new(content);
83+
var url = await responseMessage.Content.ReadAsStringAsync(ct);
84+
85+
bool ok = true;
86+
87+
if (Paranoid) {
88+
var r2 = await url.GetAsync(ct);
89+
90+
if (r2.Headers.TryGetFirst("Content-Length", out var cls)) {
91+
if (int.Parse(cls) == 0) {
92+
ok = false;
93+
}
94+
}
95+
96+
}
97+
98+
return new()
99+
{
100+
Url = url,
101+
Response = response,
102+
IsValid = ok
103+
};
75104
}
76105

77106
protected BaseCatboxEngine(string s) : base(s) { }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Read S SmartImage.Lib BaseUploadResponse.cs
2+
// 2023-05-28 @ 7:49 PM
3+
4+
using Flurl.Http;
5+
6+
namespace SmartImage.Lib.Engines.Impl.Upload;
7+
8+
public sealed class BaseUploadResponse : IDisposable
9+
{
10+
public Url Url { get; init; }
11+
12+
public IFlurlResponse Response { get; init; }
13+
14+
public bool IsValid { get; init; }
15+
16+
public void Dispose()
17+
{
18+
Response.Dispose();
19+
}
20+
}

SmartImage.Lib 3/Engines/Impl/Upload/CatboxEngine.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ namespace SmartImage.Lib.Engines.Impl.Upload;
44

55
public sealed class CatboxEngine : BaseCatboxEngine
66
{
7+
78
public override string Name => "Catbox";
89

910
public override int MaxSize => 1 * 1000 * 1000 * 200;
1011

11-
public CatboxEngine() : base("https://catbox.moe/user/api.php") { }
12+
public CatboxEngine() : base("https://catbox.moe/user/api.php")
13+
{
14+
Paranoid = true;
15+
}
16+
1217
}

SmartImage.Lib 3/SearchClient.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,7 @@ public BaseSearchEngine[] LoadEngines()
229229
public static ValueTask<IReadOnlyList<SearchResultItem>> Filter(IEnumerable<SearchResultItem> sri)
230230
{
231231
var sri2 = sri.AsParallel().DistinctBy(e => e.Url).ToList();
232-
233-
/*Parallel.ForEachAsync(sri2, async (item, token) =>
234-
{
235-
var r = await item.Url.AllowAnyHttpStatus().GetAsync();
236-
237-
if (r.ResponseMessage.IsSuccessStatusCode) { }
238-
});*/
239-
232+
240233
return ValueTask.FromResult<IReadOnlyList<SearchResultItem>>(sri2);
241234
}
242235

@@ -263,6 +256,7 @@ public static IReadOnlyList<SearchResultItem> Optimize(IEnumerable<SearchResultI
263256

264257
public static async Task<IReadOnlyList<UniSource>> GetDirectImagesAsync(IEnumerable<SearchResultItem> sri)
265258
{
259+
//
266260
var filter = Optimize(sri)
267261
.DistinctBy(r => r.Url)
268262
// .Where(r => r.Score >= SearchResultItem.SCORE_THRESHOLD) // probably can be removed/reduced

SmartImage.Lib 3/SearchQuery.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ internal SearchQuery([MN] UniSource f)
3333
{
3434
Uni = f;
3535
}
36-
36+
3737
public static readonly SearchQuery Null = new(null);
3838

3939
static SearchQuery() { }
@@ -59,9 +59,19 @@ public async Task<Url> UploadAsync(BaseUploadEngine engine = null, CancellationT
5959
else {
6060
engine ??= BaseUploadEngine.Default;
6161

62+
retry:
6263
var u = await engine.UploadFileAsync(Uni.Value.ToString(), ct);
63-
Upload = u;
64+
65+
if (!u.IsValid) {
66+
engine = BaseUploadEngine.All[Array.IndexOf(BaseUploadEngine.All, engine) + 1];
67+
Debug.WriteLine($"{u.Response.ResponseMessage} failed, retrying with {engine.Name}");
68+
u.Dispose();
69+
goto retry;
70+
}
71+
72+
Upload = u.Url;
6473
Size = engine.Size;
74+
u.Dispose();
6575
}
6676

6777
return Upload;
@@ -100,7 +110,7 @@ public void Dispose()
100110
if (OperatingSystem.IsWindows()) {
101111
Image?.Dispose();
102112
}
103-
113+
104114
Uni?.Dispose();
105115
}
106116

0 commit comments

Comments
 (0)