jellyfin/MediaBrowser.Api/VideosService.cs

200 lines
7.1 KiB
C#
Raw Normal View History

2014-03-19 04:59:45 +00:00
using MediaBrowser.Common.IO;
using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
2014-07-02 18:34:08 +00:00
using MediaBrowser.Controller.Net;
2014-03-19 04:59:45 +00:00
using MediaBrowser.Controller.Persistence;
using MediaBrowser.Model.Querying;
2013-12-07 15:52:38 +00:00
using ServiceStack;
using System;
using System.Linq;
2014-03-19 01:35:40 +00:00
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
[Route("/Videos/{Id}/AdditionalParts", "GET", Summary = "Gets additional parts for a video.")]
public class GetAdditionalParts : IReturn<ItemsResult>
{
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public Guid? UserId { get; set; }
/// <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-03-22 16:16:43 +00:00
[Route("/Videos/{Id}/AlternateSources", "DELETE", Summary = "Removes alternate video sources.")]
public class DeleteAlternateSources : IReturnVoid
{
2014-03-19 01:35:40 +00:00
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get; set; }
2014-03-19 01:35:40 +00:00
}
[Route("/Videos/MergeVersions", "POST", Summary = "Merges videos into a single record")]
2014-03-19 01:35:40 +00:00
public class MergeVersions : IReturnVoid
{
[ApiMember(Name = "Ids", Description = "Item id list. This allows multiple, comma delimited.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST", AllowMultiple = true)]
public string Ids { get; set; }
}
2014-07-02 18:34:08 +00:00
[Authenticated]
public class VideosService : BaseApiService
{
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
2013-09-04 17:02:19 +00:00
private readonly IDtoService _dtoService;
2014-03-19 04:59:45 +00:00
private readonly IFileSystem _fileSystem;
private readonly IItemRepository _itemRepo;
private readonly IServerConfigurationManager _config;
2014-03-19 04:59:45 +00:00
public VideosService(ILibraryManager libraryManager, IUserManager userManager, IDtoService dtoService, IItemRepository itemRepo, IFileSystem fileSystem, IServerConfigurationManager config)
{
_libraryManager = libraryManager;
_userManager = userManager;
2013-09-04 17:02:19 +00:00
_dtoService = dtoService;
2014-03-19 04:59:45 +00:00
_itemRepo = itemRepo;
_fileSystem = fileSystem;
_config = config;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get(GetAdditionalParts request)
{
var user = request.UserId.HasValue ? _userManager.GetUserById(request.UserId.Value) : null;
var item = string.IsNullOrEmpty(request.Id)
? (request.UserId.HasValue
? user.RootFolder
: _libraryManager.RootFolder)
2014-04-25 20:15:50 +00:00
: _libraryManager.GetItemById(request.Id);
// Get everything
var fields = Enum.GetNames(typeof(ItemFields))
.Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
.ToList();
var video = (Video)item;
var items = video.GetAdditionalParts()
.Select(i => _dtoService.GetBaseItemDto(i, fields, user, video))
.ToArray();
var result = new ItemsResult
{
Items = items,
TotalRecordCount = items.Length
};
return ToOptimizedSerializedResultUsingCache(result);
}
2014-08-18 14:20:02 +00:00
public void Delete(DeleteAlternateSources request)
{
var task = DeleteAsync(request);
Task.WaitAll(task);
}
public async Task DeleteAsync(DeleteAlternateSources request)
{
2014-04-25 20:15:50 +00:00
var video = (Video)_libraryManager.GetItemById(request.Id);
2014-03-19 01:35:40 +00:00
foreach (var link in video.GetLinkedAlternateVersions())
{
link.PrimaryVersionId = null;
2014-04-08 04:17:18 +00:00
await link.UpdateToRepository(ItemUpdateType.MetadataDownload, CancellationToken.None).ConfigureAwait(false);
2014-03-19 01:35:40 +00:00
}
video.LinkedAlternateVersions.Clear();
2014-04-08 04:17:18 +00:00
await video.UpdateToRepository(ItemUpdateType.MetadataDownload, CancellationToken.None).ConfigureAwait(false);
2014-03-19 01:35:40 +00:00
}
2014-08-18 14:20:02 +00:00
public void Post(MergeVersions request)
{
var task = PostAsync(request);
Task.WaitAll(task);
}
public async Task PostAsync(MergeVersions request)
{
2014-03-19 01:35:40 +00:00
var items = request.Ids.Split(',')
.Select(i => new Guid(i))
.Select(i => _libraryManager.GetItemById(i))
.ToList();
2014-03-19 01:35:40 +00:00
if (items.Count < 2)
{
throw new ArgumentException("Please supply at least two videos to merge.");
}
2014-03-19 04:59:45 +00:00
if (items.Any(i => !(i is Video)))
{
throw new ArgumentException("Only videos can be grouped together.");
}
var videos = items.Cast<Video>().ToList();
2014-03-22 16:16:43 +00:00
var videosWithVersions = videos.Where(i => i.MediaSourceCount > 1)
2014-03-19 01:35:40 +00:00
.ToList();
if (videosWithVersions.Count > 1)
{
2014-03-19 01:35:40 +00:00
throw new ArgumentException("Videos with sub-versions cannot be merged.");
}
2014-03-19 01:35:40 +00:00
var primaryVersion = videosWithVersions.FirstOrDefault();
2014-03-19 01:35:40 +00:00
if (primaryVersion == null)
{
2014-03-22 16:16:43 +00:00
primaryVersion = videos.OrderBy(i =>
{
if (i.Video3DFormat.HasValue)
{
return 1;
}
if (i.VideoType != Model.Entities.VideoType.VideoFile)
{
return 1;
}
return 0;
})
.ThenByDescending(i =>
2014-08-17 05:38:13 +00:00
{
var stream = i.GetDefaultVideoStream();
2014-08-17 05:38:13 +00:00
return stream == null || stream.Width == null ? 0 : stream.Width.Value;
2014-08-17 05:38:13 +00:00
}).First();
}
2014-03-19 04:59:45 +00:00
foreach (var item in videos.Where(i => i.Id != primaryVersion.Id))
2014-03-19 01:35:40 +00:00
{
item.PrimaryVersionId = primaryVersion.Id;
2014-04-08 04:17:18 +00:00
await item.UpdateToRepository(ItemUpdateType.MetadataDownload, CancellationToken.None).ConfigureAwait(false);
2014-03-19 01:35:40 +00:00
primaryVersion.LinkedAlternateVersions.Add(new LinkedChild
{
Path = item.Path,
ItemId = item.Id
});
}
2014-04-08 04:17:18 +00:00
await primaryVersion.UpdateToRepository(ItemUpdateType.MetadataDownload, CancellationToken.None).ConfigureAwait(false);
}
}
}