2
0
Fork 0
mirror of https://github.com/sphildreth/roadie synced 2025-02-17 21:48:27 +00:00

Thumbnail cleanup.

This commit is contained in:
Steven Hildreth 2019-07-25 10:43:11 -05:00
parent 247502408a
commit 93b4014b91
11 changed files with 77 additions and 129 deletions

View file

@ -81,13 +81,7 @@ namespace Roadie.Library.Engines
artist.Thumbnail = firstImageWithNotNullBytes.Bytes;
if (artist.Thumbnail != null)
{
artist.Thumbnail = ImageHelper.ResizeImage(artist.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
artist.Thumbnail = ImageHelper.ConvertToJpegFormat(artist.Thumbnail);
if (artist.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Artist Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{artist.Thumbnail.Length}]");
artist.Thumbnail = null;
}
artist.Thumbnail = ImageHelper.ResizeToThumbnail(artist.Thumbnail, Configuration);
}
}
}
@ -615,16 +609,20 @@ namespace Roadie.Library.Engines
imageBag.Add(await WebHelper.GetImageFromUrlAsync(url));
});
await Task.WhenAll(i);
result.Images = imageBag.Where(x => x != null && x.Bytes != null).GroupBy(x => x.Signature)
.Select(x => x.First()).Take(Configuration.Processing.MaximumArtistImagesToAdd).ToList();
result.Images = imageBag.Where(x => x != null && x.Bytes != null)
.GroupBy(x => x.Signature)
.Select(x => x.First())
.Take(Configuration.Processing.MaximumArtistImagesToAdd)
.ToList();
if (result.Thumbnail == null && result.Images != null)
{
result.Thumbnail = result.Images.First().Bytes;
}
}
if (result.Thumbnail != null)
{
result.Thumbnail = ImageHelper.ResizeImage(result.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
result.Thumbnail = ImageHelper.ConvertToJpegFormat(result.Thumbnail);
result.Thumbnail = ImageHelper.ResizeToThumbnail(result.Thumbnail, Configuration);
}
}
catch (Exception ex)

View file

@ -841,14 +841,7 @@ namespace Roadie.Library.Engines
if (result.Thumbnail != null)
{
result.Thumbnail = ImageHelper.ResizeImage(result.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
result.Thumbnail = ImageHelper.ConvertToJpegFormat(result.Thumbnail);
if (result.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning(
$"Release Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{result.Thumbnail.Length}]");
result.Thumbnail = null;
}
result.Thumbnail = ImageHelper.ResizeToThumbnail(result.Thumbnail, Configuration);
}
sw.Stop();

View file

@ -36,9 +36,11 @@ namespace Roadie.Library.Imaging
IImageFormat imageFormat = null;
using (var image = Image.Load(imageBytes, out imageFormat))
{
image.Save(outStream, ImageFormats.Jpeg);
if (imageFormat != ImageFormats.Jpeg)
{
image.Save(outStream, ImageFormats.Jpeg);
}
}
return outStream.ToArray();
}
}
@ -255,6 +257,24 @@ namespace Roadie.Library.Imaging
return null;
}
/// <summary>
/// Convert to JPEG and Resize given image to be a thumbnail size, if larger than maximum thumbnail size no image is returned.
/// </summary>
public static byte[] ResizeToThumbnail(byte[] imageBytes, IRoadieSettings configuration)
{
if(!imageBytes.Any())
{
return imageBytes;
}
var result = ImageHelper.ResizeImage(ImageHelper.ConvertToJpegFormat(imageBytes), configuration.ThumbnailImageSize.Width, configuration.ThumbnailImageSize.Height, true).Item2;
if(result.Length > ImageHelper.MaximumThumbnailByteSize)
{
Trace.WriteLine($"Thumbnail larger than maximum size after resizing to [{configuration.ThumbnailImageSize.Width}x{configuration.ThumbnailImageSize.Height}] Thumbnail Size [{result.Length}]");
result = new byte[0];
}
return result;
}
/// <summary>
/// Get image data from all sources for either fileanme or MetaData
/// </summary>

View file

@ -10,10 +10,10 @@
<ItemGroup>
<PackageReference Include="AutoCompare.Core" Version="1.0.0" />
<PackageReference Include="CsvHelper" Version="12.1.2" />
<PackageReference Include="EFCore.BulkExtensions" Version="2.5.1" />
<PackageReference Include="FluentFTP" Version="26.0.2" />
<PackageReference Include="EFCore.BulkExtensions" Version="2.5.2" />
<PackageReference Include="FluentFTP" Version="27.0.0" />
<PackageReference Include="Hashids.net" Version="1.2.2" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.9" />
<PackageReference Include="HtmlAgilityPack" Version="1.11.12" />
<PackageReference Include="IdSharp.Common" Version="1.0.1" />
<PackageReference Include="IdSharp.Tagging" Version="1.0.0-rc3" />
<PackageReference Include="Inflatable.Lastfm" Version="1.1.0.339" />

View file

@ -703,20 +703,12 @@ namespace Roadie.Api.Services
var artistImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
if (artistImage != null)
{
// Ensure is jpeg first
artist.Thumbnail = ImageHelper.ConvertToJpegFormat(artistImage);
// Save unaltered image to cover file
var artistImageName = Path.Combine(newArtistFolder, ImageHelper.ArtistImageFilename);
File.WriteAllBytes(artistImageName, artist.Thumbnail);
File.WriteAllBytes(artistImageName, ImageHelper.ConvertToJpegFormat(artistImage));
// Resize to store in database as thumbnail
artist.Thumbnail = ImageHelper.ResizeImage(artist.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
if (artist.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Artist Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{artist.Thumbnail.Length}]");
artist.Thumbnail = null;
}
artist.Thumbnail = ImageHelper.ResizeToThumbnail(artistImage, Configuration);
didChangeThumbnail = true;
}
@ -1290,9 +1282,6 @@ namespace Roadie.Api.Services
artist.Thumbnail = imageBytes;
if (artist.Thumbnail != null)
{
// Ensure is jpeg first
artist.Thumbnail = ImageHelper.ConvertToJpegFormat(artist.Thumbnail);
// Ensure artist folder exists
var artistFolder = artist.ArtistFileFolder(Configuration);
if (!Directory.Exists(artistFolder))
@ -1303,15 +1292,10 @@ namespace Roadie.Api.Services
// Save unaltered image to artist file
var artistImage = Path.Combine(artistFolder, ImageHelper.ArtistImageFilename);
File.WriteAllBytes(artistImage, artist.Thumbnail);
File.WriteAllBytes(artistImage, ImageHelper.ConvertToJpegFormat(imageBytes));
// Resize to store in database as thumbnail
artist.Thumbnail = ImageHelper.ResizeImage(artist.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
if (artist.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Artist Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{artist.Thumbnail.Length}]");
artist.Thumbnail = null;
}
artist.Thumbnail = ImageHelper.ResizeToThumbnail(artist.Thumbnail, Configuration);
}
artist.LastUpdated = now;
@ -1391,21 +1375,12 @@ namespace Roadie.Api.Services
var imageFiles = ImageHelper.ImageFilesInFolder(artistFolder, SearchOption.AllDirectories);
if (imageFiles != null && imageFiles.Any())
{
var imageFile = imageFiles.First();
var i = new FileInfo(imageFile);
var i = new FileInfo(imageFiles.First());
var iName = i.Name.ToLower().Trim();
var isArtistImage = iName.Contains("artist");
if (isArtistImage)
if (ImageHelper.IsArtistImage(i))
{
// Read image and convert to jpeg
artist.Thumbnail = File.ReadAllBytes(i.FullName);
artist.Thumbnail = ImageHelper.ResizeImage(artist.Thumbnail,Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
artist.Thumbnail = ImageHelper.ConvertToJpegFormat(artist.Thumbnail);
if (artist.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Artist Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{artist.Thumbnail.Length}]");
artist.Thumbnail = null;
}
artist.Thumbnail = ImageHelper.ResizeToThumbnail(File.ReadAllBytes(i.FullName), Configuration);
artist.LastUpdated = DateTime.UtcNow;
await DbContext.SaveChangesAsync();
CacheManager.ClearRegion(artist.CacheRegion);

View file

@ -249,12 +249,7 @@ namespace Roadie.Api.Services
var collectionImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
if (collectionImage != null)
{
// Ensure is jpeg first
collection.Thumbnail = ImageHelper.ConvertToJpegFormat(collectionImage);
// Resize to store in database as thumbnail
collection.Thumbnail = ImageHelper.ResizeImage(collection.Thumbnail,
Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
collection.Thumbnail = ImageHelper.ResizeToThumbnail(collectionImage, Configuration);
}
if (model.Maintainer?.Value != null)

View file

@ -210,14 +210,18 @@ namespace Roadie.Api.Services
etag);
}
/// <summary>
/// Get image for an artist, see if the artist has an image in their folder and use that else use Artist.Thumbnail, is also not found use Artist DefaultNotFound image.
/// </summary>
private Task<FileOperationResult<Image>> ArtistImageAction(Guid id, EntityTagHeaderValue etag = null)
{
try
{
var artist = GetArtist(id);
if (artist == null)
return Task.FromResult(new FileOperationResult<Image>(true,
string.Format("Artist Not Found [{0}]", id)));
{
return Task.FromResult(new FileOperationResult<Image>(true, string.Format("Artist Not Found [{0}]", id)));
}
byte[] imageBytes = null;
string artistFolder = null;
try
@ -230,9 +234,11 @@ namespace Roadie.Api.Services
}
else
{
var artistImages =
ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(artistFolder), ImageType.Artist);
if (artistImages.Any()) imageBytes = File.ReadAllBytes(artistImages.First().FullName);
var artistImages = ImageHelper.FindImageTypeInDirectory(new DirectoryInfo(artistFolder), ImageType.Artist);
if (artistImages.Any())
{
imageBytes = File.ReadAllBytes(artistImages.First().FullName);
}
}
}
catch (Exception ex)
@ -247,7 +253,10 @@ namespace Roadie.Api.Services
CreatedDate = artist.CreatedDate,
LastUpdated = artist.LastUpdated
};
if (imageBytes == null || !imageBytes.Any()) image = DefaultNotFoundImages.Artist;
if (imageBytes == null || !imageBytes.Any())
{
image = DefaultNotFoundImages.Artist;
}
return Task.FromResult(GenerateFileOperationResult(id, image, etag));
}
catch (Exception ex)
@ -466,6 +475,9 @@ namespace Roadie.Api.Services
return Task.FromResult(new FileOperationResult<Image>(OperationMessages.ErrorOccured));
}
/// <summary>
/// Get image for Release from Release folder, if not exists then use Release.Thumbnail if that is not set then use Release DefaultNotFound image.
/// </summary>
private Task<FileOperationResult<Image>> ReleaseImageAction(Guid id, EntityTagHeaderValue etag = null)
{
try
@ -505,8 +517,7 @@ namespace Roadie.Api.Services
}
catch (Exception ex)
{
Logger.LogError(ex,
$"Error Reading Release Folder [{releaseFolder}] Artist Folder [{artistFolder}] For Artist `{release.Artist.Id}`");
Logger.LogError(ex, $"Error Reading Release Folder [{releaseFolder}] Artist Folder [{artistFolder}] For Artist `{release.Artist.Id}`");
}
imageBytes = imageBytes ?? release.Thumbnail;
@ -516,7 +527,10 @@ namespace Roadie.Api.Services
CreatedDate = release.CreatedDate,
LastUpdated = release.LastUpdated
};
if (release.Thumbnail == null || !release.Thumbnail.Any()) image = DefaultNotFoundImages.Release;
if (release.Thumbnail == null || !release.Thumbnail.Any())
{
image = DefaultNotFoundImages.Release;
}
return Task.FromResult(GenerateFileOperationResult(id, image, etag));
}
catch (Exception ex)

View file

@ -184,12 +184,8 @@ namespace Roadie.Api.Services
var labelImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
if (labelImage != null)
{
// Ensure is jpeg first
label.Thumbnail = ImageHelper.ConvertToJpegFormat(labelImage);
// Resize to store in database as thumbnail
label.Thumbnail = ImageHelper.ResizeImage(label.Thumbnail, Configuration.MediumImageSize.Width,
Configuration.MediumImageSize.Height);
label.Thumbnail = ImageHelper.ResizeToThumbnail(labelImage, Configuration);
}
label.LastUpdated = now;
@ -324,11 +320,7 @@ namespace Roadie.Api.Services
if (label.Thumbnail != null)
{
// Ensure is jpeg first
label.Thumbnail = ImageHelper.ConvertToJpegFormat(label.Thumbnail);
// Resize to store in database as thumbnail
label.Thumbnail = ImageHelper.ResizeImage(label.Thumbnail, Configuration.MediumImageSize.Width,
Configuration.MediumImageSize.Height);
label.Thumbnail = ImageHelper.ResizeToThumbnail(label.Thumbnail, Configuration);
}
label.LastUpdated = now;

View file

@ -296,11 +296,7 @@ namespace Roadie.Api.Services
if (playlistImage != null)
{
// Ensure is jpeg first
playlist.Thumbnail = ImageHelper.ConvertToJpegFormat(playlistImage);
// Resize to store in database as thumbnail
playlist.Thumbnail = ImageHelper.ResizeImage(playlist.Thumbnail,
Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
playlist.Thumbnail = ImageHelper.ResizeToThumbnail(playlistImage, Configuration);
}
playlist.LastUpdated = now;

View file

@ -1294,14 +1294,7 @@ namespace Roadie.Api.Services
{
// Read image and convert to jpeg
var i = imageFiles.First();
release.Thumbnail = File.ReadAllBytes(i.FullName);
release.Thumbnail = ImageHelper.ResizeImage(release.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
release.Thumbnail = ImageHelper.ConvertToJpegFormat(release.Thumbnail);
if (release.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Release Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{release.Thumbnail.Length}]");
release.Thumbnail = null;
}
release.Thumbnail = ImageHelper.ResizeToThumbnail(File.ReadAllBytes(i.FullName), Configuration);
release.LastUpdated = now;
await DbContext.SaveChangesAsync();
@ -1400,24 +1393,12 @@ namespace Roadie.Api.Services
var releaseImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
if (releaseImage != null)
{
// Ensure is jpeg first
release.Thumbnail = ImageHelper.ConvertToJpegFormat(releaseImage);
// Save unaltered image to cover file
var coverFileName =
Path.Combine(
release.ReleaseFileFolder(
release.Artist.ArtistFileFolder(Configuration)),
"cover.jpg");
File.WriteAllBytes(coverFileName, release.Thumbnail);
var coverFileName = Path.Combine(release.ReleaseFileFolder(release.Artist.ArtistFileFolder(Configuration)),"cover.jpg");
File.WriteAllBytes(coverFileName, ImageHelper.ConvertToJpegFormat(releaseImage));
// Resize to store in database as thumbnail
release.Thumbnail = ImageHelper.ResizeImage(release.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
if (release.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Release Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{release.Thumbnail.Length}]");
release.Thumbnail = null;
}
release.Thumbnail = ImageHelper.ResizeToThumbnail(releaseImage, Configuration);
didChangeThumbnail = true;
}
@ -1881,20 +1862,12 @@ namespace Roadie.Api.Services
release.Thumbnail = imageBytes;
if (release.Thumbnail != null)
{
// Ensure is jpeg first
release.Thumbnail = ImageHelper.ConvertToJpegFormat(release.Thumbnail);
// Save unaltered image to cover file
var coverFileName = Path.Combine(release.ReleaseFileFolder(release.Artist.ArtistFileFolder(Configuration)),"cover.jpg");
File.WriteAllBytes(coverFileName, release.Thumbnail);
File.WriteAllBytes(coverFileName, ImageHelper.ConvertToJpegFormat(imageBytes));
// Resize to store in database as thumbnail
release.Thumbnail = ImageHelper.ResizeImage(release.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
if (release.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Release Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{release.Thumbnail.Length}]");
release.Thumbnail = null;
}
release.Thumbnail = ImageHelper.ResizeToThumbnail(imageBytes, Configuration);
}
release.LastUpdated = now;

View file

@ -837,20 +837,12 @@ namespace Roadie.Api.Services
var trackImage = ImageHelper.ImageDataFromUrl(model.NewThumbnailData);
if (trackImage != null)
{
// Ensure is jpeg first
track.Thumbnail = ImageHelper.ConvertToJpegFormat(trackImage);
// Save unaltered image to cover file
var trackThumbnailName = track.PathToTrackThumbnail(Configuration);
File.WriteAllBytes(trackThumbnailName, track.Thumbnail);
File.WriteAllBytes(trackThumbnailName, ImageHelper.ConvertToJpegFormat(trackImage));
// Resize to store in database as thumbnail
track.Thumbnail = ImageHelper.ResizeImage(track.Thumbnail, Configuration.MediumImageSize.Width, Configuration.MediumImageSize.Height);
if (track.Thumbnail.Length >= ImageHelper.MaximumThumbnailByteSize)
{
Logger.LogWarning($"Track Thumbnail larger than maximum size after resizing to [{Configuration.MediumImageSize.Width}x{Configuration.MediumImageSize.Height}] Thumbnail Size [{track.Thumbnail.Length}]");
track.Thumbnail = null;
}
track.Thumbnail = ImageHelper.ResizeToThumbnail(trackImage, Configuration);
didChangeThumbnail = true;
}