jellyfin-server/MediaBrowser.Api/ApiService.cs

439 lines
14 KiB
C#
Raw Normal View History

2012-09-07 16:17:39 +00:00
using MediaBrowser.Controller;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Entities.Movies;
using MediaBrowser.Controller.Entities.TV;
2012-09-07 16:17:39 +00:00
using MediaBrowser.Model.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace MediaBrowser.Api
{
/// <summary>
/// Contains some helpers for the api
/// </summary>
public static class ApiService
{
2012-09-07 16:17:39 +00:00
/// <summary>
/// Gets an Item by Id, or the root item if none is supplied
/// </summary>
public static BaseItem GetItemById(string id)
{
Guid guid = string.IsNullOrEmpty(id) ? Guid.Empty : new Guid(id);
return Kernel.Instance.GetItemById(guid);
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// Gets a User by Id
/// </summary>
/// <param name="logActivity">Whether or not to update the user's LastActivityDate</param>
public static User GetUserById(string id, bool logActivity)
{
2012-09-11 18:20:12 +00:00
var guid = new Guid(id);
2012-09-07 16:17:39 +00:00
2012-09-11 18:20:12 +00:00
var user = Kernel.Instance.Users.FirstOrDefault(u => u.Id == guid);
2012-09-07 16:17:39 +00:00
if (logActivity)
{
LogUserActivity(user);
}
return user;
}
/// <summary>
/// Gets the default User
/// </summary>
/// <param name="logActivity">Whether or not to update the user's LastActivityDate</param>
public static User GetDefaultUser(bool logActivity)
{
User user = Kernel.Instance.GetDefaultUser();
if (logActivity)
{
LogUserActivity(user);
}
return user;
}
/// <summary>
/// Updates LastActivityDate for a given User
/// </summary>
public static void LogUserActivity(User user)
{
user.LastActivityDate = DateTime.UtcNow;
Kernel.Instance.SaveUser(user);
}
/// <summary>
/// Converts a BaseItem to a DTOBaseItem
/// </summary>
2012-09-11 18:20:12 +00:00
public async static Task<DtoBaseItem> GetDtoBaseItem(BaseItem item, User user,
2012-08-20 12:09:42 +00:00
bool includeChildren = true,
bool includePeople = true)
{
2012-09-11 19:37:14 +00:00
var dto = new DtoBaseItem();
2012-09-11 19:37:14 +00:00
var tasks = new List<Task>();
2012-08-22 17:01:24 +00:00
tasks.Add(AttachStudios(dto, item));
if (includeChildren)
{
tasks.Add(AttachChildren(dto, item, user));
tasks.Add(AttachLocalTrailers(dto, item, user));
}
if (includePeople)
{
tasks.Add(AttachPeople(dto, item));
}
2012-09-02 21:55:47 +00:00
AttachBasicFields(dto, item, user);
2012-08-22 17:01:24 +00:00
// Make sure all the tasks we kicked off have completed.
if (tasks.Count > 0)
{
await Task.WhenAll(tasks).ConfigureAwait(false);
}
return dto;
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// Sets simple property values on a DTOBaseItem
/// </summary>
2012-09-11 18:20:12 +00:00
private static void AttachBasicFields(DtoBaseItem dto, BaseItem item, User user)
2012-08-22 17:01:24 +00:00
{
dto.AspectRatio = item.AspectRatio;
dto.BackdropCount = item.BackdropImagePaths == null ? 0 : item.BackdropImagePaths.Count();
dto.DateCreated = item.DateCreated;
dto.DisplayMediaType = item.DisplayMediaType;
2012-08-29 19:37:05 +00:00
if (item.Genres != null)
{
dto.Genres = item.Genres.ToArray();
}
dto.HasArt = !string.IsNullOrEmpty(item.ArtImagePath);
dto.HasBanner = !string.IsNullOrEmpty(item.BannerImagePath);
dto.HasLogo = !string.IsNullOrEmpty(item.LogoImagePath);
2012-09-02 21:55:47 +00:00
dto.HasPrimaryImage = !string.IsNullOrEmpty(item.PrimaryImagePath);
dto.HasThumb = !string.IsNullOrEmpty(item.ThumbnailImagePath);
dto.Id = item.Id;
dto.IsNew = item.IsRecentlyAdded(user);
dto.IndexNumber = item.IndexNumber;
dto.IsFolder = item.IsFolder;
2012-08-20 02:05:55 +00:00
dto.Language = item.Language;
dto.LocalTrailerCount = item.LocalTrailers == null ? 0 : item.LocalTrailers.Count();
dto.Name = item.Name;
dto.OfficialRating = item.OfficialRating;
dto.Overview = item.Overview;
2012-09-11 19:37:14 +00:00
// If there are no backdrops, indicate what parent has them in case the Ui wants to allow inheritance
if (dto.BackdropCount == 0)
{
int backdropCount;
dto.ParentBackdropItemId = GetParentBackdropItemId(item, out backdropCount);
dto.ParentBackdropCount = backdropCount;
}
if (item.Parent != null)
{
dto.ParentId = item.Parent.Id;
}
2012-08-20 02:05:55 +00:00
dto.ParentIndexNumber = item.ParentIndexNumber;
2012-09-11 19:37:14 +00:00
// If there is no logo, indicate what parent has one in case the Ui wants to allow inheritance
if (!dto.HasLogo)
{
dto.ParentLogoItemId = GetParentLogoItemId(item);
}
dto.Path = item.Path;
dto.PremiereDate = item.PremiereDate;
dto.ProductionYear = item.ProductionYear;
dto.ProviderIds = item.ProviderIds;
dto.RunTimeTicks = item.RunTimeTicks;
dto.SortName = item.SortName;
2012-08-29 19:37:05 +00:00
if (item.Taglines != null)
{
dto.Taglines = item.Taglines.ToArray();
}
dto.TrailerUrl = item.TrailerUrl;
dto.Type = item.GetType().Name;
dto.UserRating = item.UserRating;
2012-09-11 18:20:12 +00:00
dto.UserData = GetDtoUserItemData(item.GetUserData(user, false));
2012-09-11 19:37:14 +00:00
var folder = item as Folder;
if (folder != null)
{
dto.SpecialCounts = folder.GetSpecialCounts(user);
dto.IsRoot = folder.IsRoot;
dto.IsVirtualFolder = folder.IsVirtualFolder;
}
2012-08-20 12:09:42 +00:00
// Add AudioInfo
2012-09-11 19:37:14 +00:00
var audio = item as Audio;
2012-08-20 12:09:42 +00:00
if (audio != null)
{
2012-09-11 18:20:12 +00:00
dto.AudioInfo = new AudioInfo
2012-08-20 12:09:42 +00:00
{
Album = audio.Album,
AlbumArtist = audio.AlbumArtist,
Artist = audio.Artist,
BitRate = audio.BitRate,
2012-08-20 12:20:51 +00:00
Channels = audio.Channels
2012-08-20 12:09:42 +00:00
};
}
// Add VideoInfo
2012-09-11 19:37:14 +00:00
var video = item as Video;
2012-08-22 12:56:44 +00:00
if (video != null)
{
2012-09-11 18:20:12 +00:00
dto.VideoInfo = new VideoInfo
2012-08-22 12:56:44 +00:00
{
Height = video.Height,
Width = video.Width,
Codec = video.Codec,
VideoType = video.VideoType,
ScanType = video.ScanType
};
2012-08-29 19:37:05 +00:00
if (video.AudioStreams != null)
{
dto.VideoInfo.AudioStreams = video.AudioStreams.ToArray();
}
if (video.Subtitles != null)
{
dto.VideoInfo.Subtitles = video.Subtitles.ToArray();
}
2012-08-22 12:56:44 +00:00
}
// Add SeriesInfo
2012-09-11 19:37:14 +00:00
var series = item as Series;
if (series != null)
{
2012-09-11 19:37:14 +00:00
DayOfWeek[] airDays = series.AirDays == null ? new DayOfWeek[] { } : series.AirDays.ToArray();
2012-09-11 18:20:12 +00:00
dto.SeriesInfo = new SeriesInfo
{
AirDays = airDays,
AirTime = series.AirTime,
Status = series.Status
};
}
// Add MovieInfo
2012-09-11 19:37:14 +00:00
var movie = item as Movie;
if (movie != null)
{
int specialFeatureCount = movie.SpecialFeatures == null ? 0 : movie.SpecialFeatures.Count();
2012-09-11 18:20:12 +00:00
dto.MovieInfo = new MovieInfo
{
SpecialFeatureCount = specialFeatureCount
};
}
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// Attaches Studio DTO's to a DTOBaseItem
/// </summary>
2012-09-11 18:20:12 +00:00
private static async Task AttachStudios(DtoBaseItem dto, BaseItem item)
{
// Attach Studios by transforming them into BaseItemStudio (DTO)
if (item.Studios != null)
{
2012-09-11 18:20:12 +00:00
Studio[] entities = await Task.WhenAll(item.Studios.Select(c => Kernel.Instance.ItemController.GetStudio(c))).ConfigureAwait(false);
2012-08-29 19:37:05 +00:00
dto.Studios = new BaseItemStudio[entities.Length];
2012-08-20 12:09:42 +00:00
2012-08-29 19:37:05 +00:00
for (int i = 0; i < entities.Length; i++)
{
2012-08-29 19:37:05 +00:00
Studio entity = entities[i];
2012-09-11 19:37:14 +00:00
var baseItemStudio = new BaseItemStudio{};
2012-08-29 19:37:05 +00:00
baseItemStudio.Name = entity.Name;
2012-08-29 19:37:05 +00:00
baseItemStudio.HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath);
2012-08-29 19:37:05 +00:00
dto.Studios[i] = baseItemStudio;
}
}
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// Attaches child DTO's to a DTOBaseItem
/// </summary>
2012-09-11 18:20:12 +00:00
private static async Task AttachChildren(DtoBaseItem dto, BaseItem item, User user)
{
var folder = item as Folder;
if (folder != null)
{
IEnumerable<BaseItem> children = folder.GetParentalAllowedChildren(user);
2012-09-11 18:20:12 +00:00
dto.Children = await Task.WhenAll(children.Select(c => GetDtoBaseItem(c, user, false, false))).ConfigureAwait(false);
}
2012-08-22 17:01:24 +00:00
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// Attaches trailer DTO's to a DTOBaseItem
/// </summary>
2012-09-11 18:20:12 +00:00
private static async Task AttachLocalTrailers(DtoBaseItem dto, BaseItem item, User user)
2012-08-22 17:01:24 +00:00
{
if (item.LocalTrailers != null && item.LocalTrailers.Any())
{
2012-09-11 18:20:12 +00:00
dto.LocalTrailers = await Task.WhenAll(item.LocalTrailers.Select(c => GetDtoBaseItem(c, user, false, false))).ConfigureAwait(false);
}
}
2012-08-23 12:50:41 +00:00
2012-09-07 16:17:39 +00:00
/// <summary>
/// Attaches People DTO's to a DTOBaseItem
/// </summary>
2012-09-11 18:20:12 +00:00
private static async Task AttachPeople(DtoBaseItem dto, BaseItem item)
{
// Attach People by transforming them into BaseItemPerson (DTO)
if (item.People != null)
{
2012-09-11 18:20:12 +00:00
IEnumerable<Person> entities = await Task.WhenAll(item.People.Select(c => Kernel.Instance.ItemController.GetPerson(c.Key))).ConfigureAwait(false);
dto.People = item.People.Select(p =>
{
2012-09-11 19:37:14 +00:00
var baseItemPerson = new BaseItemPerson{};
baseItemPerson.Name = p.Key;
baseItemPerson.Overview = p.Value.Overview;
baseItemPerson.Type = p.Value.Type;
Person ibnObject = entities.First(i => i.Name.Equals(p.Key, StringComparison.OrdinalIgnoreCase));
if (ibnObject != null)
{
baseItemPerson.HasImage = !string.IsNullOrEmpty(ibnObject.PrimaryImagePath);
}
return baseItemPerson;
}).ToArray();
}
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// If an item does not any backdrops, this can be used to find the first parent that does have one
/// </summary>
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;
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// If an item does not have a logo, this can be used to find the first parent that does have one
/// </summary>
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;
}
2012-08-23 12:50:41 +00:00
2012-09-07 16:17:39 +00:00
/// <summary>
/// Gets an ImagesByName entity along with the number of items containing it
/// </summary>
2012-09-11 19:37:14 +00:00
public static IbnItem GetIbnItem(BaseEntity entity, int itemCount)
2012-08-23 12:50:41 +00:00
{
2012-09-11 19:37:14 +00:00
return new IbnItem
2012-08-23 12:50:41 +00:00
{
Id = entity.Id,
BaseItemCount = itemCount,
HasImage = !string.IsNullOrEmpty(entity.PrimaryImagePath),
2012-08-23 12:50:41 +00:00
Name = entity.Name
};
}
2012-09-07 16:17:39 +00:00
/// <summary>
/// Converts a User to a DTOUser
/// </summary>
2012-09-11 18:20:12 +00:00
public static DtoUser GetDtoUser(User user)
{
2012-09-11 18:20:12 +00:00
return new DtoUser
{
Id = user.Id,
Name = user.Name,
2012-09-06 18:38:29 +00:00
HasImage = !string.IsNullOrEmpty(user.PrimaryImagePath),
2012-09-06 20:29:33 +00:00
HasPassword = !string.IsNullOrEmpty(user.Password),
LastActivityDate = user.LastActivityDate,
LastLoginDate = user.LastLoginDate
};
}
2012-09-09 18:32:51 +00:00
/// <summary>
/// Converts a UserItemData to a DTOUserItemData
/// </summary>
2012-09-11 18:20:12 +00:00
public static DtoUserItemData GetDtoUserItemData(UserItemData data)
2012-09-09 18:32:51 +00:00
{
if (data == null)
{
return null;
}
2012-09-11 18:20:12 +00:00
return new DtoUserItemData
2012-09-09 18:32:51 +00:00
{
IsFavorite = data.IsFavorite,
Likes = data.Likes,
PlaybackPositionTicks = data.PlaybackPositionTicks,
PlayCount = data.PlayCount,
Rating = data.Rating
};
}
public static bool IsApiUrlMatch(string url, HttpListenerRequest request)
{
2012-09-08 15:24:45 +00:00
url = "/api/" + url;
return request.Url.LocalPath.EndsWith(url, StringComparison.OrdinalIgnoreCase);
}
}
}