Skip to content

Commit fbf47df

Browse files
committed
Rdx 1.0.6; fixes
1 parent 5215b7d commit fbf47df

23 files changed

+268
-285
lines changed

SmartImage.Lib 3/UniImage.cs renamed to SmartImage.Lib 3/Images/BinaryImageFile.cs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
// Author: Deci | Project: SmartImage.Lib | Name: UniImage.cs
1+
// Author: Deci | Project: SmartImage.Lib | Name: BinaryImageFile.cs
22
// Date: 2024/05/02 @ 10:05:55
33

44
using System.Diagnostics;
55
using System.Net;
66
using Flurl.Http;
77
using JetBrains.Annotations;
88
using Novus.FileTypes;
9+
using Novus.FileTypes.Uni;
910
using Novus.Streams;
1011
using Novus.Win32;
1112
using SixLabors.ImageSharp;
@@ -14,9 +15,12 @@
1415
using SixLabors.ImageSharp.Processing;
1516
using SmartImage.Lib.Model;
1617

17-
namespace SmartImage.Lib;
18+
namespace SmartImage.Lib.Images;
1819

19-
public enum UniImageType
20+
/// <summary>
21+
/// <seealso cref="Novus.FileTypes.Uni.UniSourceType"/>
22+
/// </summary>
23+
public enum BinaryImageFileSource
2024
{
2125

2226
Unknown = 0,
@@ -26,14 +30,17 @@ public enum UniImageType
2630

2731
}
2832

29-
public class UniImage : IItemSize, IDisposable, IAsyncDisposable, IEquatable<UniImage>
33+
/// <summary>
34+
/// <seealso cref="Novus.FileTypes.Uni.UniSource"/>
35+
/// </summary>
36+
public class BinaryImageFile : IItemSize, IDisposable, IAsyncDisposable, IEquatable<BinaryImageFile>
3037
{
3138

3239
public Stream Stream { get; internal init; }
3340

3441
public object Value { get; internal init; }
3542

36-
public UniImageType Type { get; internal init; }
43+
public BinaryImageFileSource Type { get; internal init; }
3744

3845
public long Size
3946
{
@@ -65,44 +72,48 @@ public long Size
6572
[MNNW(true, nameof(Info))]
6673
public bool HasInfo => Info != null;
6774

68-
public bool IsUri => Type == UniImageType.Uri;
75+
public bool IsUri => Type == BinaryImageFileSource.Uri;
6976

70-
public bool IsFile => Type == UniImageType.File;
77+
public bool IsFile => Type == BinaryImageFileSource.File;
7178

72-
public bool IsStream => Type == UniImageType.Stream;
79+
public bool IsStream => Type == BinaryImageFileSource.Stream;
7380

74-
public static readonly UniImage Null = new();
81+
public static readonly BinaryImageFile Null = new();
7582

76-
internal UniImage(object value, Stream stream, UniImageType type)
83+
internal BinaryImageFile(object value, Stream stream, BinaryImageFileSource type, IImageFormat format)
7784
{
7885
Stream = stream;
7986
Value = value;
8087
Type = type;
88+
Info = format;
8189
}
8290

83-
private UniImage() : this(null, Stream.Null, UniImageType.Unknown) { }
91+
internal BinaryImageFile(object value, Stream stream, BinaryImageFileSource type)
92+
: this(value, stream, type, null) { }
8493

85-
public static async Task<UniImage> TryCreateAsync(object o, CancellationToken t = default)
94+
private BinaryImageFile() : this(null, Stream.Null, BinaryImageFileSource.Unknown) { }
95+
96+
public static async Task<BinaryImageFile> TryCreateAsync(object o, CancellationToken t = default)
8697
{
87-
Stream str;
88-
IImageFormat fmt;
89-
UniImageType qt;
90-
string s = null;
98+
Stream str;
99+
IImageFormat fmt;
100+
BinaryImageFileSource qt;
101+
string s = null;
91102

92103
if (IsFileType(o, out var fi)) {
93104
// var s = ((FileInfo) fi).FullName;
94105
s = (string) o;
95106
str = File.OpenRead(s);
96-
qt = UniImageType.File;
107+
qt = BinaryImageFileSource.File;
97108
}
98109
else if (IsUriType(o, out var url2)) {
99110
var res = await HandleUriAsync(url2, t);
100111
str = await res.GetStreamAsync();
101-
qt = UniImageType.Uri;
112+
qt = BinaryImageFileSource.Uri;
102113
}
103114
else if (o is Stream) {
104115
str = (Stream) o;
105-
qt = UniImageType.Stream;
116+
qt = BinaryImageFileSource.Stream;
106117
}
107118
else {
108119
return Null;
@@ -114,9 +125,8 @@ public static async Task<UniImage> TryCreateAsync(object o, CancellationToken t
114125

115126
str.TrySeek();
116127

117-
var query = new UniImage(o, str, qt)
128+
var query = new BinaryImageFile(o, str, qt, fmt)
118129
{
119-
Info = fmt,
120130
FilePath = s
121131
};
122132

@@ -131,7 +141,7 @@ public static async Task<IFlurlResponse> HandleUriAsync(Url value, CancellationT
131141
.WithHeaders(new
132142
{
133143
// todo
134-
User_Agent = Resources.UserAgent1,
144+
User_Agent = R1.UserAgent1,
135145
})
136146
.GetAsync(cancellationToken: ct);
137147

@@ -256,7 +266,7 @@ public string WriteToFile(string fn = null)
256266
string t;
257267
fn ??= Path.GetTempFileName();
258268

259-
if (Type != UniImageType.File) {
269+
if (Type != BinaryImageFileSource.File) {
260270
t = Path.Combine(Path.GetTempPath(), fn);
261271

262272
using FileStream fs = File.Create(t);
@@ -303,7 +313,7 @@ public override string ToString()
303313

304314
#region Equality members
305315

306-
public bool Equals(UniImage other)
316+
public bool Equals(BinaryImageFile other)
307317
{
308318
if (ReferenceEquals(null, other)) return false;
309319
if (ReferenceEquals(this, other)) return true;
@@ -313,7 +323,7 @@ public bool Equals(UniImage other)
313323

314324
public override bool Equals(object obj)
315325
{
316-
return ReferenceEquals(this, obj) || (obj is UniImage other && Equals(other));
326+
return ReferenceEquals(this, obj) || obj is BinaryImageFile other && Equals(other);
317327
}
318328

319329
public override int GetHashCode()
@@ -324,12 +334,12 @@ public override int GetHashCode()
324334
// return Uni.GetHashCode();
325335
}
326336

327-
public static bool operator ==(UniImage left, UniImage right)
337+
public static bool operator ==(BinaryImageFile left, BinaryImageFile right)
328338
{
329339
return Equals(left, right);
330340
}
331341

332-
public static bool operator !=(UniImage left, UniImage right)
342+
public static bool operator !=(BinaryImageFile left, BinaryImageFile right)
333343
{
334344
return !Equals(left, right);
335345
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Author: Deci | Project: SmartImage.Lib | Name: GenericImageFilter.cs
2+
// Date: 2024/05/02 @ 11:05:10
3+
4+
using System.Diagnostics;
5+
6+
namespace SmartImage.Lib.Images;
7+
8+
public class GenericImageFilter : IImageFilter
9+
{
10+
// TODO
11+
12+
public string[] Blacklist
13+
=>
14+
[
15+
"thumbnail", "avatar", "error", "logo"
16+
];
17+
18+
public bool Predicate(BinaryImageFile us)
19+
{
20+
try
21+
{
22+
if (us.Stream.Length <= 25_000 || us.Info.DefaultMimeType == null)
23+
{
24+
return false;
25+
}
26+
27+
return true;
28+
}
29+
catch (Exception e)
30+
{
31+
Debug.WriteLine($"{e.Message}", nameof(Predicate));
32+
return true;
33+
}
34+
}
35+
36+
public bool Refine(string b)
37+
{
38+
if (!Url.IsValid(b))
39+
{
40+
return false;
41+
}
42+
43+
var u = Url.Parse(b);
44+
var ps = u.PathSegments;
45+
46+
if (ps.Any())
47+
{
48+
return !Blacklist.Any(i => ps.Any(p => p.Contains(i, StringComparison.InvariantCultureIgnoreCase)));
49+
}
50+
51+
return true;
52+
}
53+
54+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Author: Deci | Project: SmartImage.Lib | Name: IImageFilter.cs
2+
// Date: 2024/05/02 @ 11:05:28
3+
4+
namespace SmartImage.Lib.Images;
5+
// todo
6+
7+
public interface IImageFilter
8+
{
9+
10+
public string[] Blacklist { get; }
11+
12+
public bool Refine(string b);
13+
14+
public bool Predicate(BinaryImageFile us);
15+
16+
}

SmartImage.Lib 3/Utilities/ImageScanner.cs renamed to SmartImage.Lib 3/Images/ImageScanner.cs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,33 @@
33

44
using System.Collections.Concurrent;
55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using AngleSharp.Html.Parser;
78
using Flurl.Http;
89
using Kantan.Net.Utilities;
910
using Novus.FileTypes;
1011
using Novus.FileTypes.Uni;
1112
using Novus.OS;
1213
using Novus.Utilities;
13-
using SmartImage.Lib.Model;
14+
using SixLabors.ImageSharp;
15+
using SixLabors.ImageSharp.PixelFormats;
16+
using SmartImage.Lib.Results;
17+
using SmartImage.Lib.Utilities;
1418

15-
namespace SmartImage.Lib.Utilities;
19+
namespace SmartImage.Lib.Images;
1620

1721
public static class ImageScanner
1822
{
1923

2024
/*public static readonly BaseImageHost[] All =
2125
ReflectionHelper.CreateAllInAssembly<BaseImageHost>(InheritanceProperties.Subclass).ToArray();*/
2226

23-
public static async Task<UniImage[]> ScanAsync(Url u, IImageFilter filter = null,
24-
CancellationToken ct = default)
27+
/// <summary>
28+
/// Scans for images within the webpage located at <paramref name="u"/>; if <paramref name="u"/> itself
29+
/// points to binary image data, it is returned.
30+
/// </summary>
31+
public static async Task<BinaryImageFile[]> ScanImagesAsync(Url u, IImageFilter filter = null,
32+
CancellationToken ct = default)
2533
{
2634
IFlurlResponse res;
2735
Stream stream;
@@ -49,9 +57,9 @@ public static async Task<UniImage[]> ScanAsync(Url u, IImageFilter filter = null
4957
return [];
5058
}
5159

52-
var ul = new ConcurrentBag<UniImage>();
60+
var ul = new ConcurrentBag<BinaryImageFile>();
5361
stream = await res.GetStreamAsync();
54-
var uf = await UniImage.TryCreateAsync(stream, t: ct);
62+
var uf = await BinaryImageFile.TryCreateAsync(stream, t: ct);
5563

5664
if (uf != null) {
5765
/*if (!FileType.Image.Contains(uf.FileType)) {
@@ -90,14 +98,14 @@ public static async Task<UniImage[]> ScanAsync(Url u, IImageFilter filter = null
9098

9199
await Parallel.ForEachAsync(c, po, async (s, token) =>
92100
{
93-
var ux = await UniImage.TryCreateAsync(s, t: token);
101+
var ux = await BinaryImageFile.TryCreateAsync(s, t: token);
94102

95103
if (ux != null) {
96104
/*if (!FileType.Image.Contains(ux.FileType)) {
97105
ux?.Dispose();
98106
return;
99107
}*/
100-
// Debug.WriteLine($"Found {ux.Value} for {u}", nameof(ScanAsync));
108+
// Debug.WriteLine($"Found {ux.Value} for {u}", nameof(ScanForEmbeddedImagesAsync));
101109
if (filter.Predicate(ux)) {
102110
ul.Add(ux);
103111

@@ -133,7 +141,8 @@ public static bool UniSourcePredicate(UniSource us)
133141
}
134142
}
135143

136-
public static async Task<UniImage[]> RunGalleryAsync(Url cri, CancellationToken ct = default)
144+
// todo
145+
public static async Task<BinaryImageFile[]> RunGalleryAsync(Url cri, CancellationToken ct = default)
137146
{
138147
using var p = Process.Start(new ProcessStartInfo("gallery-dl", $"-G {cri}")
139148
{
@@ -144,11 +153,11 @@ public static async Task<UniImage[]> RunGalleryAsync(Url cri, CancellationToken
144153
await p.WaitForExitAsync(ct);
145154
var s = await p.StandardOutput.ReadToEndAsync(ct);
146155
var s2 = s.Split(Environment.NewLine);
147-
var rg = new ConcurrentBag<UniImage>();
156+
var rg = new ConcurrentBag<BinaryImageFile>();
148157

149158
await Parallel.ForEachAsync(s2, ct, async (s1, token) =>
150159
{
151-
var uni = await UniImage.TryCreateAsync(s1, t: token);
160+
var uni = await BinaryImageFile.TryCreateAsync(s1, t: token);
152161

153162
if (uni != null) {
154163
rg.Add(uni);

0 commit comments

Comments
 (0)