using MediaBrowser.Controller.Configuration; using MediaBrowser.Controller.Dto; using MediaBrowser.Controller.Entities; using MediaBrowser.Controller.Library; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Persistence; using MediaBrowser.Model.Querying; using System; using System.Linq; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Controller.IO; using MediaBrowser.Model.IO; using MediaBrowser.Model.Dto; using MediaBrowser.Model.Services; namespace MediaBrowser.Api { [Route("/Videos/{Id}/AdditionalParts", "GET", Summary = "Gets additional parts for a video.")] [Authenticated] public class GetAdditionalParts : IReturn> { [ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")] public string UserId { get; set; } /// /// 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("/Videos/{Id}/AlternateSources", "DELETE", Summary = "Removes alternate video sources.")] [Authenticated(Roles = "Admin")] public class DeleteAlternateSources : IReturnVoid { [ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")] public string Id { get; set; } } [Route("/Videos/MergeVersions", "POST", Summary = "Merges videos into a single record")] [Authenticated(Roles = "Admin")] 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; } } public class VideosService : BaseApiService { private readonly ILibraryManager _libraryManager; private readonly IUserManager _userManager; private readonly IDtoService _dtoService; private readonly IFileSystem _fileSystem; private readonly IItemRepository _itemRepo; private readonly IServerConfigurationManager _config; private readonly IAuthorizationContext _authContext; public VideosService(ILibraryManager libraryManager, IUserManager userManager, IDtoService dtoService, IItemRepository itemRepo, IFileSystem fileSystem, IServerConfigurationManager config, IAuthorizationContext authContext) { _libraryManager = libraryManager; _userManager = userManager; _dtoService = dtoService; _itemRepo = itemRepo; _fileSystem = fileSystem; _config = config; _authContext = authContext; } /// /// Gets the specified request. /// /// The request. /// System.Object. public object Get(GetAdditionalParts request) { var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null; var item = string.IsNullOrEmpty(request.Id) ? (!string.IsNullOrWhiteSpace(request.UserId) ? user.RootFolder : _libraryManager.RootFolder) : _libraryManager.GetItemById(request.Id); var dtoOptions = GetDtoOptions(_authContext, request); var video = item as Video; BaseItemDto[] items; if (video != null) { items = video.GetAdditionalParts() .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, video)) .ToArray(); } else { items = new BaseItemDto[] { }; } var result = new QueryResult { Items = items, TotalRecordCount = items.Length }; return ToOptimizedSerializedResultUsingCache(result); } public void Delete(DeleteAlternateSources request) { var task = DeleteAsync(request); Task.WaitAll(task); } public async Task DeleteAsync(DeleteAlternateSources request) { var video = (Video)_libraryManager.GetItemById(request.Id); foreach (var link in video.GetLinkedAlternateVersions()) { link.PrimaryVersionId = null; await link.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false); } video.LinkedAlternateVersions = Video.EmptyLinkedChildArray; await video.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None).ConfigureAwait(false); } public void Post(MergeVersions request) { var task = PostAsync(request); Task.WaitAll(task); } public async Task PostAsync(MergeVersions request) { var items = request.Ids.Split(',') .Select(i => new Guid(i)) .Select(i => _libraryManager.GetItemById(i)) .OfType