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;
|
|
|
|
|
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
|
|
|
|
|
{
|
2012-08-10 20:13:56 -07:00
|
|
|
|
private static string _FFMpegDirectory = null;
|
2012-08-10 08:17:52 -07:00
|
|
|
|
/// <summary>
|
2012-08-10 20:13:56 -07:00
|
|
|
|
/// Gets the folder path to ffmpeg
|
2012-08-10 08:17:52 -07:00
|
|
|
|
/// </summary>
|
2012-08-10 20:13:56 -07:00
|
|
|
|
public static string FFMpegDirectory
|
2012-08-10 08:17:52 -07:00
|
|
|
|
{
|
2012-08-10 20:13:56 -07:00
|
|
|
|
get
|
2012-08-10 08:17:52 -07:00
|
|
|
|
{
|
2012-08-10 20:13:56 -07:00
|
|
|
|
if (_FFMpegDirectory == null)
|
|
|
|
|
{
|
|
|
|
|
_FFMpegDirectory = System.IO.Path.Combine(ApplicationPaths.ProgramDataPath, "ffmpeg");
|
2012-08-10 08:17:52 -07:00
|
|
|
|
|
2012-08-10 20:13:56 -07:00
|
|
|
|
if (!Directory.Exists(_FFMpegDirectory))
|
|
|
|
|
{
|
|
|
|
|
Directory.CreateDirectory(_FFMpegDirectory);
|
|
|
|
|
|
|
|
|
|
// Extract ffmpeg
|
|
|
|
|
using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.Api.ffmpeg.ffmpeg.exe"))
|
|
|
|
|
{
|
|
|
|
|
using (FileStream fileStream = new FileStream(FFMpegPath, FileMode.Create))
|
|
|
|
|
{
|
|
|
|
|
stream.CopyTo(fileStream);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _FFMpegDirectory;
|
2012-08-10 08:17:52 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-10 20:13:56 -07:00
|
|
|
|
public static string FFMpegPath
|
2012-08-10 08:17:52 -07:00
|
|
|
|
{
|
2012-08-10 20:13:56 -07:00
|
|
|
|
get
|
2012-08-10 08:17:52 -07:00
|
|
|
|
{
|
2012-08-10 20:13:56 -07:00
|
|
|
|
return System.IO.Path.Combine(FFMpegDirectory, "ffmpeg.exe");
|
2012-08-10 08:17:52 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-12 20:50:50 -07:00
|
|
|
|
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-07-19 19:22:44 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Takes a BaseItem and returns the actual object that will be serialized by the api
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static ApiBaseItemWrapper<BaseItem> GetSerializationObject(BaseItem item, bool includeChildren, Guid userId)
|
2012-07-12 20:50:50 -07:00
|
|
|
|
{
|
2012-07-19 19:22:44 -07:00
|
|
|
|
ApiBaseItemWrapper<BaseItem> wrapper = new ApiBaseItemWrapper<BaseItem>()
|
2012-07-12 20:50:50 -07:00
|
|
|
|
{
|
2012-07-19 19:22:44 -07:00
|
|
|
|
Item = item,
|
|
|
|
|
UserItemData = Kernel.Instance.GetUserItemData(userId, item.Id),
|
2012-07-29 08:19:25 -07:00
|
|
|
|
Type = item.GetType().Name,
|
2012-08-03 08:54:05 -07:00
|
|
|
|
IsFolder = (item is Folder)
|
2012-07-19 19:22:44 -07:00
|
|
|
|
};
|
2012-07-12 20:50:50 -07:00
|
|
|
|
|
2012-08-03 08:54:05 -07: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 10:20:22 -07:00
|
|
|
|
if (item.Parent != null)
|
|
|
|
|
{
|
|
|
|
|
wrapper.ParentId = item.Parent.Id;
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-19 19:22:44 -07:00
|
|
|
|
if (includeChildren)
|
2012-07-16 09:50:44 -07:00
|
|
|
|
{
|
2012-07-19 19:22:44 -07:00
|
|
|
|
var folder = item as Folder;
|
2012-07-12 20:50:50 -07:00
|
|
|
|
|
2012-07-19 19:22:44 -07:00
|
|
|
|
if (folder != null)
|
2012-07-16 09:50:44 -07:00
|
|
|
|
{
|
2012-07-19 19:22:44 -07:00
|
|
|
|
wrapper.Children = Kernel.Instance.GetParentalAllowedChildren(folder, userId).Select(c => GetSerializationObject(c, false, userId));
|
2012-07-16 09:50:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-07-19 19:22:44 -07:00
|
|
|
|
return wrapper;
|
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-07-12 20:50:50 -07:00
|
|
|
|
}
|
|
|
|
|
}
|