jellyfin/MediaBrowser.Api/ApiService.cs

143 lines
4.4 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Linq;
using System.Reflection;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Controller;
using MediaBrowser.Model.Entities;
namespace MediaBrowser.Api
{
/// <summary>
/// Contains some helpers for the api
/// </summary>
public static class ApiService
{
private static string _FFMpegDirectory = null;
2012-08-10 08:17:52 -07:00
/// <summary>
/// Gets the folder path to ffmpeg
2012-08-10 08:17:52 -07:00
/// </summary>
public static string FFMpegDirectory
2012-08-10 08:17:52 -07:00
{
get
2012-08-10 08:17:52 -07:00
{
if (_FFMpegDirectory == null)
{
_FFMpegDirectory = System.IO.Path.Combine(ApplicationPaths.ProgramDataPath, "ffmpeg");
2012-08-10 08:17:52 -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
}
}
public static string FFMpegPath
2012-08-10 08:17:52 -07:00
{
get
2012-08-10 08:17:52 -07:00
{
return System.IO.Path.Combine(FFMpegDirectory, "ffmpeg.exe");
2012-08-10 08:17:52 -07:00
}
}
public static BaseItem GetItemById(string id)
{
Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
return Kernel.Instance.GetItemById(guid);
}
/// <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)
{
ApiBaseItemWrapper<BaseItem> wrapper = new ApiBaseItemWrapper<BaseItem>()
{
Item = item,
UserItemData = Kernel.Instance.GetUserItemData(userId, item.Id),
Type = item.GetType().Name,
IsFolder = (item is Folder)
};
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;
}
if (item.Parent != null)
{
wrapper.ParentId = item.Parent.Id;
}
if (includeChildren)
{
var folder = item as Folder;
if (folder != null)
{
wrapper.Children = Kernel.Instance.GetParentalAllowedChildren(folder, userId).Select(c => GetSerializationObject(c, false, userId));
}
}
return wrapper;
}
private static Guid? GetParentBackdropItemId(BaseItem item, out int backdropCount)
{
backdropCount = 0;
var parent = item.Parent;
while (parent != null)
{
if (parent.BackdropImagePaths != null && parent.BackdropImagePaths.Any())
{
backdropCount = parent.BackdropImagePaths.Count();
return parent.Id;
}
parent = parent.Parent;
}
return null;
}
private static Guid? GetParentLogoItemId(BaseItem item)
{
var parent = item.Parent;
while (parent != null)
{
if (!string.IsNullOrEmpty(parent.LogoImagePath))
{
return parent.Id;
}
parent = parent.Parent;
}
return null;
}
}
}