jellyfin-server/MediaBrowser.Api/Social/SharingService.cs

180 lines
6.3 KiB
C#
Raw Normal View History

2015-06-30 23:59:45 +00:00
using MediaBrowser.Common.Extensions;
using MediaBrowser.Controller.Dlna;
2015-07-03 11:51:45 +00:00
using MediaBrowser.Controller.Dto;
2015-06-30 23:59:45 +00:00
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Net;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Social;
using System;
using System.IO;
using System.Threading.Tasks;
2016-10-25 19:02:04 +00:00
using MediaBrowser.Model.Services;
2015-06-30 23:59:45 +00:00
namespace MediaBrowser.Api.Social
{
[Route("/Social/Shares/{Id}", "GET", Summary = "Gets a share")]
[Authenticated]
public class GetSocialShareInfo : IReturn<SocialShareInfo>
{
[ApiMember(Name = "Id", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
[Route("/Social/Shares/Public/{Id}", "GET", Summary = "Gets a share")]
public class GetPublicSocialShareInfo : IReturn<SocialShareInfo>
{
[ApiMember(Name = "Id", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
[Route("/Social/Shares/Public/{Id}/Image", "GET", Summary = "Gets a share")]
public class GetShareImage
{
[ApiMember(Name = "Id", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
[Route("/Social/Shares", "POST", Summary = "Creates a share")]
[Authenticated]
public class CreateShare : IReturn<SocialShareInfo>
{
[ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string ItemId { get; set; }
[ApiMember(Name = "UserId", Description = "The user id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
public string UserId { get; set; }
}
[Route("/Social/Shares/{Id}", "DELETE", Summary = "Deletes a share")]
[Authenticated]
public class DeleteShare : IReturnVoid
{
[ApiMember(Name = "Id", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get; set; }
}
2015-07-03 11:51:45 +00:00
[Route("/Social/Shares/Public/{Id}/Item", "GET", Summary = "Gets a share")]
public class GetSharedLibraryItem
{
[ApiMember(Name = "Id", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get; set; }
}
2015-06-30 23:59:45 +00:00
public class SharingService : BaseApiService
{
private readonly ISharingManager _sharingManager;
private readonly ILibraryManager _libraryManager;
private readonly IDlnaManager _dlnaManager;
2015-07-03 11:51:45 +00:00
private readonly IDtoService _dtoService;
2016-11-10 14:41:24 +00:00
private readonly IHttpResultFactory _resultFactory;
2015-06-30 23:59:45 +00:00
2016-11-10 14:41:24 +00:00
public SharingService(ISharingManager sharingManager, IDlnaManager dlnaManager, ILibraryManager libraryManager, IDtoService dtoService, IHttpResultFactory resultFactory)
2015-06-30 23:59:45 +00:00
{
_sharingManager = sharingManager;
_dlnaManager = dlnaManager;
_libraryManager = libraryManager;
2015-07-03 11:51:45 +00:00
_dtoService = dtoService;
2016-11-10 14:41:24 +00:00
_resultFactory = resultFactory;
2015-06-30 23:59:45 +00:00
}
public object Get(GetSocialShareInfo request)
{
var info = _sharingManager.GetShareInfo(request.Id);
return ToOptimizedResult(info);
}
2015-07-03 11:51:45 +00:00
public object Get(GetSharedLibraryItem request)
{
var info = _sharingManager.GetShareInfo(request.Id);
if (info.ExpirationDate <= DateTime.UtcNow)
{
throw new ResourceNotFoundException();
}
var item = _libraryManager.GetItemById(info.ItemId);
var dto = _dtoService.GetBaseItemDto(item, new DtoOptions());
return ToOptimizedResult(dto);
}
2015-06-30 23:59:45 +00:00
public object Get(GetPublicSocialShareInfo request)
{
var info = _sharingManager.GetShareInfo(request.Id);
2015-07-03 11:51:45 +00:00
if (info.ExpirationDate <= DateTime.UtcNow)
2015-06-30 23:59:45 +00:00
{
throw new ResourceNotFoundException();
}
return ToOptimizedResult(info);
}
public async Task<object> Post(CreateShare request)
{
var info = await _sharingManager.CreateShare(request.ItemId, request.UserId).ConfigureAwait(false);
return ToOptimizedResult(info);
}
public void Delete(DeleteShare request)
{
var task = _sharingManager.DeleteShare(request.Id);
Task.WaitAll(task);
}
2015-10-16 17:06:31 +00:00
public async Task<object> Get(GetShareImage request)
2015-06-30 23:59:45 +00:00
{
var share = _sharingManager.GetShareInfo(request.Id);
if (share == null)
{
throw new ResourceNotFoundException();
}
2015-07-03 11:51:45 +00:00
if (share.ExpirationDate <= DateTime.UtcNow)
2015-06-30 23:59:45 +00:00
{
throw new ResourceNotFoundException();
}
var item = _libraryManager.GetItemById(share.ItemId);
var image = item.GetImageInfo(ImageType.Primary, 0);
if (image != null)
{
2015-10-16 17:06:31 +00:00
if (image.IsLocalFile)
{
2016-11-10 21:06:00 +00:00
return await _resultFactory.GetStaticFileResult(Request, image.Path).ConfigureAwait(false);
2015-10-16 17:06:31 +00:00
}
try
{
// Don't fail the request over this
var updatedImage = await _libraryManager.ConvertImageToLocal(item, image, 0).ConfigureAwait(false);
2016-11-10 21:06:00 +00:00
return await _resultFactory.GetStaticFileResult(Request, updatedImage.Path).ConfigureAwait(false);
2015-10-16 17:06:31 +00:00
}
catch
{
}
2015-06-30 23:59:45 +00:00
}
// Grab a dlna icon if nothing else is available
using (var response = _dlnaManager.GetIcon("logo240.jpg"))
{
using (var ms = new MemoryStream())
{
response.Stream.CopyTo(ms);
ms.Position = 0;
var bytes = ms.ToArray();
return ResultFactory.GetResult(bytes, "image/" + response.Format.ToString().ToLower());
}
}
}
}
}