jellyfin/MediaBrowser.Api/Images/RemoteImageService.cs

311 lines
12 KiB
C#
Raw Normal View History

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;
2014-11-15 02:31:03 +00:00
using MediaBrowser.Controller.Net;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Providers;
2013-12-07 15:52:38 +00:00
using ServiceStack;
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<RemoteImageResult>
{
[ApiMember(Name = "Type", Description = "The image type", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public ImageType? Type { get; set; }
/// <summary>
/// Skips over a given number of items within the results. Use for paging.
/// </summary>
/// <value>The start index.</value>
[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; }
/// <summary>
/// The maximum number of items to return
/// </summary>
/// <value>The limit.</value>
[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; }
2014-02-12 03:46:27 +00:00
[ApiMember(Name = "IncludeAllLanguages", Description = "Optional.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool IncludeAllLanguages { get; set; }
}
2014-11-15 02:31:03 +00:00
[Route("/Items/{Id}/RemoteImages", "GET", Summary = "Gets available remote images for an item")]
[Authenticated]
public class GetRemoteImages : BaseRemoteImageRequest
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
2014-11-15 02:31:03 +00:00
[Route("/Items/{Id}/RemoteImages/Providers", "GET", Summary = "Gets available remote image providers for an item")]
[Authenticated]
public class GetRemoteImageProviders : IReturn<List<ImageProviderInfo>>
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { 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; }
}
2014-11-15 02:31:03 +00:00
[Route("/Items/{Id}/RemoteImages/Download", "POST", Summary = "Downloads a remote image for an item")]
[Authenticated(Roles="Admin")]
public class DownloadRemoteImage : BaseDownloadRemoteImage
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
2014-11-15 02:31:03 +00:00
[Route("/Images/Remote", "GET", Summary = "Gets a remote image")]
public class GetRemoteImage
{
2014-02-09 06:08:10 +00:00
[ApiMember(Name = "ImageUrl", Description = "The image url", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
public string ImageUrl { 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)
{
2014-04-25 20:15:50 +00:00
var item = _libraryManager.GetItemById(request.Id);
var result = GetImageProviders(item);
return ToOptimizedSerializedResultUsingCache(result);
}
private List<ImageProviderInfo> GetImageProviders(BaseItem item)
{
2014-02-09 21:11:11 +00:00
return _providerManager.GetRemoteImageProviderInfo(item).ToList();
}
2014-08-19 01:42:53 +00:00
public async Task<object> Get(GetRemoteImages request)
{
2014-04-25 20:15:50 +00:00
var item = _libraryManager.GetItemById(request.Id);
2014-08-19 01:42:53 +00:00
return await GetRemoteImageResult(item, request).ConfigureAwait(false);
}
2014-08-17 05:38:13 +00:00
private async Task<RemoteImageResult> GetRemoteImageResult(BaseItem item, BaseRemoteImageRequest request)
{
2014-08-17 05:38:13 +00:00
var images = await _providerManager.GetAvailableRemoteImages(item, new RemoteImageQuery
2014-02-12 03:46:27 +00:00
{
ProviderName = request.ProviderName,
IncludeAllLanguages = request.IncludeAllLanguages,
IncludeDisabledProviders = true,
ImageType = request.Type
2014-08-17 05:38:13 +00:00
}, CancellationToken.None).ConfigureAwait(false);
var imagesList = images.ToList();
2014-02-11 04:55:01 +00:00
var allProviders = _providerManager.GetRemoteImageProviderInfo(item);
if (request.Type.HasValue)
{
allProviders = allProviders.Where(i => i.SupportedImages.Contains(request.Type.Value));
}
var result = new RemoteImageResult
{
TotalRecordCount = imagesList.Count,
2014-02-11 04:55:01 +00:00
Providers = allProviders.Select(i => i.Name)
.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;
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public void Post(DownloadRemoteImage request)
{
2014-04-25 20:15:50 +00:00
var item = _libraryManager.GetItemById(request.Id);
var task = DownloadRemoteImage(item, request);
Task.WaitAll(task);
}
/// <summary>
/// Downloads the remote image.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="request">The request.</param>
/// <returns>Task.</returns>
private async Task DownloadRemoteImage(BaseItem item, BaseDownloadRemoteImage request)
{
await _providerManager.SaveImage(item, request.ImageUrl, null, request.Type, null, CancellationToken.None).ConfigureAwait(false);
await item.UpdateToRepository(ItemUpdateType.ImageUpdate, CancellationToken.None).ConfigureAwait(false);
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
2014-08-19 01:42:53 +00:00
public object Get(GetRemoteImage request)
{
return GetAsync(request).Result;
}
public async Task<object> GetAsync(GetRemoteImage request)
{
2014-02-09 06:08:10 +00:00
var urlHash = request.ImageUrl.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);
}
}
2014-02-09 07:27:44 +00:00
catch (DirectoryNotFoundException)
{
// Means the file isn't cached yet
}
catch (FileNotFoundException)
{
// Means the file isn't cached yet
}
2014-02-09 06:08:10 +00:00
await DownloadImage(request.ImageUrl, urlHash, pointerCachePath).ConfigureAwait(false);
// Read the pointer file again
using (var reader = new StreamReader(pointerCachePath))
{
contentPath = await reader.ReadToEndAsync().ConfigureAwait(false);
}
return ToStaticFileResult(contentPath);
}
2014-08-19 01:42:53 +00:00
/// <summary>
/// Downloads the image.
/// </summary>
/// <param name="url">The URL.</param>
/// <param name="urlHash">The URL hash.</param>
/// <param name="pointerCachePath">The pointer cache path.</param>
/// <returns>Task.</returns>
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);
}
}
2014-02-09 07:27:44 +00:00
Directory.CreateDirectory(Path.GetDirectoryName(pointerCachePath));
using (var writer = new StreamWriter(pointerCachePath))
{
await writer.WriteAsync(fullCachePath).ConfigureAwait(false);
}
}
/// <summary>
/// Gets the full cache path.
/// </summary>
/// <param name="filename">The filename.</param>
/// <returns>System.String.</returns>
private string GetFullCachePath(string filename)
{
2014-02-08 20:02:35 +00:00
return Path.Combine(_appPaths.CachePath, "remote-images", filename.Substring(0, 1), filename);
}
}
}