1313using AngleSharp . Html . Dom ;
1414using JetBrains . Annotations ;
1515using Kantan . Net ;
16+ using Kantan . Utilities ;
1617using Novus . Win32 ;
1718using RestSharp ;
1819using static Kantan . Diagnostics . LogCategories ;
@@ -82,7 +83,7 @@ public static Dictionary<string, string> UtilitiesMap
8283 [ CanBeNull ]
8384 public static string Download ( Uri src , string path )
8485 {
85- var filename = NormalizeFilename ( src ) ;
86+ var filename = UriUtilities . NormalizeFilename ( src ) ;
8687
8788 string combine = Path . Combine ( path , filename ) ;
8889
@@ -105,38 +106,7 @@ public static string Download(Uri src, string path)
105106 }
106107 }
107108
108- private static string NormalizeFilename ( Uri src )
109- {
110- string filename = Path . GetFileName ( src . AbsolutePath ) ;
111-
112- if ( ! Path . HasExtension ( filename ) ) {
113-
114- // If no format is specified/found, just append a jpg extension
115- string ext = ".jpg" ;
116-
117- // For Pixiv (?)
118- var kv = HttpUtility . ParseQueryString ( src . Query ) ;
119-
120- var t = kv [ "format" ] ;
121-
122- if ( t != null ) {
123- ext = $ ".{ t } ";
124- }
125-
126- filename += ext ;
127-
128- }
129-
130- // Stupid URI parameter Twitter appends to filenames
131-
132- var i = filename . IndexOf ( ":large" , StringComparison . Ordinal ) ;
133-
134- if ( i != - 1 ) {
135- filename = filename [ ..i ] ;
136- }
137-
138- return filename ;
139- }
109+
140110
141111 /// <summary>
142112 /// Scans for direct images within a webpage.
@@ -146,7 +116,6 @@ private static string NormalizeFilename(Uri src)
146116 /// <param name="timeoutMS"></param>
147117 public static async Task < List < string > > ScanForImages ( string url , int count = 10 , long timeoutMS = TimeoutMS )
148118 {
149-
150119 var images = new List < string > ( ) ;
151120
152121 IHtmlDocument document ;
@@ -195,14 +164,7 @@ public static async Task<List<string>> ScanForImages(string url, int count = 10,
195164 count -- ;
196165 }
197166 }
198-
199- /*for (int i = 0; i < images.Count; i++) {
200- for (int j = i + 1; j < images.Count; j++) {
201- if (UrlUtilities.UrlEqual(images[i], images[j])) {
202- Debug.WriteLine($"{images[i]} = {images[j]}");
203- }
204- }
205- }*/
167+
206168
207169 return images ;
208170 }
@@ -229,10 +191,24 @@ public static bool IsImage(string url, long timeout, DirectImageCriterion direct
229191 return false ;
230192 }
231193
232- var a = response . ContentType . StartsWith ( "image" ) && response . ContentType != "image/svg+xml" ;
233- var b = response . ContentLength >= 50_000 ;
194+ /* Check content-type */
234195
235- return a && b ;
196+ // The content-type returned from the response may not be the actual content-type, so
197+ // we'll resolve it using binary data instead to be sure
198+
199+ var stream = WebUtilities . GetStream ( url ) ;
200+ var buffer = new byte [ 256 ] ;
201+ stream . Read ( buffer , 0 , buffer . Length ) ;
202+ // var rg = response.RawBytes;
203+ var m = MediaTypes . ResolveFromData ( buffer ) ;
204+
205+ // var a = response.ContentType.StartsWith("image") && response.ContentType != "image/svg+xml";
206+ // var b = response.ContentLength >= 50_000;
207+
208+ var a = m . StartsWith ( "image" ) && m != "image/svg+xml" ;
209+ // var b = stream.Length >= 50_000;
210+
211+ return a ;
236212 default :
237213 throw new ArgumentOutOfRangeException ( nameof ( directCriterion ) , directCriterion , null ) ;
238214 }
@@ -291,6 +267,9 @@ public static DisplayResolutionType GetDisplayResolution(int w, int h)
291267 public static Bitmap ResizeImage ( Bitmap mg , Size newSize )
292268 {
293269 // todo
270+ /*
271+ * Adapted from https://stackoverflow.com/questions/5243203/how-to-compress-jpg-image
272+ */
294273 double ratio = 0d ;
295274 double myThumbWidth = 0d ;
296275 double myThumbHeight = 0d ;
0 commit comments