Fix bug where book with corrupt image cannot be queued.

This commit is contained in:
MBucari 2023-03-11 20:40:02 -07:00
parent 245e55782e
commit 5ec01913d5
11 changed files with 58 additions and 67 deletions

View File

@ -1,6 +1,8 @@
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Media.Imaging;
using LibationAvalonia.Dialogs;
using LibationFileManager;
using System.Threading.Tasks;
namespace LibationAvalonia
@ -20,5 +22,21 @@ namespace LibationAvalonia
=> dialogWindow.ShowDialog<DialogResult>(owner ?? App.MainWindow);
public static Window GetParentWindow(this IControl control) => control.VisualRoot as Window;
private static Bitmap defaultImage;
public static Bitmap TryLoadImageOrDefault(byte[] picture, PictureSize defaultSize = PictureSize.Native)
{
try
{
using var ms = new System.IO.MemoryStream(picture);
return new Bitmap(ms);
}
catch
{
using var ms = new System.IO.MemoryStream(PictureStorage.GetDefaultImage(defaultSize));
return defaultImage ??= new Bitmap(ms);
}
}
}
}

View File

@ -1,5 +1,4 @@
using ApplicationServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
@ -10,7 +9,6 @@ using LibationAvalonia.ViewModels;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System;
namespace LibationAvalonia.Dialogs
{
@ -112,8 +110,7 @@ namespace LibationAvalonia.Dialogs
//init cover image
var picture = PictureStorage.GetPictureSynchronously(new PictureDefinition(libraryBook.Book.PictureId, PictureSize._80x80));
using var ms = new System.IO.MemoryStream(picture);
Cover = new Bitmap(ms);
Cover = AvaloniaUtils.TryLoadImageOrDefault(picture, PictureSize._80x80);
//init book details
DetailsText = @$"

View File

@ -2,7 +2,6 @@ using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using System;
using System.ComponentModel;
using System.IO;
using ReactiveUI;
using Avalonia.Platform.Storage;
@ -29,17 +28,7 @@ namespace LibationAvalonia.Dialogs
public void SetCoverBytes(byte[] cover)
{
try
{
var ms = new MemoryStream(cover);
_bitmapHolder.CoverImage = new Bitmap(ms);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Error loading cover art for {file}", PictureFileName);
using var ms = App.OpenAsset("img-coverart-prod-unavailable_500x500.jpg");
_bitmapHolder.CoverImage = new Bitmap(ms);
}
_bitmapHolder.CoverImage = AvaloniaUtils.TryLoadImageOrDefault(cover);
}
public async void SaveImage_Clicked(object sender, Avalonia.Interactivity.RoutedEventArgs e)

View File

@ -8,28 +8,17 @@ namespace LibationAvalonia.ViewModels
{
public class AvaloniaEntryStatus : EntryStatus, IEntryStatus, IComparable
{
private static Bitmap _defaultImage;
public override IBrush BackgroundBrush => IsEpisode ? App.SeriesEntryGridBackgroundBrush : Brushes.Transparent;
private AvaloniaEntryStatus(LibraryBook libraryBook) : base(libraryBook) { }
public static EntryStatus Create(LibraryBook libraryBook) => new AvaloniaEntryStatus(libraryBook);
protected override Bitmap LoadImage(byte[] picture)
{
try
{
using var ms = new System.IO.MemoryStream(picture);
return new Bitmap(ms);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Error loading cover art for {Book}", Book);
return _defaultImage ??= new Bitmap(App.OpenAsset("img-coverart-prod-unavailable_80x80.jpg"));
}
}
=> AvaloniaUtils.TryLoadImageOrDefault(picture, LibationFileManager.PictureSize._80x80);
protected override Bitmap GetResourceImage(string rescName)
{
//These images are assest, so assume they will never corrupt.
using var stream = App.OpenAsset(rescName + ".png");
return new Bitmap(stream);
}

View File

@ -115,16 +115,14 @@ namespace LibationAvalonia.ViewModels
PictureStorage.PictureCached += PictureStorage_PictureCached;
// Mutable property. Set the field so PropertyChanged isn't fired.
using var ms = new System.IO.MemoryStream(picture);
_cover = new Bitmap(ms);
_cover = AvaloniaUtils.TryLoadImageOrDefault(picture, PictureSize._80x80);
}
private void PictureStorage_PictureCached(object sender, PictureCachedEventArgs e)
{
if (e.Definition.PictureId == LibraryBook.Book.PictureId)
{
using var ms = new System.IO.MemoryStream(e.Picture);
Cover = new Bitmap(ms);
Cover = AvaloniaUtils.TryLoadImageOrDefault(e.Picture, PictureSize._80x80);
PictureStorage.PictureCached -= PictureStorage_PictureCached;
}
}

View File

@ -67,7 +67,7 @@ namespace LibationFileManager
}
DownloadQueue.Add(def);
return (true, getDefaultImage(def.Size));
return (true, GetDefaultImage(def.Size));
}
}
@ -96,7 +96,7 @@ namespace LibationFileManager
public static void SetDefaultImage(PictureSize pictureSize, byte[] bytes)
=> defaultImages[pictureSize] = bytes;
private static byte[] getDefaultImage(PictureSize size)
public static byte[] GetDefaultImage(PictureSize size)
=> defaultImages.ContainsKey(size)
? defaultImages[size]
: new byte[0];
@ -120,7 +120,7 @@ namespace LibationFileManager
private static byte[] downloadBytes(PictureDefinition def)
{
if (def.PictureId is null)
return getDefaultImage(def.Size);
return GetDefaultImage(def.Size);
try
{
@ -135,7 +135,7 @@ namespace LibationFileManager
}
catch
{
return getDefaultImage(def.Size);
return GetDefaultImage(def.Size);
}
}
}

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataLayer;
@ -42,7 +41,7 @@ namespace LibationWinForms.Dialogs
this.Text = Book.Title;
(_, var picture) = PictureStorage.GetPicture(new PictureDefinition(Book.PictureId, PictureSize._80x80));
this.coverPb.Image = Dinah.Core.WindowsDesktop.Drawing.ImageReader.ToImage(picture);
this.coverPb.Image = WinFormsUtil.TryLoadImageOrDefault(picture, PictureSize._80x80);
var t = @$"
Title: {Book.Title}

View File

@ -19,15 +19,7 @@ namespace LibationWinForms.GridView
public void SetCoverArt(byte[] cover)
{
try
{
pictureBox1.Image = Dinah.Core.WindowsDesktop.Drawing.ImageReader.ToImage(cover);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Error loading cover art for {file}", PictureFileName);
pictureBox1.Image = Properties.Resources.default_cover_500x500;
}
pictureBox1.Image = WinFormsUtil.TryLoadImageOrDefault(cover);
}
#region Make the form's aspect ratio always match the picture's aspect ratio.

View File

@ -1,7 +1,5 @@
using DataLayer;
using Dinah.Core.WindowsDesktop.Drawing;
using LibationUiBase.GridView;
using System;
using System.Drawing;
namespace LibationWinForms.GridView
@ -14,23 +12,12 @@ namespace LibationWinForms.GridView
private WinFormsEntryStatus(LibraryBook libraryBook) : base(libraryBook) { }
public static EntryStatus Create(LibraryBook libraryBook) => new WinFormsEntryStatus(libraryBook);
protected override object LoadImage(byte[] picture)
{
try
{
return ImageReader.ToImage(picture);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Error loading cover art for {Book}", Book);
return Properties.Resources.default_cover_80x80;
}
}
protected override Image LoadImage(byte[] picture)
=> WinFormsUtil.TryLoadImageOrDefault(picture, LibationFileManager.PictureSize._80x80);
protected override Image GetResourceImage(string rescName)
{
var image = Properties.Resources.ResourceManager.GetObject(rescName);
return image as Bitmap;
}
}

View File

@ -12,7 +12,6 @@ using AudibleApi;
using DataLayer;
using Dinah.Core;
using Dinah.Core.ErrorHandling;
using Dinah.Core.WindowsDesktop.Drawing;
using FileLiberator;
using LibationFileManager;
using LibationUiBase;
@ -87,7 +86,7 @@ namespace LibationWinForms.ProcessQueue
if (isDefault)
PictureStorage.PictureCached += PictureStorage_PictureCached;
_cover = ImageReader.ToImage(picture);
_cover = WinFormsUtil.TryLoadImageOrDefault(picture, PictureSize._80x80); ;
}
@ -95,7 +94,7 @@ namespace LibationWinForms.ProcessQueue
{
if (e.Definition.PictureId == LibraryBook.Book.PictureId)
{
Cover = ImageReader.ToImage(e.Picture);
Cover = WinFormsUtil.TryLoadImageOrDefault(e.Picture, PictureSize._80x80);
PictureStorage.PictureCached -= PictureStorage_PictureCached;
}
}
@ -260,7 +259,7 @@ namespace LibationWinForms.ProcessQueue
private void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt)
{
Cover = ImageReader.ToImage(coverArt);
Cover = WinFormsUtil.TryLoadImageOrDefault(coverArt, PictureSize._80x80);
}
#endregion

View File

@ -0,0 +1,23 @@
using Dinah.Core.WindowsDesktop.Drawing;
using LibationFileManager;
using System.Drawing;
namespace LibationWinForms
{
internal static class WinFormsUtil
{
private static Bitmap defaultImage;
public static Image TryLoadImageOrDefault(byte[] picture, PictureSize defaultSize = PictureSize.Native)
{
try
{
return ImageReader.ToImage(picture);
}
catch
{
using var ms = new System.IO.MemoryStream(PictureStorage.GetDefaultImage(defaultSize));
return defaultImage ??= new Bitmap(ms);
}
}
}
}