Example #1
0
        private async Task AddCachedImageAsync(WeissSchwarzCard card)
        {
            try
            {
                var imgURL = card.Images.Last();
                Log.Information("Caching: {imgURL}", imgURL);
                using (System.IO.Stream netStream = await card.GetImageStreamAsync())
                    using (Image img = Image.Load(netStream))
                    {
                        var imageDirectoryPath = Path.Get(_IMAGE_CACHE_PATH);
                        if (!imageDirectoryPath.Exists)
                        {
                            imageDirectoryPath.CreateDirectory();
                        }

                        img.Metadata.ExifProfile ??= new ExifProfile();
                        img.Metadata.ExifProfile.SetValue(SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifTag.Copyright, card.Images.Last().Authority);
                        var savePath = Path.Get(_IMAGE_CACHE_PATH).Combine($"{card.Serial.Replace('-', '_').AsFileNameFriendly()}.jpg");
                        savePath.Open(img.SaveAsJpeg);
                    }
            } catch (InvalidOperationException e) when(e.Message == "Sequence contains no elements")
            {
                Log.Warning("Cannot be cached as no image URLs were found: {serial}", card.Serial);
            }
        }
Example #2
0
 private async Task AddCachedImageAsync(WeissSchwarzCard card, Func <Flurl.Url, CookieSession> _cookieSession, CancellationToken ct = default)
 {
     try
     {
         var imgURL = card.Images.Last();
         Log.Information("Caching: {imgURL}", imgURL);
         var session = _cookieSession(imgURL);
         using (System.IO.Stream netStream = await card.GetImageStreamAsync(session, ct))
             using (Image img = Image.Load(netStream))
             {
                 var imageDirectoryPath = Path.Get(_IMAGE_CACHE_PATH);
                 if (!imageDirectoryPath.Exists)
                 {
                     imageDirectoryPath.CreateDirectory();
                 }
                 if (img.Height < img.Width)
                 {
                     Log.Debug("Image is probably incorrectly oriented, rotating it 90 degs. clockwise to compensate.");
                     img.Mutate(ipc => ipc.Rotate(90));
                 }
                 img.Metadata.ExifProfile ??= new ExifProfile();
                 img.Metadata.ExifProfile.SetValue(SixLabors.ImageSharp.Metadata.Profiles.Exif.ExifTag.Copyright, card.Images.Last().Authority);
                 var savePath = Path.Get(_IMAGE_CACHE_PATH).Combine($"{card.Serial.Replace('-', '_').AsFileNameFriendly()}.jpg");
                 await img.SaveAsPngAsync(savePath.FullPath, ct);
             }
     } catch (InvalidOperationException e) when(e.Message == "Sequence contains no elements")
     {
         Log.Warning("Cannot be cached as no image URLs were found: {serial}", card.Serial);
     }
 }