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
44using System . Diagnostics ;
55using System . Net ;
66using Flurl . Http ;
77using JetBrains . Annotations ;
88using Novus . FileTypes ;
9+ using Novus . FileTypes . Uni ;
910using Novus . Streams ;
1011using Novus . Win32 ;
1112using SixLabors . ImageSharp ;
1415using SixLabors . ImageSharp . Processing ;
1516using 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 }
0 commit comments