Skip to content

Commit ed8cd31

Browse files
committed
Work on refactoring
1 parent 0b6b07f commit ed8cd31

9 files changed

Lines changed: 113 additions & 137 deletions

File tree

SmartImage.Lib/Engines/BaseSearchEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using SmartImage.Lib.Utilities.Diagnostics;
1414
using SmartImage.Lib.Model;
1515
using SmartImage.Lib.Utilities;
16+
using SmartImage.Shared;
1617

1718
[assembly: InternalsVisibleTo(Common.PROJ_SMARTIMAGE_TEST)]
1819
[assembly: InternalsVisibleTo(Common.PROJ_SMARTIMAGE_UI2)]

SmartImage.Lib/Engines/Results/SearchResultItem.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using SmartImage.Lib.Images;
77
using SmartImage.Lib.Images.Uni;
88
using SmartImage.Lib.Model;
9+
using SmartImage.Lib.Utilities;
910

1011
namespace SmartImage.Lib.Engines.Results;
1112

@@ -14,13 +15,8 @@ namespace SmartImage.Lib.Engines.Results;
1415
public class ScannedResultItem : UniImageUrl, IResultItem
1516
{
1617

17-
18-
}
18+
internal ScannedResultItem(Url url) : base(url) { }
1919

20-
public async ValueTask<(bool AllocSourceOk, bool AllocImageOk)> AllocAll(CancellationToken ct)
21-
{
22-
throw new NotImplementedException();
23-
}
2420

2521
}
2622

@@ -47,9 +43,11 @@ public class SearchResultItem : IResultItem, IComparable<SearchResultItem>, ICom
4743

4844
#endregion
4945

46+
private static readonly ILogger s_logger = AppSupport.Factory.CreateLogger(nameof(SearchResultItem));
47+
5048
[MN]
5149
[JPN("url")]
52-
public Url Url { get; protected set; }
50+
public Url Url { get; internal set; }
5351

5452
/// <summary>
5553
/// Title/caption of this result
@@ -66,12 +64,12 @@ public class SearchResultItem : IResultItem, IComparable<SearchResultItem>, ICom
6664
/// <summary>
6765
/// Image width
6866
/// </summary>
69-
public int? Width { get; internal set; }
67+
public int? Width { get; set; }
7068

7169
/// <summary>
7270
/// Image height
7371
/// </summary>
74-
public int? Height { get; internal set; }
72+
public int? Height { get; set; }
7573

7674
[MNNW(true, nameof(Width), nameof(Height))]
7775
public bool HasDimensions => Width.HasValue && Height.HasValue;
@@ -342,7 +340,7 @@ public void Dispose()
342340
s_logger.LogDebug("Disposing {Item} of {Name}", Url, Root.Engine.Name);
343341
ThumbnailImage?.Dispose();
344342

345-
foreach (SearchResultItem item in ScannedItems) {
343+
foreach (IResultItem item in ScannedItems) {
346344
item.Dispose();
347345
}
348346

SmartImage.Lib/Images/Uni/IUniImage.cs

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace SmartImage.Lib.Images.Uni;
99

10-
public interface IUniImage : IImage, IDisposable, ILength, IUrl
10+
public interface IUniImage : IImage, IDisposable, ILength
1111
{
1212

1313
byte[] Bytes { get; }
@@ -30,10 +30,7 @@ public interface IUniImage : IImage, IDisposable, ILength, IUrl
3030
}
3131

3232
[MURV]
33-
Stream GetStream()
34-
{
35-
return UniImage.MemMgr.GetStream(Bytes.GetHashCode().ToString(), Bytes);
36-
}
33+
Stream GetStream();
3734

3835
/// <summary>
3936
/// Allocates <see cref="Bytes"/>
@@ -45,35 +42,7 @@ Stream GetStream()
4542
/// Allocates <see cref="IImage.Image"/> from <see cref="Bytes"/>
4643
/// </summary>
4744
[MNNW(true, nameof(Image))]
48-
async ValueTask<bool> AllocImageAsync(CancellationToken ct = default)
49-
{
50-
if (this.Url == null) {
51-
return false;
52-
}
53-
54-
if (HasImage) {
55-
return true;
56-
}
57-
58-
bool allocImgOk = false;
59-
var allocOk = await AllocSourceAsync(ct);
60-
61-
if (allocOk) {
62-
//todo?
63-
allocImgOk = await AllocImageAsync(ct);
64-
}
65-
66-
if (allocImgOk) {
67-
68-
Width ??= Image.Width;
69-
Height ??= Image.Height;
70-
71-
// Root.Results.Add(this);
72-
}
73-
else { }
74-
75-
return HasImage;
76-
}
45+
ValueTask<bool> AllocImageAsync(CancellationToken ct = default);
7746

7847

7948
/// <returns><see cref="AllocSourceAsync"/>, <see cref="AllocImageAsync"/></returns>

SmartImage.Lib/Images/Uni/UniImage.cs

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
// Date: 2024/05/02 @ 10:05:55
33

44

5-
using System.ComponentModel;
5+
using AngleSharp.Css.Values;
6+
using CoenM.ImageHash;
67
using Microsoft.Extensions.Logging;
8+
using Microsoft.IO;
79
using Novus.FileTypes.Uni;
810
using Novus.Streams;
911
using SixLabors.ImageSharp;
1012
using SixLabors.ImageSharp.Formats;
11-
using CoenM.ImageHash;
12-
using Microsoft.IO;
1313
using SmartImage.Lib.Model;
1414
using SmartImage.Lib.Utilities;
15+
using System.ComponentModel;
16+
using System.Drawing.Imaging;
17+
1518

1619
// ReSharper disable InconsistentNaming
1720

@@ -41,8 +44,6 @@ public enum UniImageType
4144
public abstract class UniImage : IUniImage, IEquatable<UniImage>
4245
{
4346

44-
public abstract Url Url { get; set; }
45-
4647
protected static readonly ILogger s_logger;
4748

4849
static UniImage()
@@ -87,6 +88,14 @@ protected set
8788
}
8889
}
8990

91+
public IImageFormat ImageFormat => Image?.Metadata.DecodedImageFormat;
92+
93+
[MNNW(true, nameof(ImageFormat), nameof(Image))]
94+
public bool HasImageFormat => ImageFormat != null;
95+
96+
[MNNW(true, nameof(Image), nameof(ImageFormat))]
97+
public bool HasImage => Image != null;
98+
9099
#endregion
91100

92101
#region
@@ -133,6 +142,11 @@ protected set
133142
}
134143
}
135144

145+
[MNNW(true, nameof(Bytes), nameof(IUniImage.Length))]
146+
public bool HasBytes => Bytes != null;
147+
148+
public long? Length => Bytes?.Length;
149+
136150
#endregion
137151

138152

@@ -145,6 +159,12 @@ private protected UniImage(string value, UniImageType type)
145159

146160
#region
147161

162+
[MURV]
163+
public Stream GetStream()
164+
{
165+
return UniImage.MemMgr.GetStream(Name, Bytes);
166+
}
167+
148168
/// <summary>
149169
/// Allocates <see cref="Bytes"/>
150170
/// </summary>
@@ -193,8 +213,7 @@ public virtual async ValueTask<bool> AllocImageAsync(CancellationToken ct = defa
193213
/// <summary>
194214
/// Attempts to create the appropriate <see cref="UniImage" /> for <paramref name="o" />.
195215
/// </summary>
196-
public static async Task<UniImage> TryCreateAsync(object o, bool autoInit = true, bool autoDisposeOnError = true,
197-
CancellationToken ct = default)
216+
public static async Task<UniImage> TryCreateAsync(object o, bool autoInit = true, bool autoDisposeOnError = true, CancellationToken ct = default)
198217
{
199218
UniImage ui = null;
200219

@@ -271,7 +290,7 @@ public bool TryDeleteFile()
271290
[MURV]
272291
public virtual string WriteImageToFile([CBN] string fn = null)
273292
{
274-
if (IImage.HasImage) {
293+
if (HasImage) {
275294
throw new InvalidOperationException();
276295
}
277296

SmartImage.Lib/Images/Uni/UniImageFile.cs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,6 @@ internal UniImageFile(FileInfo fi) : base(fi.FullName, UniImageType.File)
1818

1919
public override string Name => LocalFileInfo.Name;
2020

21-
22-
public override Url Url
23-
{
24-
get => new Url(Name);
25-
set => throw new NotImplementedException();
26-
}
27-
2821
public override async ValueTask<bool> AllocSourceAsync(CancellationToken ct = default)
2922
{
3023
if (HasBytes) {

0 commit comments

Comments
 (0)