Lidarr/src/Lidarr.Api.V1/MediaCovers/MediaCoverModule.cs

72 lines
3.0 KiB
C#
Raw Normal View History

2017-11-26 04:55:07 +00:00
using System.IO;
2017-09-04 02:20:56 +00:00
using System.Text.RegularExpressions;
using Nancy;
using Nancy.Responses;
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
2017-10-31 01:28:29 +00:00
namespace Lidarr.Api.V1.MediaCovers
2017-09-04 02:20:56 +00:00
{
2017-10-31 01:28:29 +00:00
public class MediaCoverModule : LidarrV1Module
2017-09-04 02:20:56 +00:00
{
private const string MEDIA_COVER_ARTIST_ROUTE = @"/Artist/(?<artistId>\d+)/(?<filename>(.+)\.(jpg|png|gif))";
private const string MEDIA_COVER_ALBUM_ROUTE = @"/Album/(?<artistId>\d+)/(?<filename>(.+)\.(jpg|png|gif))";
2017-09-04 02:20:56 +00:00
private static readonly Regex RegexResizedImage = new Regex(@"-\d+(?=\.(jpg|png|gif)$)", RegexOptions.Compiled | RegexOptions.IgnoreCase);
2017-09-04 02:20:56 +00:00
private readonly IAppFolderInfo _appFolderInfo;
private readonly IDiskProvider _diskProvider;
public MediaCoverModule(IAppFolderInfo appFolderInfo, IDiskProvider diskProvider)
: base("MediaCover")
2017-09-04 02:20:56 +00:00
{
_appFolderInfo = appFolderInfo;
_diskProvider = diskProvider;
Get(MEDIA_COVER_ARTIST_ROUTE, options => GetArtistMediaCover(options.artistId, options.filename));
Get(MEDIA_COVER_ALBUM_ROUTE, options => GetAlbumMediaCover(options.artistId, options.filename));
2017-09-04 02:20:56 +00:00
}
2019-09-12 20:32:51 +00:00
private object GetArtistMediaCover(int artistId, string filename)
2017-09-04 02:20:56 +00:00
{
2017-11-26 04:55:07 +00:00
var filePath = Path.Combine(_appFolderInfo.GetAppDataPath(), "MediaCover", artistId.ToString(), filename);
2017-09-04 02:20:56 +00:00
if (!_diskProvider.FileExists(filePath) || _diskProvider.GetFileSize(filePath) == 0)
{
// Return the full sized image if someone requests a non-existing resized one.
// TODO: This code can be removed later once everyone had the update for a while.
var basefilePath = RegexResizedImage.Replace(filePath, "");
2017-09-04 02:20:56 +00:00
if (basefilePath == filePath || !_diskProvider.FileExists(basefilePath))
{
return new NotFoundResponse();
}
2017-09-04 02:20:56 +00:00
filePath = basefilePath;
}
return new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
}
2019-09-12 20:32:51 +00:00
private object GetAlbumMediaCover(int albumId, string filename)
{
var filePath = Path.Combine(_appFolderInfo.GetAppDataPath(), "MediaCover", "Albums", albumId.ToString(), filename);
if (!_diskProvider.FileExists(filePath) || _diskProvider.GetFileSize(filePath) == 0)
{
// Return the full sized image if someone requests a non-existing resized one.
// TODO: This code can be removed later once everyone had the update for a while.
var basefilePath = RegexResizedImage.Replace(filePath, "");
if (basefilePath == filePath || !_diskProvider.FileExists(basefilePath))
{
return new NotFoundResponse();
}
filePath = basefilePath;
}
return new StreamResponse(() => File.OpenRead(filePath), MimeTypes.GetMimeType(filePath));
}
2017-09-04 02:20:56 +00:00
}
}