using MediaBrowser.Common.Extensions; using MediaBrowser.Common.IO; using MediaBrowser.Common.Net; using MediaBrowser.Controller; using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Entities; using MediaBrowser.Model.Providers; using ServiceStack; using ServiceStack.Text.Controller; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace MediaBrowser.Api.Images { public class BaseRemoteImageRequest : IReturn { [ApiMember(Name = "Type", Description = "The image type", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public ImageType? Type { get; set; } /// /// Skips over a given number of items within the results. Use for paging. /// /// The start index. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")] public int? StartIndex { get; set; } /// /// The maximum number of items to return /// /// The limit. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")] public int? Limit { get; set; } [ApiMember(Name = "ProviderName", Description = "Optional. The image provider to use", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public string ProviderName { get; set; } } [Route("/Items/{Id}/RemoteImages", "GET")] [Api(Description = "Gets available remote images for an item")] public class GetRemoteImages : BaseRemoteImageRequest { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Id { get; set; } } [Route("/Artists/{Name}/RemoteImages", "GET")] [Route("/Genres/{Name}/RemoteImages", "GET")] [Route("/GameGenres/{Name}/RemoteImages", "GET")] [Route("/MusicGenres/{Name}/RemoteImages", "GET")] [Route("/Persons/{Name}/RemoteImages", "GET")] [Route("/Studios/{Name}/RemoteImages", "GET")] [Api(Description = "Gets available remote images for an item")] public class GetItemByNameRemoteImages : BaseRemoteImageRequest { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Name { get; set; } } [Route("/Items/{Id}/RemoteImages/Providers", "GET")] [Api(Description = "Gets available remote image providers for an item")] public class GetRemoteImageProviders : IReturn> { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Id { get; set; } } [Route("/Artists/{Name}/RemoteImages/Providers", "GET")] [Route("/Genres/{Name}/RemoteImages/Providers", "GET")] [Route("/GameGenres/{Name}/RemoteImages/Providers", "GET")] [Route("/MusicGenres/{Name}/RemoteImages/Providers", "GET")] [Route("/Persons/{Name}/RemoteImages/Providers", "GET")] [Route("/Studios/{Name}/RemoteImages/Providers", "GET")] [Api(Description = "Gets available remote image providers for an item")] public class GetItemByNameRemoteImageProviders : IReturn> { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Name { get; set; } } public class BaseDownloadRemoteImage : IReturnVoid { [ApiMember(Name = "Type", Description = "The image type", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] public ImageType Type { get; set; } [ApiMember(Name = "ProviderName", Description = "The image provider", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public string ProviderName { get; set; } [ApiMember(Name = "ImageUrl", Description = "The image url", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public string ImageUrl { get; set; } } [Route("/Items/{Id}/RemoteImages/Download", "POST")] [Api(Description = "Downloads a remote image for an item")] public class DownloadRemoteImage : BaseDownloadRemoteImage { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string Id { get; set; } } [Route("/Artists/{Name}/RemoteImages/Download", "POST")] [Route("/Genres/{Name}/RemoteImages/Download", "POST")] [Route("/GameGenres/{Name}/RemoteImages/Download", "POST")] [Route("/MusicGenres/{Name}/RemoteImages/Download", "POST")] [Route("/Persons/{Name}/RemoteImages/Download", "POST")] [Route("/Studios/{Name}/RemoteImages/Download", "POST")] [Api(Description = "Downloads a remote image for an item")] public class DownloadItemByNameRemoteImage : BaseDownloadRemoteImage { /// /// Gets or sets the id. /// /// The id. [ApiMember(Name = "Name", Description = "Name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] public string Name { get; set; } } [Route("/Images/Remote", "GET")] [Api(Description = "Gets a remote image")] public class GetRemoteImage { [ApiMember(Name = "Url", Description = "The image url", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")] public string Url { get; set; } } public class RemoteImageService : BaseApiService { private readonly IProviderManager _providerManager; private readonly IServerApplicationPaths _appPaths; private readonly IHttpClient _httpClient; private readonly IFileSystem _fileSystem; private readonly IDtoService _dtoService; private readonly ILibraryManager _libraryManager; public RemoteImageService(IProviderManager providerManager, IDtoService dtoService, IServerApplicationPaths appPaths, IHttpClient httpClient, IFileSystem fileSystem, ILibraryManager libraryManager) { _providerManager = providerManager; _dtoService = dtoService; _appPaths = appPaths; _httpClient = httpClient; _fileSystem = fileSystem; _libraryManager = libraryManager; } public object Get(GetRemoteImageProviders request) { var item = _dtoService.GetItemByDtoId(request.Id); var result = GetImageProviders(item); return ToOptimizedResult(result); } public object Get(GetItemByNameRemoteImageProviders request) { var pathInfo = PathInfo.Parse(Request.PathInfo); var type = pathInfo.GetArgumentValue(0); var item = GetItemByName(request.Name, type, _libraryManager); var result = GetImageProviders(item); return ToOptimizedResult(result); } private List GetImageProviders(BaseItem item) { return _providerManager.GetImageProviderInfo(item).ToList(); } public object Get(GetRemoteImages request) { var item = _dtoService.GetItemByDtoId(request.Id); var result = GetRemoteImageResult(item, request); return ToOptimizedResult(result); } public object Get(GetItemByNameRemoteImages request) { var pathInfo = PathInfo.Parse(Request.PathInfo); var type = pathInfo.GetArgumentValue(0); var item = GetItemByName(request.Name, type, _libraryManager); return GetRemoteImageResult(item, request); } private RemoteImageResult GetRemoteImageResult(BaseItem item, BaseRemoteImageRequest request) { var images = _providerManager.GetAvailableRemoteImages(item, CancellationToken.None, request.ProviderName, request.Type).Result; var imagesList = images.ToList(); var result = new RemoteImageResult { TotalRecordCount = imagesList.Count, Providers = images.Select(i => i.ProviderName) .Distinct(StringComparer.OrdinalIgnoreCase) .ToList() }; if (request.StartIndex.HasValue) { imagesList = imagesList.Skip(request.StartIndex.Value) .ToList(); } if (request.Limit.HasValue) { imagesList = imagesList.Take(request.Limit.Value) .ToList(); } result.Images = imagesList; return result; } /// /// Posts the specified request. /// /// The request. public void Post(DownloadRemoteImage request) { var item = _dtoService.GetItemByDtoId(request.Id); var task = DownloadRemoteImage(item, request); Task.WaitAll(task); } public void Post(DownloadItemByNameRemoteImage request) { var pathInfo = PathInfo.Parse(Request.PathInfo); var type = pathInfo.GetArgumentValue(0); var item = GetItemByName(request.Name, type, _libraryManager); var task = DownloadRemoteImage(item, request); Task.WaitAll(task); } /// /// Downloads the remote image. /// /// The item. /// The request. /// Task. private async Task DownloadRemoteImage(BaseItem item, BaseDownloadRemoteImage request) { await _providerManager.SaveImage(item, request.ImageUrl, null, request.Type, null, CancellationToken.None).ConfigureAwait(false); await item.RefreshMetadata(new MetadataRefreshOptions { ForceSave = true, ImageRefreshMode = MetadataRefreshMode.None, MetadataRefreshMode = MetadataRefreshMode.None }, CancellationToken.None).ConfigureAwait(false); } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetRemoteImage request) { var task = GetRemoteImage(request); return task.Result; } /// /// Gets the remote image. /// /// The request. /// Task{System.Object}. private async Task GetRemoteImage(GetRemoteImage request) { var urlHash = request.Url.GetMD5(); var pointerCachePath = GetFullCachePath(urlHash.ToString()); string contentPath; try { using (var reader = new StreamReader(pointerCachePath)) { contentPath = await reader.ReadToEndAsync().ConfigureAwait(false); } if (File.Exists(contentPath)) { return ToStaticFileResult(contentPath); } } catch (FileNotFoundException) { // Means the file isn't cached yet } await DownloadImage(request.Url, urlHash, pointerCachePath).ConfigureAwait(false); // Read the pointer file again using (var reader = new StreamReader(pointerCachePath)) { contentPath = await reader.ReadToEndAsync().ConfigureAwait(false); } return ToStaticFileResult(contentPath); } /// /// Downloads the image. /// /// The URL. /// The URL hash. /// The pointer cache path. /// Task. private async Task DownloadImage(string url, Guid urlHash, string pointerCachePath) { var result = await _httpClient.GetResponse(new HttpRequestOptions { Url = url }).ConfigureAwait(false); var ext = result.ContentType.Split('/').Last(); var fullCachePath = GetFullCachePath(urlHash + "." + ext); Directory.CreateDirectory(Path.GetDirectoryName(fullCachePath)); using (var stream = result.Content) { using (var filestream = _fileSystem.GetFileStream(fullCachePath, FileMode.Create, FileAccess.Write, FileShare.Read, true)) { await stream.CopyToAsync(filestream).ConfigureAwait(false); } } using (var writer = new StreamWriter(pointerCachePath)) { await writer.WriteAsync(fullCachePath).ConfigureAwait(false); } } /// /// Gets the full cache path. /// /// The filename. /// System.String. private string GetFullCachePath(string filename) { return Path.Combine(_appPaths.DownloadedImagesDataPath, filename.Substring(0, 1), filename); } } }