aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Api/VideosService.cs
diff options
context:
space:
mode:
authorLuke Pulverenti <luke.pulverenti@gmail.com>2014-03-19 00:59:45 -0400
committerLuke Pulverenti <luke.pulverenti@gmail.com>2014-03-19 00:59:45 -0400
commit683d8455c6ce75bcefc8749fe5a5d15d90a28625 (patch)
treeecae2ce3365c839d20756545c83f4cf246f9c88c /MediaBrowser.Api/VideosService.cs
parent94f5ba6ce6cc46c2918dbb74c3bbb7d5ee554699 (diff)
#712 - Group multiple versions
Diffstat (limited to 'MediaBrowser.Api/VideosService.cs')
-rw-r--r--MediaBrowser.Api/VideosService.cs180
1 files changed, 151 insertions, 29 deletions
diff --git a/MediaBrowser.Api/VideosService.cs b/MediaBrowser.Api/VideosService.cs
index 9dfeb216e..7d7baa576 100644
--- a/MediaBrowser.Api/VideosService.cs
+++ b/MediaBrowser.Api/VideosService.cs
@@ -1,9 +1,15 @@
-using MediaBrowser.Controller.Dto;
+using MediaBrowser.Common.IO;
+using MediaBrowser.Controller.Configuration;
+using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
+using MediaBrowser.Controller.Persistence;
+using MediaBrowser.Model.Dto;
+using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Querying;
using ServiceStack;
using System;
+using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
@@ -27,11 +33,8 @@ namespace MediaBrowser.Api
[Route("/Videos/{Id}/AlternateVersions", "GET")]
[Api(Description = "Gets alternate versions of a video.")]
- public class GetAlternateVersions : IReturn<ItemsResult>
+ public class GetAlternateVersions : IReturn<List<AlternateVersionInfo>>
{
- [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>
@@ -61,12 +64,18 @@ namespace MediaBrowser.Api
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IDtoService _dtoService;
+ private readonly IFileSystem _fileSystem;
+ private readonly IItemRepository _itemRepo;
+ private readonly IServerConfigurationManager _config;
- public VideosService(ILibraryManager libraryManager, IUserManager userManager, IDtoService dtoService)
+ public VideosService(ILibraryManager libraryManager, IUserManager userManager, IDtoService dtoService, IItemRepository itemRepo, IFileSystem fileSystem, IServerConfigurationManager config)
{
_libraryManager = libraryManager;
_userManager = userManager;
_dtoService = dtoService;
+ _itemRepo = itemRepo;
+ _fileSystem = fileSystem;
+ _config = config;
}
/// <summary>
@@ -106,34 +115,141 @@ namespace MediaBrowser.Api
public object Get(GetAlternateVersions 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)
- : _dtoService.GetItemByDtoId(request.Id, request.UserId);
-
- // Get everything
- var fields = Enum.GetNames(typeof(ItemFields))
- .Select(i => (ItemFields)Enum.Parse(typeof(ItemFields), i, true))
- .ToList();
+ var item = _libraryManager.GetItemById(new Guid(request.Id));
var video = (Video)item;
- var items = video.GetAlternateVersions()
- .Select(i => _dtoService.GetBaseItemDto(i, fields, user, video))
- .ToArray();
+ var items = video.GetAlternateVersions();
- var result = new ItemsResult
+ var result = items.Select(i => new AlternateVersionInfo
{
- Items = items,
- TotalRecordCount = items.Length
- };
+ Chapters = _itemRepo.GetChapters(i.Id).Select(c => _dtoService.GetChapterInfoDto(c, i)).ToList(),
+
+ Id = i.Id.ToString("N"),
+ IsoType = i.IsoType,
+ LocationType = i.LocationType,
+ MediaStreams = _itemRepo.GetMediaStreams(new MediaStreamQuery { ItemId = i.Id }).ToList(),
+ Name = GetAlternateVersionName(i),
+ Path = GetMappedPath(i),
+ RunTimeTicks = i.RunTimeTicks,
+ Video3DFormat = i.Video3DFormat,
+ VideoType = i.VideoType,
+ IsHD = i.IsHD
+
+ }).ToList();
return ToOptimizedSerializedResultUsingCache(result);
}
+ private string GetMappedPath(Video video)
+ {
+ var path = video.Path;
+
+ var locationType = video.LocationType;
+
+ if (locationType != LocationType.FileSystem && locationType != LocationType.Offline)
+ {
+ return path;
+ }
+
+ foreach (var map in _config.Configuration.PathSubstitutions)
+ {
+ path = _fileSystem.SubstitutePath(path, map.From, map.To);
+ }
+
+ return path;
+ }
+
+ private string GetAlternateVersionName(Video video)
+ {
+ var name = "";
+
+ var stream = video.GetDefaultVideoStream();
+
+ if (video.Video3DFormat.HasValue)
+ {
+ name = "3D " + name;
+ name = name.Trim();
+ }
+
+ if (video.VideoType == VideoType.BluRay)
+ {
+ name = name + " " + "Bluray";
+ name = name.Trim();
+ }
+ else if (video.VideoType == VideoType.Dvd)
+ {
+ name = name + " " + "DVD";
+ name = name.Trim();
+ }
+ else if (video.VideoType == VideoType.HdDvd)
+ {
+ name = name + " " + "HD-DVD";
+ name = name.Trim();
+ }
+ else if (video.VideoType == VideoType.Iso)
+ {
+ if (video.IsoType.HasValue)
+ {
+ if (video.IsoType.Value == IsoType.BluRay)
+ {
+ name = name + " " + "Bluray";
+ }
+ else if (video.IsoType.Value == IsoType.Dvd)
+ {
+ name = name + " " + "DVD";
+ }
+ }
+ else
+ {
+ name = name + " " + "ISO";
+ }
+ name = name.Trim();
+ }
+ else if (video.VideoType == VideoType.VideoFile)
+ {
+ if (stream != null)
+ {
+ if (stream.Width.HasValue)
+ {
+ if (stream.Width.Value >= 1900)
+ {
+ name = name + " " + "1080P";
+ name = name.Trim();
+ }
+ else if (stream.Width.Value >= 1270)
+ {
+ name = name + " " + "720P";
+ name = name.Trim();
+ }
+ else if (stream.Width.Value >= 700)
+ {
+ name = name + " " + "480p";
+ name = name.Trim();
+ }
+ else
+ {
+ name = name + " " + "SD";
+ name = name.Trim();
+ }
+ }
+ }
+ }
+
+ if (stream != null && !string.IsNullOrWhiteSpace(stream.Codec))
+ {
+ name = name + " " + stream.Codec.ToUpper();
+ name = name.Trim();
+ }
+
+ if (string.IsNullOrWhiteSpace(name))
+ {
+ return video.Name;
+ }
+
+ return name;
+ }
+
public void Delete(DeleteAlternateVersions request)
{
var task = RemoveAlternateVersions(request);
@@ -168,7 +284,6 @@ namespace MediaBrowser.Api
var items = request.Ids.Split(',')
.Select(i => new Guid(i))
.Select(i => _libraryManager.GetItemById(i))
- .Cast<Video>()
.ToList();
if (items.Count < 2)
@@ -176,7 +291,14 @@ namespace MediaBrowser.Api
throw new ArgumentException("Please supply at least two videos to merge.");
}
- var videosWithVersions = items.Where(i => i.AlternateVersionCount > 0)
+ if (items.Any(i => !(i is Video)))
+ {
+ throw new ArgumentException("Only videos can be grouped together.");
+ }
+
+ var videos = items.Cast<Video>().ToList();
+
+ var videosWithVersions = videos.Where(i => i.AlternateVersionCount > 0)
.ToList();
if (videosWithVersions.Count > 1)
@@ -188,7 +310,7 @@ namespace MediaBrowser.Api
if (primaryVersion == null)
{
- primaryVersion = items.OrderByDescending(i =>
+ primaryVersion = videos.OrderByDescending(i =>
{
var stream = i.GetDefaultVideoStream();
@@ -198,7 +320,7 @@ namespace MediaBrowser.Api
.First();
}
- foreach (var item in items.Where(i => i.Id != primaryVersion.Id))
+ foreach (var item in videos.Where(i => i.Id != primaryVersion.Id))
{
item.PrimaryVersionId = primaryVersion.Id;