2019-01-13 20:01:16 +00:00
|
|
|
using System;
|
2019-02-28 22:22:57 +00:00
|
|
|
using System.Globalization;
|
2019-01-13 19:24:58 +00:00
|
|
|
using System.Linq;
|
|
|
|
using System.Threading;
|
|
|
|
using MediaBrowser.Controller.Configuration;
|
2014-03-19 04:59:45 +00:00
|
|
|
using MediaBrowser.Controller.Dto;
|
2013-06-12 21:46:50 +00:00
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2014-07-02 18:34:08 +00:00
|
|
|
using MediaBrowser.Controller.Net;
|
2016-08-06 04:38:01 +00:00
|
|
|
using MediaBrowser.Model.Dto;
|
2019-01-13 19:24:58 +00:00
|
|
|
using MediaBrowser.Model.Querying;
|
2016-10-25 19:02:04 +00:00
|
|
|
using MediaBrowser.Model.Services;
|
2019-11-17 22:05:39 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-06-12 21:46:50 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
{
|
2014-03-22 03:35:03 +00:00
|
|
|
[Route("/Videos/{Id}/AdditionalParts", "GET", Summary = "Gets additional parts for a video.")]
|
2014-11-15 02:31:03 +00:00
|
|
|
[Authenticated]
|
2017-08-19 19:43:35 +00:00
|
|
|
public class GetAdditionalParts : IReturn<QueryResult<BaseItemDto>>
|
2013-06-12 21:46:50 +00:00
|
|
|
{
|
|
|
|
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
|
2018-09-12 17:26:21 +00:00
|
|
|
public Guid UserId { get; set; }
|
2013-06-12 21:46:50 +00: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 22:52:43 +00:00
|
|
|
|
2014-03-22 16:16:43 +00:00
|
|
|
[Route("/Videos/{Id}/AlternateSources", "DELETE", Summary = "Removes alternate video sources.")]
|
2014-11-15 02:31:03 +00:00
|
|
|
[Authenticated(Roles = "Admin")]
|
2014-03-22 16:16:43 +00:00
|
|
|
public class DeleteAlternateSources : IReturnVoid
|
2014-03-16 04:23:58 +00:00
|
|
|
{
|
2014-03-19 01:35:40 +00:00
|
|
|
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
|
2014-03-16 04:23:58 +00:00
|
|
|
public string Id { get; set; }
|
2014-03-19 01:35:40 +00:00
|
|
|
}
|
2014-03-16 19:39:58 +00:00
|
|
|
|
2014-03-22 03:35:03 +00:00
|
|
|
[Route("/Videos/MergeVersions", "POST", Summary = "Merges videos into a single record")]
|
2014-11-15 02:31:03 +00:00
|
|
|
[Authenticated(Roles = "Admin")]
|
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-03-16 04:23:58 +00:00
|
|
|
}
|
|
|
|
|
2013-06-12 21:46:50 +00:00
|
|
|
public class VideosService : BaseApiService
|
|
|
|
{
|
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly IUserManager _userManager;
|
2013-09-04 17:02:19 +00:00
|
|
|
private readonly IDtoService _dtoService;
|
2016-11-10 14:41:24 +00:00
|
|
|
private readonly IAuthorizationContext _authContext;
|
2013-06-12 21:46:50 +00:00
|
|
|
|
2019-11-17 22:05:39 +00:00
|
|
|
public VideosService(
|
|
|
|
ILogger<VideosService> logger,
|
|
|
|
IServerConfigurationManager serverConfigurationManager,
|
|
|
|
IHttpResultFactory httpResultFactory,
|
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IUserManager userManager,
|
|
|
|
IDtoService dtoService,
|
|
|
|
IAuthorizationContext authContext)
|
|
|
|
: base(logger, serverConfigurationManager, httpResultFactory)
|
2013-06-12 21:46:50 +00:00
|
|
|
{
|
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_userManager = userManager;
|
2013-09-04 17:02:19 +00:00
|
|
|
_dtoService = dtoService;
|
2016-11-10 14:41:24 +00:00
|
|
|
_authContext = authContext;
|
2013-06-12 21:46:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the specified request.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">The request.</param>
|
|
|
|
/// <returns>System.Object.</returns>
|
|
|
|
public object Get(GetAdditionalParts request)
|
|
|
|
{
|
2018-09-12 17:26:21 +00:00
|
|
|
var user = !request.UserId.Equals(Guid.Empty) ? _userManager.GetUserById(request.UserId) : null;
|
2013-06-12 21:46:50 +00:00
|
|
|
|
|
|
|
var item = string.IsNullOrEmpty(request.Id)
|
2018-09-12 17:26:21 +00:00
|
|
|
? (!request.UserId.Equals(Guid.Empty)
|
|
|
|
? _libraryManager.GetUserRootFolder()
|
2014-03-15 22:52:43 +00:00
|
|
|
: _libraryManager.RootFolder)
|
2014-04-25 20:15:50 +00:00
|
|
|
: _libraryManager.GetItemById(request.Id);
|
2014-03-15 22:52:43 +00:00
|
|
|
|
2016-11-10 14:41:24 +00:00
|
|
|
var dtoOptions = GetDtoOptions(_authContext, request);
|
2014-03-15 22:52:43 +00:00
|
|
|
|
2016-08-06 04:38:01 +00:00
|
|
|
BaseItemDto[] items;
|
2019-11-17 22:05:39 +00:00
|
|
|
if (item is Video video)
|
2016-08-06 04:38:01 +00:00
|
|
|
{
|
|
|
|
items = video.GetAdditionalParts()
|
|
|
|
.Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, video))
|
|
|
|
.ToArray();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-17 22:05:39 +00:00
|
|
|
items = Array.Empty<BaseItemDto>();
|
2016-08-06 04:38:01 +00:00
|
|
|
}
|
2014-03-15 22:52:43 +00:00
|
|
|
|
2017-08-19 19:43:35 +00:00
|
|
|
var result = new QueryResult<BaseItemDto>
|
2014-03-15 22:52:43 +00:00
|
|
|
{
|
|
|
|
Items = items,
|
|
|
|
TotalRecordCount = items.Length
|
|
|
|
};
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
return ToOptimizedResult(result);
|
2014-03-15 22:52:43 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 14:20:02 +00:00
|
|
|
public void Delete(DeleteAlternateSources request)
|
2014-03-16 04:23:58 +00:00
|
|
|
{
|
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())
|
|
|
|
{
|
2017-11-17 21:54:15 +00:00
|
|
|
link.SetPrimaryVersionId(null);
|
2018-09-12 17:26:21 +00:00
|
|
|
link.LinkedAlternateVersions = Array.Empty<LinkedChild>();
|
2014-03-19 01:35:40 +00:00
|
|
|
|
2017-10-03 18:39:37 +00:00
|
|
|
link.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-19 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2018-09-12 17:26:21 +00:00
|
|
|
video.LinkedAlternateVersions = Array.Empty<LinkedChild>();
|
2017-11-17 21:54:15 +00:00
|
|
|
video.SetPrimaryVersionId(null);
|
2017-10-03 18:39:37 +00:00
|
|
|
video.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-19 01:35:40 +00:00
|
|
|
}
|
|
|
|
|
2014-08-18 14:20:02 +00:00
|
|
|
public void Post(MergeVersions request)
|
2014-03-16 04:23:58 +00:00
|
|
|
{
|
2014-03-19 01:35:40 +00:00
|
|
|
var items = request.Ids.Split(',')
|
|
|
|
.Select(i => _libraryManager.GetItemById(i))
|
2016-05-01 22:11:24 +00:00
|
|
|
.OfType<Video>()
|
2020-05-31 18:35:09 +00:00
|
|
|
.OrderBy(i => i.Id)
|
2014-03-19 01:35:40 +00:00
|
|
|
.ToList();
|
2014-03-16 04:23:58 +00:00
|
|
|
|
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-16 04:23:58 +00:00
|
|
|
|
2016-05-01 22:11:24 +00:00
|
|
|
var videosWithVersions = items.Where(i => i.MediaSourceCount > 1)
|
2014-03-19 01:35:40 +00:00
|
|
|
.ToList();
|
|
|
|
|
2020-04-05 21:58:39 +00:00
|
|
|
var primaryVersion = videosWithVersions.FirstOrDefault();
|
|
|
|
if (primaryVersion == null)
|
|
|
|
{
|
|
|
|
primaryVersion = items.OrderBy(i =>
|
|
|
|
{
|
|
|
|
if (i.Video3DFormat.HasValue || i.VideoType != Model.Entities.VideoType.VideoFile)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.ThenByDescending(i =>
|
|
|
|
{
|
|
|
|
return i.GetDefaultVideoStream()?.Width ?? 0;
|
|
|
|
}).First();
|
|
|
|
}
|
2014-03-16 04:23:58 +00:00
|
|
|
|
2017-08-10 18:01:31 +00:00
|
|
|
var list = primaryVersion.LinkedAlternateVersions.ToList();
|
|
|
|
|
2016-05-01 22:11:24 +00:00
|
|
|
foreach (var item in items.Where(i => i.Id != primaryVersion.Id))
|
2014-03-19 01:35:40 +00:00
|
|
|
{
|
2019-02-28 22:22:57 +00:00
|
|
|
item.SetPrimaryVersionId(primaryVersion.Id.ToString("N", CultureInfo.InvariantCulture));
|
2014-03-16 04:23:58 +00:00
|
|
|
|
2017-10-03 18:39:37 +00:00
|
|
|
item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-16 04:23:58 +00:00
|
|
|
|
2017-08-10 18:01:31 +00:00
|
|
|
list.Add(new LinkedChild
|
2014-03-19 01:35:40 +00:00
|
|
|
{
|
|
|
|
Path = item.Path,
|
|
|
|
ItemId = item.Id
|
|
|
|
});
|
2018-09-12 17:26:21 +00:00
|
|
|
|
|
|
|
foreach (var linkedItem in item.LinkedAlternateVersions)
|
|
|
|
{
|
|
|
|
if (!list.Any(i => string.Equals(i.Path, linkedItem.Path, StringComparison.OrdinalIgnoreCase)))
|
|
|
|
{
|
|
|
|
list.Add(linkedItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.LinkedAlternateVersions.Length > 0)
|
|
|
|
{
|
|
|
|
item.LinkedAlternateVersions = Array.Empty<LinkedChild>();
|
|
|
|
item.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
|
|
|
}
|
2014-03-19 01:35:40 +00:00
|
|
|
}
|
2014-03-16 04:23:58 +00:00
|
|
|
|
2017-08-10 18:01:31 +00:00
|
|
|
primaryVersion.LinkedAlternateVersions = list.ToArray();
|
2017-10-03 18:39:37 +00:00
|
|
|
primaryVersion.UpdateToRepository(ItemUpdateType.MetadataEdit, CancellationToken.None);
|
2014-03-16 04:23:58 +00:00
|
|
|
}
|
2013-06-12 21:46:50 +00:00
|
|
|
}
|
|
|
|
}
|