2016-03-27 14:11:27 -07:00
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-03-18 21:59:45 -07:00
|
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-06-12 14:46:50 -07:00
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-02 11:34:08 -07:00
|
|
|
|
using MediaBrowser.Controller.Net;
|
2014-03-18 21:59:45 -07:00
|
|
|
|
using MediaBrowser.Controller.Persistence;
|
2013-06-12 14:46:50 -07:00
|
|
|
|
using MediaBrowser.Model.Querying;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2014-03-18 18:35:40 -07:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-05-25 23:48:54 -07:00
|
|
|
|
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Controller.IO;
|
|
|
|
|
using MediaBrowser.Model.IO;
|
2016-08-05 21:38:01 -07:00
|
|
|
|
using MediaBrowser.Model.Dto;
|
2016-10-25 12:02:04 -07:00
|
|
|
|
using MediaBrowser.Model.Services;
|
2013-06-12 14:46:50 -07:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
2014-03-21 20:35:03 -07:00
|
|
|
|
[Route("/Videos/{Id}/AdditionalParts", "GET", Summary = "Gets additional parts for a video.")]
|
2014-11-14 19:31:03 -07:00
|
|
|
|
[Authenticated]
|
2017-08-19 12:43:35 -07:00
|
|
|
|
public class GetAdditionalParts : IReturn<QueryResult<BaseItemDto>>
|
2013-06-12 14:46:50 -07:00
|
|
|
|
{
|
|
|
|
|
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
|
2015-05-29 16:51:33 -07:00
|
|
|
|
public string UserId { get; set; }
|
2013-06-12 14:46:50 -07:00
|
|
|
|
|
|
|
|
|
/// <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-15 15:52:43 -07:00
|
|
|
|
|
2014-03-22 09:16:43 -07:00
|
|
|
|
[Route("/Videos/{Id}/AlternateSources", "DELETE", Summary = "Removes alternate video sources.")]
|
2014-11-14 19:31:03 -07:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2014-03-22 09:16:43 -07:00
|
|
|
|
public class DeleteAlternateSources : IReturnVoid
|
2014-03-15 21:23:58 -07:00
|
|
|
|
{
|
2014-03-18 18:35:40 -07:00
|
|
|
|
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
2014-03-15 21:23:58 -07:00
|
|
|
|
public string Id { get; set; }
|
2014-03-18 18:35:40 -07:00
|
|
|
|
}
|
2014-03-16 12:39:58 -07:00
|
|
|
|
|
2014-03-21 20:35:03 -07:00
|
|
|
|
[Route("/Videos/MergeVersions", "POST", Summary = "Merges videos into a single record")]
|
2014-11-14 19:31:03 -07:00
|
|
|
|
[Authenticated(Roles = "Admin")]
|
2014-03-18 18:35:40 -07: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-03-15 21:23:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2013-06-12 14:46:50 -07:00
|
|
|
|
public class VideosService : BaseApiService
|
|
|
|
|
{
|
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
|
private readonly IUserManager _userManager;
|
2013-09-04 10:02:19 -07:00
|
|
|
|
private readonly IDtoService _dtoService;
|
2014-03-18 21:59:45 -07:00
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly IItemRepository _itemRepo;
|
|
|
|
|
private readonly IServerConfigurationManager _config;
|
2016-11-10 07:41:24 -07:00
|
|
|
|
private readonly IAuthorizationContext _authContext;
|
2013-06-12 14:46:50 -07:00
|
|
|
|
|
2016-11-10 07:41:24 -07:00
|
|
|
|
public VideosService(ILibraryManager libraryManager, IUserManager userManager, IDtoService dtoService, IItemRepository itemRepo, IFileSystem fileSystem, IServerConfigurationManager config, IAuthorizationContext authContext)
|
2013-06-12 14:46:50 -07:00
|
|
|
|
{
|
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
|
_userManager = userManager;
|
2013-09-04 10:02:19 -07:00
|
|
|
|
_dtoService = dtoService;
|
2014-03-18 21:59:45 -07:00
|
|
|
|
_itemRepo = itemRepo;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_config = config;
|
2016-11-10 07:41:24 -07:00
|
|
|
|
_authContext = authContext;
|
2013-06-12 14:46:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the specified request.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
|
public object Get(GetAdditionalParts request)
|
|
|
|
|
{
|
2015-05-29 16:51:33 -07:00
|
|
|
|
var user = !string.IsNullOrWhiteSpace(request.UserId) ? _userManager.GetUserById(request.UserId) : null;
|
2013-06-12 14:46:50 -07:00
|
|
|
|
|
|
|
|
|
var item = string.IsNullOrEmpty(request.Id)
|
2015-05-29 16:51:33 -07:00
|
|
|
|
? (!string.IsNullOrWhiteSpace(request.UserId)
|
2013-06-12 14:46:50 -07:00
|
|
|
|
? user.RootFolder
|
2014-03-15 15:52:43 -07:00
|
|
|
|
: _libraryManager.RootFolder)
|
2014-04-25 13:15:50 -07:00
|
|
|
|
: _libraryManager.GetItemById(request.Id);
|
2014-03-15 15:52:43 -07:00
|
|
|
|
|
2016-11-10 07:41:24 -07:00
|
|
|
|
var dtoOptions = GetDtoOptions(_authContext, request);
|
2014-03-15 15:52:43 -07:00
|
|
|
|
|
2016-08-05 21:38:01 -07:00
|
|
|
|
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[] { };
|
|
|
|
|
}
|
2014-03-15 15:52:43 -07:00
|
|
|
|
|
2017-08-19 12:43:35 -07:00
|
|
|
|
var result = new QueryResult<BaseItemDto>
|
2014-03-15 15:52:43 -07:00
|
|
|
|
{
|
|
|
|
|
Items = items,
|
|
|
|
|
TotalRecordCount = items.Length
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ToOptimizedSerializedResultUsingCache(result);
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 07:20:02 -07:00
|
|
|
|
public void Delete(DeleteAlternateSources request)
|
2014-03-15 21:23:58 -07:00
|
|
|
|
{
|
2014-04-25 13:15:50 -07:00
|
|
|
|
var video = (Video)_libraryManager.GetItemById(request.Id);
|
2014-03-18 18:35:40 -07:00
|
|
|
|
|
|
|
|
|
foreach (var link in video.GetLinkedAlternateVersions())
|
|
|
|
|
{
|
|
|
|
|
link.PrimaryVersionId = null;
|
|
|
|
|
|
2017-10-03 11:39:37 -07:00
|
|
|
|
link.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-18 18:35:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 11:01:31 -07:00
|
|
|
|
video.LinkedAlternateVersions = Video.EmptyLinkedChildArray;
|
2017-10-03 11:39:37 -07:00
|
|
|
|
video.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-18 18:35:40 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-18 07:20:02 -07:00
|
|
|
|
public void Post(MergeVersions request)
|
2014-03-15 21:23:58 -07:00
|
|
|
|
{
|
2014-03-18 18:35:40 -07:00
|
|
|
|
var items = request.Ids.Split(',')
|
|
|
|
|
.Select(i => _libraryManager.GetItemById(i))
|
2016-05-01 15:11:24 -07:00
|
|
|
|
.OfType<Video>()
|
2014-03-18 18:35:40 -07:00
|
|
|
|
.ToList();
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2014-03-18 18:35:40 -07:00
|
|
|
|
if (items.Count < 2)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException("Please supply at least two videos to merge.");
|
|
|
|
|
}
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2016-05-01 15:11:24 -07:00
|
|
|
|
var videosWithVersions = items.Where(i => i.MediaSourceCount > 1)
|
2014-03-18 18:35:40 -07:00
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
if (videosWithVersions.Count > 1)
|
2014-03-15 21:23:58 -07:00
|
|
|
|
{
|
2014-03-18 18:35:40 -07:00
|
|
|
|
throw new ArgumentException("Videos with sub-versions cannot be merged.");
|
|
|
|
|
}
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2014-03-18 18:35:40 -07:00
|
|
|
|
var primaryVersion = videosWithVersions.FirstOrDefault();
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2014-03-18 18:35:40 -07:00
|
|
|
|
if (primaryVersion == null)
|
|
|
|
|
{
|
2016-05-01 15:11:24 -07:00
|
|
|
|
primaryVersion = items.OrderBy(i =>
|
2014-03-22 09:16:43 -07:00
|
|
|
|
{
|
2017-10-03 11:39:37 -07:00
|
|
|
|
if (i.Video3DFormat.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i.VideoType != Model.Entities.VideoType.VideoFile)
|
|
|
|
|
{
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
})
|
2014-03-22 09:16:43 -07:00
|
|
|
|
.ThenByDescending(i =>
|
2014-08-16 22:38:13 -07:00
|
|
|
|
{
|
|
|
|
|
var stream = i.GetDefaultVideoStream();
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2014-08-16 22:38:13 -07:00
|
|
|
|
return stream == null || stream.Width == null ? 0 : stream.Width.Value;
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2014-08-16 22:38:13 -07:00
|
|
|
|
}).First();
|
2014-03-15 21:23:58 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-08-10 11:01:31 -07:00
|
|
|
|
var list = primaryVersion.LinkedAlternateVersions.ToList();
|
|
|
|
|
|
2016-05-01 15:11:24 -07:00
|
|
|
|
foreach (var item in items.Where(i => i.Id != primaryVersion.Id))
|
2014-03-18 18:35:40 -07:00
|
|
|
|
{
|
2016-05-05 21:50:39 -07:00
|
|
|
|
item.PrimaryVersionId = primaryVersion.Id.ToString("N");
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2017-10-03 11:39:37 -07:00
|
|
|
|
item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2017-08-10 11:01:31 -07:00
|
|
|
|
list.Add(new LinkedChild
|
2014-03-18 18:35:40 -07:00
|
|
|
|
{
|
|
|
|
|
Path = item.Path,
|
|
|
|
|
ItemId = item.Id
|
|
|
|
|
});
|
|
|
|
|
}
|
2014-03-15 21:23:58 -07:00
|
|
|
|
|
2017-08-10 11:01:31 -07:00
|
|
|
|
primaryVersion.LinkedAlternateVersions = list.ToArray();
|
|
|
|
|
|
2017-10-03 11:39:37 -07:00
|
|
|
|
primaryVersion.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-15 21:23:58 -07:00
|
|
|
|
}
|
2013-06-12 14:46:50 -07:00
|
|
|
|
}
|
|
|
|
|
}
|