2012-07-12 20:50:50 -07:00
|
|
|
|
using System;
|
2012-08-10 20:13:56 -07:00
|
|
|
|
using System.IO;
|
2012-07-19 19:22:44 -07:00
|
|
|
|
using System.Linq;
|
2012-08-10 20:13:56 -07:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2012-07-12 20:50:50 -07:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-08-17 06:16:50 -07:00
|
|
|
|
using MediaBrowser.Model.DTO;
|
2012-07-12 20:50:50 -07:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
2012-07-19 19:22:44 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains some helpers for the api
|
|
|
|
|
/// </summary>
|
2012-07-12 20:50:50 -07:00
|
|
|
|
public static class ApiService
|
|
|
|
|
{
|
|
|
|
|
public static BaseItem GetItemById(string id)
|
|
|
|
|
{
|
2012-07-16 09:50:44 -07:00
|
|
|
|
Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
|
2012-07-12 20:50:50 -07:00
|
|
|
|
|
2012-07-16 09:50:44 -07:00
|
|
|
|
return Kernel.Instance.GetItemById(guid);
|
2012-07-12 20:50:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
public static DTOBaseItem GetDTOBaseItem(BaseItem item, User user,
|
|
|
|
|
bool includeChildren = true,
|
|
|
|
|
bool includePeople = true)
|
2012-07-12 20:50:50 -07:00
|
|
|
|
{
|
2012-08-18 01:22:54 -07:00
|
|
|
|
DTOBaseItem dto = new DTOBaseItem();
|
|
|
|
|
|
|
|
|
|
dto.AspectRatio = item.AspectRatio;
|
|
|
|
|
dto.BackdropCount = item.BackdropImagePaths == null ? 0 : item.BackdropImagePaths.Count();
|
|
|
|
|
dto.DateCreated = item.DateCreated;
|
|
|
|
|
dto.DisplayMediaType = item.DisplayMediaType;
|
|
|
|
|
dto.Genres = item.Genres;
|
|
|
|
|
dto.HasArt = !string.IsNullOrEmpty(item.ArtImagePath);
|
|
|
|
|
dto.HasBanner = !string.IsNullOrEmpty(item.BannerImagePath);
|
|
|
|
|
dto.HasLogo = !string.IsNullOrEmpty(item.LogoImagePath);
|
|
|
|
|
dto.HasPrimaryImage = !string.IsNullOrEmpty(item.LogoImagePath);
|
|
|
|
|
dto.HasThumb = !string.IsNullOrEmpty(item.ThumbnailImagePath);
|
|
|
|
|
dto.Id = item.Id;
|
|
|
|
|
dto.IndexNumber = item.IndexNumber;
|
|
|
|
|
dto.IsFolder = item is Folder;
|
|
|
|
|
dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
|
|
|
|
|
dto.Name = item.Name;
|
|
|
|
|
dto.OfficialRating = item.OfficialRating;
|
|
|
|
|
dto.Overview = item.Overview;
|
|
|
|
|
|
|
|
|
|
// If there are no backdrops, indicate what parent has them in case the UI wants to allow inheritance
|
|
|
|
|
if (dto.BackdropCount == 0)
|
2012-08-03 08:54:05 -07:00
|
|
|
|
{
|
|
|
|
|
int backdropCount;
|
2012-08-18 01:22:54 -07:00
|
|
|
|
dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
|
|
|
|
|
dto.ParentBackdropCount = backdropCount;
|
2012-08-03 08:54:05 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-01 10:20:22 -07:00
|
|
|
|
if (item.Parent != null)
|
|
|
|
|
{
|
2012-08-18 01:22:54 -07:00
|
|
|
|
dto.ParentId = item.Parent.Id;
|
2012-08-01 10:20:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
// If there is no logo, indicate what parent has one in case the UI wants to allow inheritance
|
|
|
|
|
if (!dto.HasLogo)
|
2012-07-16 09:50:44 -07:00
|
|
|
|
{
|
2012-08-18 01:22:54 -07:00
|
|
|
|
dto.ParentLogoItemId = GetParentLogoItemId(item);
|
|
|
|
|
}
|
2012-07-12 20:50:50 -07:00
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
dto.Path = item.Path;
|
2012-08-15 06:20:29 -07:00
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
dto.PremiereDate = item.PremiereDate;
|
|
|
|
|
dto.ProductionYear = item.ProductionYear;
|
|
|
|
|
dto.ProviderIds = item.ProviderIds;
|
|
|
|
|
dto.RunTimeTicks = item.RunTimeTicks;
|
|
|
|
|
dto.SortName = item.SortName;
|
|
|
|
|
dto.Taglines = item.Taglines;
|
|
|
|
|
dto.TrailerUrl = item.TrailerUrl;
|
|
|
|
|
dto.Type = item.GetType().Name;
|
|
|
|
|
dto.UserRating = item.UserRating;
|
2012-08-17 09:47:35 -07:00
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
dto.UserData = item.GetUserData(user);
|
2012-08-17 09:47:35 -07:00
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
AttachStudios(dto, item);
|
2012-08-17 09:47:35 -07:00
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
if (includeChildren)
|
|
|
|
|
{
|
|
|
|
|
AttachChildren(dto, item, user);
|
|
|
|
|
}
|
2012-08-17 09:47:35 -07:00
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
if (includePeople)
|
|
|
|
|
{
|
|
|
|
|
AttachPeople(dto, item);
|
2012-08-17 09:47:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
Folder folder = item as Folder;
|
|
|
|
|
|
|
|
|
|
if (folder != null)
|
|
|
|
|
{
|
|
|
|
|
dto.SpecialCounts = folder.GetSpecialCounts(user);
|
2012-08-18 09:45:11 -07:00
|
|
|
|
|
|
|
|
|
dto.IsRoot = folder.IsRoot;
|
2012-08-18 12:38:27 -07:00
|
|
|
|
dto.IsVirtualFolder = folder is VirtualFolder;
|
2012-08-18 01:22:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AttachStudios(DTOBaseItem dto, BaseItem item)
|
|
|
|
|
{
|
2012-08-17 09:47:35 -07:00
|
|
|
|
// Attach Studios by transforming them into BaseItemStudio (DTO)
|
|
|
|
|
if (item.Studios != null)
|
|
|
|
|
{
|
2012-08-18 01:22:54 -07:00
|
|
|
|
dto.Studios = item.Studios.Select(s =>
|
2012-08-17 09:47:35 -07:00
|
|
|
|
{
|
|
|
|
|
BaseItemStudio baseItemStudio = new BaseItemStudio();
|
|
|
|
|
|
|
|
|
|
baseItemStudio.Name = s;
|
|
|
|
|
|
|
|
|
|
Studio ibnObject = Kernel.Instance.ItemController.GetStudio(s);
|
|
|
|
|
|
|
|
|
|
if (ibnObject != null)
|
|
|
|
|
{
|
2012-08-18 01:22:54 -07:00
|
|
|
|
baseItemStudio.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
|
2012-08-17 09:47:35 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseItemStudio;
|
|
|
|
|
});
|
2012-07-16 09:50:44 -07:00
|
|
|
|
}
|
2012-08-18 01:22:54 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AttachChildren(DTOBaseItem dto, BaseItem item, User user)
|
|
|
|
|
{
|
|
|
|
|
var folder = item as Folder;
|
2012-07-16 09:50:44 -07:00
|
|
|
|
|
2012-08-18 01:22:54 -07:00
|
|
|
|
if (folder != null)
|
|
|
|
|
{
|
|
|
|
|
dto.Children = folder.GetParentalAllowedChildren(user).Select(c => GetDTOBaseItem(c, user, false, false));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dto.LocalTrailers = item.LocalTrailers;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static void AttachPeople(DTOBaseItem dto, BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
// Attach People by transforming them into BaseItemPerson (DTO)
|
|
|
|
|
if (item.People != null)
|
|
|
|
|
{
|
|
|
|
|
dto.People = item.People.Select(p =>
|
|
|
|
|
{
|
|
|
|
|
BaseItemPerson baseItemPerson = new BaseItemPerson();
|
|
|
|
|
|
|
|
|
|
baseItemPerson.PersonInfo = p;
|
|
|
|
|
|
|
|
|
|
Person ibnObject = Kernel.Instance.ItemController.GetPerson(p.Name);
|
|
|
|
|
|
|
|
|
|
if (ibnObject != null)
|
|
|
|
|
{
|
|
|
|
|
baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseItemPerson;
|
|
|
|
|
});
|
|
|
|
|
}
|
2012-07-12 20:50:50 -07:00
|
|
|
|
}
|
2012-08-01 10:20:22 -07:00
|
|
|
|
|
2012-08-03 08:54:05 -07:00
|
|
|
|
private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
|
2012-08-01 10:20:22 -07:00
|
|
|
|
{
|
2012-08-03 08:54:05 -07:00
|
|
|
|
backdropCount = 0;
|
|
|
|
|
|
|
|
|
|
var parent = item.Parent;
|
2012-08-01 10:20:22 -07:00
|
|
|
|
|
2012-08-03 08:54:05 -07:00
|
|
|
|
while (parent != null)
|
|
|
|
|
{
|
|
|
|
|
if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
|
2012-08-01 10:20:22 -07:00
|
|
|
|
{
|
2012-08-03 08:54:05 -07:00
|
|
|
|
backdropCount = parent.BackdropImagePaths.Count();
|
|
|
|
|
return parent.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent = parent.Parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-01 10:20:22 -07:00
|
|
|
|
|
2012-08-03 08:54:05 -07:00
|
|
|
|
private static Guid? GetParentLogoItemId(BaseItem item)
|
|
|
|
|
{
|
|
|
|
|
var parent = item.Parent;
|
|
|
|
|
|
|
|
|
|
while (parent != null)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrEmpty(parent.LogoImagePath))
|
|
|
|
|
{
|
|
|
|
|
return parent.Id;
|
2012-08-01 10:20:22 -07:00
|
|
|
|
}
|
2012-08-03 08:54:05 -07:00
|
|
|
|
|
|
|
|
|
parent = parent.Parent;
|
2012-08-01 10:20:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-11 21:03:19 -07:00
|
|
|
|
|
|
|
|
|
private static string _FFMpegDirectory = null;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the folder path to ffmpeg
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string FFMpegDirectory
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_FFMpegDirectory == null)
|
|
|
|
|
{
|
|
|
|
|
_FFMpegDirectory = System.IO.Path.Combine(ApplicationPaths.ProgramDataPath, "ffmpeg");
|
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(_FFMpegDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(_FFMpegDirectory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _FFMpegDirectory;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string _FFMpegPath = null;
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the path to ffmpeg.exe
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static string FFMpegPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_FFMpegPath == null)
|
|
|
|
|
{
|
|
|
|
|
string filename = "ffmpeg.exe";
|
|
|
|
|
|
|
|
|
|
_FFMpegPath = Path.Combine(FFMpegDirectory, filename);
|
|
|
|
|
|
2012-08-15 06:20:29 -07:00
|
|
|
|
// Always re-extract the first time to handle new versions
|
|
|
|
|
if (File.Exists(_FFMpegPath))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(_FFMpegPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract ffprobe
|
|
|
|
|
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Api.FFMpeg." + filename))
|
2012-08-11 21:03:19 -07:00
|
|
|
|
{
|
2012-08-15 06:20:29 -07:00
|
|
|
|
using (FileStream fileStream = new FileStream(_FFMpegPath, FileMode.Create))
|
2012-08-11 21:03:19 -07:00
|
|
|
|
{
|
2012-08-15 06:20:29 -07:00
|
|
|
|
stream.CopyTo(fileStream);
|
2012-08-11 21:03:19 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _FFMpegPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-12 20:50:50 -07:00
|
|
|
|
}
|
|
|
|
|
}
|