|
5 | 5 | using System.Collections.Generic; |
6 | 6 | using System.Diagnostics; |
7 | 7 | using System.Drawing; |
| 8 | +using System.IO; |
8 | 9 | using System.Linq; |
| 10 | +using System.Net.Mime; |
9 | 11 | using System.Reflection; |
10 | 12 | using Novus.Win32; |
| 13 | +using SimpleCore.Model; |
11 | 14 | using SimpleCore.Net; |
12 | 15 |
|
13 | 16 | #nullable enable |
@@ -180,34 +183,68 @@ public void UpdateFrom(ImageResult result) |
180 | 183 | Description = result.Description; |
181 | 184 | Date = result.Date; |
182 | 185 |
|
183 | | - if (result.Direct is { }) { |
184 | | - var stream = WebUtilities.GetStream(result.Direct.ToString()); |
185 | | - var image = Image.FromStream(stream); |
186 | | - |
187 | | - |
188 | | - Width = image.Width; |
189 | | - Height = image.Height; |
190 | | - |
191 | | - OtherMetadata.Add("Mime", MediaTypes.ResolveFromData(stream)); |
192 | | - } |
193 | 186 |
|
| 187 | + ReadDirectImageData(); |
194 | 188 |
|
195 | 189 | } |
196 | 190 |
|
197 | 191 | public async void FindDirectImagesAsync() |
198 | 192 | { |
199 | 193 | if (Url is not null) { |
200 | | - var directImages = await ImageHelper.FindDirectImagesAsync(Url?.ToString()); |
201 | 194 |
|
202 | | - if (directImages is { }) { |
203 | | - string? images = directImages.FirstOrDefault(); |
| 195 | + if (ImageHelper.IsDirect(Url.ToString())) { |
| 196 | + Direct = Url; |
| 197 | + |
| 198 | + } |
| 199 | + else { |
| 200 | + try { |
| 201 | + |
| 202 | + var directImages = await ImageHelper.FindDirectImagesAsync(Url.ToString()); |
| 203 | + |
| 204 | + if (directImages is { }) { |
| 205 | + string? images = directImages.FirstOrDefault(); |
| 206 | + |
| 207 | + if (images is { }) { |
| 208 | + var uri = new Uri(images); |
| 209 | + Direct = uri; |
| 210 | + Debug.WriteLine($"{Url} -> {Direct}"); |
| 211 | + } |
| 212 | + } |
| 213 | + } |
| 214 | + catch (Exception e) { |
| 215 | + Debug.WriteLine(e); |
204 | 216 |
|
205 | | - if (images is { }) { |
206 | | - var uri = new Uri(images); |
207 | | - Direct = uri; |
208 | | - Debug.WriteLine($"{Url} -> {Direct}"); |
209 | 217 | } |
210 | 218 | } |
| 219 | + |
| 220 | + |
| 221 | + try { |
| 222 | + ReadDirectImageData(); |
| 223 | + } |
| 224 | + catch (Exception) { |
| 225 | + // ignored |
| 226 | + } |
| 227 | + |
| 228 | + } |
| 229 | + } |
| 230 | + |
| 231 | + private void ReadDirectImageData() |
| 232 | + { |
| 233 | + if (Direct is { }) { |
| 234 | + var stream = WebUtilities.GetStream(Direct.ToString()); |
| 235 | + var image = Image.FromStream(stream); |
| 236 | + |
| 237 | + |
| 238 | + Width = image.Width; |
| 239 | + Height = image.Height; |
| 240 | + |
| 241 | + stream.Position = 0; |
| 242 | + using var ms = new MemoryStream(); |
| 243 | + stream.CopyTo(ms); |
| 244 | + var rg = ms.ToArray(); |
| 245 | + |
| 246 | + OtherMetadata.Add("Size", MathHelper.ConvertToUnit(rg.Length)); |
| 247 | + OtherMetadata.Add("Mime", MediaTypes.ResolveFromData(stream)); |
211 | 248 | } |
212 | 249 | } |
213 | 250 |
|
|
0 commit comments