2012-07-13 03:50:50 +00:00
|
|
|
|
using System;
|
2012-08-11 03:13:56 +00:00
|
|
|
|
using System.IO;
|
2012-07-20 02:22:44 +00:00
|
|
|
|
using System.Linq;
|
2012-08-11 03:13:56 +00:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
using MediaBrowser.Controller;
|
2012-08-17 13:16:50 +00:00
|
|
|
|
using MediaBrowser.Model.DTO;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
using MediaBrowser.Model.Entities;
|
2012-08-17 17:37:26 +00:00
|
|
|
|
using MediaBrowser.Model.Users;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Api
|
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Contains some helpers for the api
|
|
|
|
|
/// </summary>
|
2012-07-13 03:50:50 +00:00
|
|
|
|
public static class ApiService
|
|
|
|
|
{
|
|
|
|
|
public static BaseItem GetItemById(string id)
|
|
|
|
|
{
|
2012-07-16 16:50:44 +00:00
|
|
|
|
Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-07-16 16:50:44 +00:00
|
|
|
|
return Kernel.Instance.GetItemById(guid);
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Takes a BaseItem and returns the actual object that will be serialized by the api
|
|
|
|
|
/// </summary>
|
2012-08-17 16:47:35 +00:00
|
|
|
|
public static BaseItemContainer<BaseItem> GetSerializationObject(BaseItem item, bool includeChildren, Guid userId)
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-08-17 17:37:26 +00:00
|
|
|
|
User user = Kernel.Instance.Users.First(u => u.Id == userId);
|
|
|
|
|
|
2012-08-17 16:47:35 +00:00
|
|
|
|
BaseItemContainer<BaseItem> wrapper = new BaseItemContainer<BaseItem>()
|
2012-07-13 03:50:50 +00:00
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
Item = item,
|
2012-08-17 17:37:26 +00:00
|
|
|
|
UserItemData = user.GetItemData(item.Id),
|
2012-07-29 15:19:25 +00:00
|
|
|
|
Type = item.GetType().Name,
|
2012-08-03 15:54:05 +00:00
|
|
|
|
IsFolder = (item is Folder)
|
2012-07-20 02:22:44 +00:00
|
|
|
|
};
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-08-03 15:54:05 +00:00
|
|
|
|
if (string.IsNullOrEmpty(item.LogoImagePath))
|
|
|
|
|
{
|
|
|
|
|
wrapper.ParentLogoItemId = GetParentLogoItemId(item);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (item.BackdropImagePaths == null || !item.BackdropImagePaths.Any())
|
|
|
|
|
{
|
|
|
|
|
int backdropCount;
|
|
|
|
|
wrapper.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
|
|
|
|
|
wrapper.ParentBackdropCount = backdropCount;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-01 17:20:22 +00:00
|
|
|
|
if (item.Parent != null)
|
|
|
|
|
{
|
|
|
|
|
wrapper.ParentId = item.Parent.Id;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
if (includeChildren)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-07-20 02:22:44 +00:00
|
|
|
|
var folder = item as Folder;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
if (folder != null)
|
2012-07-16 16:50:44 +00:00
|
|
|
|
{
|
2012-08-17 17:37:26 +00:00
|
|
|
|
wrapper.Children = folder.GetParentalAllowedChildren(user).Select(c => GetSerializationObject(c, false, userId));
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
2012-08-15 13:20:29 +00:00
|
|
|
|
|
2012-08-17 16:47:35 +00:00
|
|
|
|
// Attach People by transforming them into BaseItemPerson (DTO)
|
|
|
|
|
if (item.People != null)
|
|
|
|
|
{
|
|
|
|
|
wrapper.People = item.People.Select(p =>
|
|
|
|
|
{
|
|
|
|
|
BaseItemPerson baseItemPerson = new BaseItemPerson();
|
|
|
|
|
|
|
|
|
|
baseItemPerson.PersonInfo = p;
|
|
|
|
|
|
|
|
|
|
Person ibnObject = Kernel.Instance.ItemController.GetPerson(p.Name);
|
|
|
|
|
|
|
|
|
|
if (ibnObject != null)
|
|
|
|
|
{
|
|
|
|
|
baseItemPerson.PrimaryImagePath = ibnObject.PrimaryImagePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseItemPerson;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attach Studios by transforming them into BaseItemStudio (DTO)
|
|
|
|
|
if (item.Studios != null)
|
|
|
|
|
{
|
|
|
|
|
wrapper.Studios = item.Studios.Select(s =>
|
|
|
|
|
{
|
|
|
|
|
BaseItemStudio baseItemStudio = new BaseItemStudio();
|
|
|
|
|
|
|
|
|
|
baseItemStudio.Name = s;
|
|
|
|
|
|
|
|
|
|
Studio ibnObject = Kernel.Instance.ItemController.GetStudio(s);
|
|
|
|
|
|
|
|
|
|
if (ibnObject != null)
|
|
|
|
|
{
|
|
|
|
|
baseItemStudio.PrimaryImagePath = ibnObject.PrimaryImagePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return baseItemStudio;
|
|
|
|
|
});
|
2012-07-16 16:50:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-20 02:22:44 +00:00
|
|
|
|
return wrapper;
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
2012-08-01 17:20:22 +00:00
|
|
|
|
|
2012-08-03 15:54:05 +00:00
|
|
|
|
private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
|
2012-08-01 17:20:22 +00:00
|
|
|
|
{
|
2012-08-03 15:54:05 +00:00
|
|
|
|
backdropCount = 0;
|
|
|
|
|
|
|
|
|
|
var parent = item.Parent;
|
2012-08-01 17:20:22 +00:00
|
|
|
|
|
2012-08-03 15:54:05 +00:00
|
|
|
|
while (parent != null)
|
|
|
|
|
{
|
|
|
|
|
if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
|
2012-08-01 17:20:22 +00:00
|
|
|
|
{
|
2012-08-03 15:54:05 +00:00
|
|
|
|
backdropCount = parent.BackdropImagePaths.Count();
|
|
|
|
|
return parent.Id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parent = parent.Parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-01 17:20:22 +00:00
|
|
|
|
|
2012-08-03 15:54:05 +00: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 17:20:22 +00:00
|
|
|
|
}
|
2012-08-03 15:54:05 +00:00
|
|
|
|
|
|
|
|
|
parent = parent.Parent;
|
2012-08-01 17:20:22 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2012-08-12 04:03:19 +00: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 13:20:29 +00: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-12 04:03:19 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
using (FileStream fileStream = new FileStream(_FFMpegPath, FileMode.Create))
|
2012-08-12 04:03:19 +00:00
|
|
|
|
{
|
2012-08-15 13:20:29 +00:00
|
|
|
|
stream.CopyTo(fileStream);
|
2012-08-12 04:03:19 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _FFMpegPath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-07-13 03:50:50 +00:00
|
|
|
|
}
|
|
|
|
|
}
|