2015-08-21 04:36:30 +02:00
using MediaBrowser.Api.Movies ;
using MediaBrowser.Api.Music ;
using MediaBrowser.Controller.Activity ;
using MediaBrowser.Controller.Channels ;
2014-02-21 06:04:11 +01:00
using MediaBrowser.Controller.Dto ;
using MediaBrowser.Controller.Entities ;
using MediaBrowser.Controller.Entities.Audio ;
using MediaBrowser.Controller.Entities.Movies ;
using MediaBrowser.Controller.Entities.TV ;
2013-02-21 02:33:05 +01:00
using MediaBrowser.Controller.Library ;
2015-08-03 04:12:52 +02:00
using MediaBrowser.Controller.LiveTv ;
2015-02-12 04:54:31 +01:00
using MediaBrowser.Controller.Localization ;
2014-07-04 04:22:57 +02:00
using MediaBrowser.Controller.Net ;
2014-02-21 06:04:11 +01:00
using MediaBrowser.Controller.Persistence ;
2015-08-21 04:36:30 +02:00
using MediaBrowser.Controller.TV ;
2015-02-12 04:54:31 +01:00
using MediaBrowser.Model.Activity ;
2015-08-21 04:36:30 +02:00
using MediaBrowser.Model.Channels ;
2014-02-21 06:04:11 +01:00
using MediaBrowser.Model.Dto ;
using MediaBrowser.Model.Entities ;
using MediaBrowser.Model.Querying ;
2013-12-07 16:52:38 +01:00
using ServiceStack ;
2013-02-21 02:33:05 +01:00
using System ;
2014-02-21 06:04:11 +01:00
using System.Collections ;
2013-02-21 02:33:05 +01:00
using System.Collections.Generic ;
2014-02-21 06:04:11 +01:00
using System.Globalization ;
using System.IO ;
2013-02-21 02:33:05 +01:00
using System.Linq ;
2014-02-21 06:04:11 +01:00
using System.Threading ;
using System.Threading.Tasks ;
2015-10-04 06:23:11 +02:00
using CommonIO ;
2015-09-14 01:07:54 +02:00
using MediaBrowser.Common.IO ;
2013-02-21 02:33:05 +01:00
2013-02-25 04:56:00 +01:00
namespace MediaBrowser.Api.Library
2013-02-21 02:33:05 +01:00
{
2014-11-15 03:31:03 +01:00
[Route("/Items/{Id}/File", "GET", Summary = "Gets the original file of an item")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetFile
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
}
/// <summary>
/// Class GetCriticReviews
/// </summary>
2014-11-15 03:31:03 +01:00
[Route("/Items/{Id}/CriticReviews", "GET", Summary = "Gets critic reviews for an item")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetCriticReviews : IReturn < QueryResult < ItemReview > >
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
/// <summary>
/// Skips over a given number of items within the results. Use for paging.
/// </summary>
/// <value>The start index.</value>
[ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? StartIndex { get ; set ; }
/// <summary>
/// The maximum number of items to return
/// </summary>
/// <value>The limit.</value>
[ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
public int? Limit { get ; set ; }
}
/// <summary>
/// Class GetThemeSongs
/// </summary>
2014-11-15 03:31:03 +01:00
[Route("/Items/{Id}/ThemeSongs", "GET", Summary = "Gets theme songs for an item")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetThemeSongs : IReturn < ThemeMediaResult >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-05-30 01:51:33 +02:00
public string UserId { get ; set ; }
2014-02-21 06:04:11 +01:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
[ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public bool InheritFromParent { get ; set ; }
}
/// <summary>
/// Class GetThemeVideos
/// </summary>
2014-11-15 03:31:03 +01:00
[Route("/Items/{Id}/ThemeVideos", "GET", Summary = "Gets theme videos for an item")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetThemeVideos : IReturn < ThemeMediaResult >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-05-30 01:51:33 +02:00
public string UserId { get ; set ; }
2014-02-21 06:04:11 +01:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
[ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public bool InheritFromParent { get ; set ; }
}
/// <summary>
/// Class GetThemeVideos
/// </summary>
2014-11-15 03:31:03 +01:00
[Route("/Items/{Id}/ThemeMedia", "GET", Summary = "Gets theme videos and songs for an item")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetThemeMedia : IReturn < AllThemeMediaResult >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-05-30 01:51:33 +02:00
public string UserId { get ; set ; }
2014-02-21 06:04:11 +01:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
[ApiMember(Name = "InheritFromParent", Description = "Determines whether or not parent items should be searched for theme media.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
public bool InheritFromParent { get ; set ; }
}
2014-11-15 03:31:03 +01:00
[Route("/Library/Refresh", "POST", Summary = "Starts a library scan")]
[Authenticated(Roles = "Admin")]
2014-02-21 06:04:11 +01:00
public class RefreshLibrary : IReturnVoid
{
}
2014-11-15 03:31:03 +01:00
[Route("/Items/{Id}", "DELETE", Summary = "Deletes an item from the library and file system")]
2014-11-19 03:45:12 +01:00
[Authenticated]
2014-02-21 06:04:11 +01:00
public class DeleteItem : IReturnVoid
{
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Id { get ; set ; }
}
[Route("/Items/Counts", "GET")]
2014-11-15 03:31:03 +01:00
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetItemCounts : IReturn < ItemCounts >
{
[ApiMember(Name = "UserId", Description = "Optional. Get counts from a specific user's library.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-05-30 01:51:33 +02:00
public string UserId { get ; set ; }
2014-02-21 06:04:11 +01:00
[ApiMember(Name = "IsFavorite", Description = "Optional. Get counts of favorite items", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "GET")]
public bool? IsFavorite { get ; set ; }
}
2014-11-15 03:31:03 +01:00
[Route("/Items/{Id}/Ancestors", "GET", Summary = "Gets all parents of an item")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetAncestors : IReturn < BaseItemDto [ ] >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-05-30 01:51:33 +02:00
public string UserId { get ; set ; }
2014-02-21 06:04:11 +01:00
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
}
2014-11-15 03:31:03 +01:00
[Route("/Items/YearIndex", "GET", Summary = "Gets a year index based on an item query.")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetYearIndex : IReturn < List < ItemIndex > >
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "Optional. Filter by user id, and attach user data", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
2015-05-30 01:51:33 +02:00
public string UserId { get ; set ; }
2014-02-21 06:04:11 +01:00
[ApiMember(Name = "IncludeItemTypes", Description = "Optional. If specified, results will be filtered based on item type. This allows multiple, comma delimeted.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET", AllowMultiple = true)]
public string IncludeItemTypes { get ; set ; }
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Class GetPhyscialPaths
/// </summary>
2014-11-15 03:31:03 +01:00
[Route("/Library/PhysicalPaths", "GET", Summary = "Gets a list of physical paths from virtual folders")]
[Authenticated(Roles = "Admin")]
2013-02-21 02:33:05 +01:00
public class GetPhyscialPaths : IReturn < List < string > >
{
}
2014-11-15 03:31:03 +01:00
[Route("/Library/MediaFolders", "GET", Summary = "Gets all user media folders.")]
[Authenticated]
2014-02-21 06:04:11 +01:00
public class GetMediaFolders : IReturn < ItemsResult >
{
2014-07-01 06:26:50 +02:00
[ApiMember(Name = "IsHidden", Description = "Optional. Filter by folders that are marked hidden, or not.", IsRequired = false, DataType = "boolean", ParameterType = "query", Verb = "GET")]
public bool? IsHidden { get ; set ; }
2014-02-21 06:04:11 +01:00
}
2014-11-15 03:31:03 +01:00
[Route("/Library/Series/Added", "POST", Summary = "Reports that new episodes of a series have been added by an external source")]
[Route("/Library/Series/Updated", "POST", Summary = "Reports that new episodes of a series have been added by an external source")]
[Authenticated]
2014-04-27 05:42:05 +02:00
public class PostUpdatedSeries : IReturnVoid
{
2014-07-12 16:05:51 +02:00
[ApiMember(Name = "TvdbId", Description = "Tvdb Id", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
2014-04-27 05:42:05 +02:00
public string TvdbId { get ; set ; }
}
2015-09-02 17:33:20 +02:00
[Route("/Library/Movies/Added", "POST", Summary = "Reports that new movies have been added by an external source")]
[Route("/Library/Movies/Updated", "POST", Summary = "Reports that new movies have been added by an external source")]
[Authenticated]
public class PostUpdatedMovies : IReturnVoid
{
[ApiMember(Name = "TmdbId", Description = "Tmdb Id", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string TmdbId { get ; set ; }
[ApiMember(Name = "ImdbId", Description = "Imdb Id", IsRequired = false, DataType = "string", ParameterType = "path", Verb = "GET")]
public string ImdbId { get ; set ; }
}
2015-02-06 06:39:07 +01:00
[Route("/Items/{Id}/Download", "GET", Summary = "Downloads item media")]
[Authenticated(Roles = "download")]
public class GetDownload
{
/// <summary>
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public string Id { get ; set ; }
}
2015-08-21 05:21:27 +02:00
[Route("/Items/{Id}/Similar", "GET", Summary = "Gets similar items")]
[Authenticated]
2015-08-21 04:36:30 +02:00
public class GetSimilarItems : BaseGetSimilarItemsFromItem
{
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Class LibraryService
/// </summary>
2013-03-16 06:52:33 +01:00
public class LibraryService : BaseApiService
2013-02-21 02:33:05 +01:00
{
2013-02-24 22:53:54 +01:00
/// <summary>
2014-02-21 06:04:11 +01:00
/// The _item repo
2013-02-24 22:53:54 +01:00
/// </summary>
2014-02-21 06:04:11 +01:00
private readonly IItemRepository _itemRepo ;
2013-02-28 20:32:41 +01:00
private readonly ILibraryManager _libraryManager ;
2014-02-21 06:04:11 +01:00
private readonly IUserManager _userManager ;
private readonly IUserDataManager _userDataManager ;
private readonly IDtoService _dtoService ;
2014-11-19 03:45:12 +01:00
private readonly IAuthorizationContext _authContext ;
2015-02-12 04:54:31 +01:00
private readonly IActivityManager _activityManager ;
private readonly ILocalizationManager _localization ;
2015-08-03 04:12:52 +02:00
private readonly ILiveTvManager _liveTv ;
2015-08-21 04:36:30 +02:00
private readonly IChannelManager _channelManager ;
private readonly ITVSeriesManager _tvManager ;
2015-08-28 21:10:44 +02:00
private readonly ILibraryMonitor _libraryMonitor ;
2015-09-14 01:07:54 +02:00
private readonly IFileSystem _fileSystem ;
2013-02-24 22:53:54 +01:00
/// <summary>
/// Initializes a new instance of the <see cref="LibraryService" /> class.
/// </summary>
2014-02-21 06:04:11 +01:00
public LibraryService ( IItemRepository itemRepo , ILibraryManager libraryManager , IUserManager userManager ,
2015-09-14 01:07:54 +02:00
IDtoService dtoService , IUserDataManager userDataManager , IAuthorizationContext authContext , IActivityManager activityManager , ILocalizationManager localization , ILiveTvManager liveTv , IChannelManager channelManager , ITVSeriesManager tvManager , ILibraryMonitor libraryMonitor , IFileSystem fileSystem )
2013-02-24 22:53:54 +01:00
{
2014-02-21 06:04:11 +01:00
_itemRepo = itemRepo ;
_libraryManager = libraryManager ;
_userManager = userManager ;
_dtoService = dtoService ;
_userDataManager = userDataManager ;
2014-11-19 03:45:12 +01:00
_authContext = authContext ;
2015-02-12 04:54:31 +01:00
_activityManager = activityManager ;
_localization = localization ;
2015-08-05 05:43:54 +02:00
_liveTv = liveTv ;
2015-08-21 04:36:30 +02:00
_channelManager = channelManager ;
_tvManager = tvManager ;
2015-09-02 17:35:47 +02:00
_libraryMonitor = libraryMonitor ;
2015-09-14 01:07:54 +02:00
_fileSystem = fileSystem ;
2015-08-21 04:36:30 +02:00
}
public object Get ( GetSimilarItems request )
{
var user = ! string . IsNullOrWhiteSpace ( request . UserId ) ? _userManager . GetUserById ( request . UserId ) : null ;
var item = string . IsNullOrEmpty ( request . Id ) ?
( ! string . IsNullOrWhiteSpace ( request . UserId ) ? user . RootFolder :
_libraryManager . RootFolder ) : _libraryManager . GetItemById ( request . Id ) ;
if ( item is Game )
{
return new GamesService ( _userManager , _userDataManager , _libraryManager , _itemRepo , _dtoService )
{
AuthorizationContext = AuthorizationContext ,
Logger = Logger ,
Request = Request ,
SessionContext = SessionContext ,
ResultFactory = ResultFactory
} . Get ( new GetSimilarGames
{
Fields = request . Fields ,
Id = request . Id ,
Limit = request . Limit ,
UserId = request . UserId
} ) ;
}
if ( item is MusicAlbum )
{
return new AlbumsService ( _userManager , _userDataManager , _libraryManager , _itemRepo , _dtoService )
{
AuthorizationContext = AuthorizationContext ,
Logger = Logger ,
Request = Request ,
SessionContext = SessionContext ,
ResultFactory = ResultFactory
} . Get ( new GetSimilarAlbums
{
Fields = request . Fields ,
Id = request . Id ,
Limit = request . Limit ,
UserId = request . UserId
} ) ;
}
if ( item is MusicArtist )
{
return new AlbumsService ( _userManager , _userDataManager , _libraryManager , _itemRepo , _dtoService )
{
AuthorizationContext = AuthorizationContext ,
Logger = Logger ,
Request = Request ,
SessionContext = SessionContext ,
ResultFactory = ResultFactory
} . Get ( new GetSimilarArtists
{
Fields = request . Fields ,
Id = request . Id ,
Limit = request . Limit ,
UserId = request . UserId
} ) ;
}
var program = item as IHasProgramAttributes ;
var channelItem = item as ChannelVideoItem ;
2015-08-21 05:21:27 +02:00
if ( item is Movie | | ( program ! = null & & program . IsMovie ) | | ( channelItem ! = null & & channelItem . ContentType = = ChannelMediaContentType . Movie ) | | ( channelItem ! = null & & channelItem . ContentType = = ChannelMediaContentType . MovieExtra ) )
2015-08-21 04:36:30 +02:00
{
return new MoviesService ( _userManager , _userDataManager , _libraryManager , _itemRepo , _dtoService , _channelManager )
{
AuthorizationContext = AuthorizationContext ,
Logger = Logger ,
Request = Request ,
SessionContext = SessionContext ,
ResultFactory = ResultFactory
} . Get ( new GetSimilarMovies
{
Fields = request . Fields ,
Id = request . Id ,
Limit = request . Limit ,
UserId = request . UserId
} ) ;
}
if ( item is Series | | ( program ! = null & & program . IsSeries ) | | ( channelItem ! = null & & channelItem . ContentType = = ChannelMediaContentType . Episode ) )
{
return new TvShowsService ( _userManager , _userDataManager , _libraryManager , _itemRepo , _dtoService , _tvManager )
{
AuthorizationContext = AuthorizationContext ,
Logger = Logger ,
Request = Request ,
SessionContext = SessionContext ,
ResultFactory = ResultFactory
} . Get ( new GetSimilarShows
{
Fields = request . Fields ,
Id = request . Id ,
Limit = request . Limit ,
UserId = request . UserId
} ) ;
}
return new ItemsResult ( ) ;
2014-02-21 06:04:11 +01:00
}
public object Get ( GetMediaFolders request )
{
2015-11-03 05:34:47 +01:00
var items = _libraryManager . GetUserRootFolder ( ) . Children . Concat ( _libraryManager . RootFolder . VirtualChildren ) . OrderBy ( i = > i . SortName ) . ToList ( ) ;
2014-02-21 06:04:11 +01:00
2014-07-01 06:26:50 +02:00
if ( request . IsHidden . HasValue )
{
var val = request . IsHidden . Value ;
items = items . Where ( i = > i . IsHidden = = val ) . ToList ( ) ;
}
2015-01-24 20:03:55 +01:00
var dtoOptions = GetDtoOptions ( request ) ;
2015-02-06 06:39:07 +01:00
2014-02-21 06:04:11 +01:00
var result = new ItemsResult
2013-02-24 22:53:54 +01:00
{
2014-02-21 06:04:11 +01:00
TotalRecordCount = items . Count ,
2014-12-27 06:08:39 +01:00
Items = items . Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions ) ) . ToArray ( )
2014-02-21 06:04:11 +01:00
} ;
return ToOptimizedResult ( result ) ;
}
2014-08-05 05:41:56 +02:00
public void Post ( PostUpdatedSeries request )
{
2015-08-28 21:10:44 +02:00
var series = _libraryManager . GetItems ( new InternalItemsQuery
{
IncludeItemTypes = new [ ] { typeof ( Series ) . Name }
} ) . Items ;
series = series . Where ( i = > string . Equals ( request . TvdbId , i . GetProviderId ( MetadataProviders . Tvdb ) , StringComparison . OrdinalIgnoreCase ) ) . ToArray ( ) ;
if ( series . Length > 0 )
{
foreach ( var item in series )
{
_libraryMonitor . ReportFileSystemChanged ( item . Path ) ;
}
2015-09-02 17:33:20 +02:00
}
else
{
Task . Run ( ( ) = > _libraryManager . ValidateMediaLibrary ( new Progress < double > ( ) , CancellationToken . None ) ) ;
}
}
public void Post ( PostUpdatedMovies request )
{
var movies = _libraryManager . GetItems ( new InternalItemsQuery
{
IncludeItemTypes = new [ ] { typeof ( Movie ) . Name }
} ) . Items ;
if ( ! string . IsNullOrWhiteSpace ( request . ImdbId ) )
{
movies = movies . Where ( i = > string . Equals ( request . ImdbId , i . GetProviderId ( MetadataProviders . Imdb ) , StringComparison . OrdinalIgnoreCase ) ) . ToArray ( ) ;
}
else if ( ! string . IsNullOrWhiteSpace ( request . TmdbId ) )
{
movies = movies . Where ( i = > string . Equals ( request . TmdbId , i . GetProviderId ( MetadataProviders . Tmdb ) , StringComparison . OrdinalIgnoreCase ) ) . ToArray ( ) ;
}
else
{
movies = new BaseItem [ ] { } ;
}
if ( movies . Length > 0 )
{
foreach ( var item in movies )
{
_libraryMonitor . ReportFileSystemChanged ( item . Path ) ;
}
2015-08-28 21:10:44 +02:00
}
else
{
Task . Run ( ( ) = > _libraryManager . ValidateMediaLibrary ( new Progress < double > ( ) , CancellationToken . None ) ) ;
}
2014-08-05 05:41:56 +02:00
}
2015-02-06 06:39:07 +01:00
public object Get ( GetDownload request )
{
var item = _libraryManager . GetItemById ( request . Id ) ;
2015-02-12 04:54:31 +01:00
var auth = _authContext . GetAuthorizationInfo ( Request ) ;
2015-02-06 06:39:07 +01:00
2015-02-12 04:54:31 +01:00
var user = _userManager . GetUserById ( auth . UserId ) ;
if ( user ! = null )
{
if ( ! item . CanDownload ( user ) )
{
throw new ArgumentException ( "Item does not support downloading" ) ;
}
}
else
2015-02-06 06:39:07 +01:00
{
2015-02-12 04:54:31 +01:00
if ( ! item . CanDownload ( ) )
{
throw new ArgumentException ( "Item does not support downloading" ) ;
}
2015-02-06 06:39:07 +01:00
}
var headers = new Dictionary < string , string > ( ) ;
// Quotes are valid in linux. They'll possibly cause issues here
var filename = Path . GetFileName ( item . Path ) . Replace ( "\"" , string . Empty ) ;
2015-02-07 14:43:44 +01:00
headers [ "Content-Disposition" ] = string . Format ( "attachment; filename=\"{0}\"" , filename ) ;
2015-02-06 06:39:07 +01:00
2015-02-12 04:54:31 +01:00
if ( user ! = null )
{
LogDownload ( item , user , auth ) ;
}
2015-02-06 06:39:07 +01:00
return ResultFactory . GetStaticFileResult ( Request , new StaticFileResultOptions
{
Path = item . Path ,
ResponseHeaders = headers
} ) ;
}
2015-02-12 04:54:31 +01:00
private async void LogDownload ( BaseItem item , User user , AuthorizationInfo auth )
{
try
{
await _activityManager . Create ( new ActivityLogEntry
{
Name = string . Format ( _localization . GetLocalizedString ( "UserDownloadingItemWithValues" ) , user . Name , item . Name ) ,
Type = "UserDownloadingContent" ,
ShortOverview = string . Format ( _localization . GetLocalizedString ( "AppDeviceValues" ) , auth . Client , auth . Device ) ,
UserId = auth . UserId
} ) . ConfigureAwait ( false ) ;
}
catch
{
// Logged at lower levels
}
}
2014-02-21 06:04:11 +01:00
public object Get ( GetFile request )
{
2014-04-25 22:15:50 +02:00
var item = _libraryManager . GetItemById ( request . Id ) ;
2014-02-21 06:04:11 +01:00
var locationType = item . LocationType ;
if ( locationType = = LocationType . Remote | | locationType = = LocationType . Virtual )
{
throw new ArgumentException ( "This command cannot be used for remote or virtual items." ) ;
}
2015-09-13 23:32:02 +02:00
if ( _fileSystem . DirectoryExists ( item . Path ) )
2014-02-21 06:04:11 +01:00
{
throw new ArgumentException ( "This command cannot be used for directories." ) ;
2013-02-24 22:53:54 +01:00
}
2014-02-21 06:04:11 +01:00
return ToStaticFileResult ( item . Path ) ;
}
2013-02-21 02:33:05 +01:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetPhyscialPaths request )
{
2013-12-20 17:51:25 +01:00
var result = _libraryManager . RootFolder . Children
2014-02-10 21:11:46 +01:00
. SelectMany ( c = > c . PhysicalLocations )
2013-12-20 17:51:25 +01:00
. ToList ( ) ;
2013-02-21 02:33:05 +01:00
2014-02-04 05:04:19 +01:00
return ToOptimizedSerializedResultUsingCache ( result ) ;
2013-02-21 02:33:05 +01:00
}
2014-02-21 06:04:11 +01:00
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetAncestors request )
{
var result = GetAncestors ( request ) ;
return ToOptimizedSerializedResultUsingCache ( result ) ;
}
/// <summary>
/// Gets the ancestors.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{BaseItemDto[]}.</returns>
public List < BaseItemDto > GetAncestors ( GetAncestors request )
{
2014-04-25 22:15:50 +02:00
var item = _libraryManager . GetItemById ( request . Id ) ;
2014-02-21 06:04:11 +01:00
var baseItemDtos = new List < BaseItemDto > ( ) ;
2015-05-30 01:51:33 +02:00
var user = ! string . IsNullOrWhiteSpace ( request . UserId ) ? _userManager . GetUserById ( request . UserId ) : null ;
2014-02-21 06:04:11 +01:00
2015-01-24 20:03:55 +01:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-02-21 06:04:11 +01:00
2015-11-11 15:56:31 +01:00
BaseItem parent = item . GetParent ( ) ;
2015-02-06 06:39:07 +01:00
2014-02-21 06:04:11 +01:00
while ( parent ! = null )
{
if ( user ! = null )
{
parent = TranslateParentItem ( parent , user ) ;
}
2014-12-27 06:08:39 +01:00
baseItemDtos . Add ( _dtoService . GetBaseItemDto ( parent , dtoOptions , user ) ) ;
2014-02-21 06:04:11 +01:00
2015-11-11 15:56:31 +01:00
parent = parent . GetParent ( ) ;
2014-02-21 06:04:11 +01:00
}
return baseItemDtos . ToList ( ) ;
}
private BaseItem TranslateParentItem ( BaseItem item , User user )
{
2015-11-11 15:56:31 +01:00
if ( item . GetParent ( ) is AggregateFolder )
2014-02-21 06:04:11 +01:00
{
return user . RootFolder . GetChildren ( user , true ) . FirstOrDefault ( i = > i . PhysicalLocations . Contains ( item . Path ) ) ;
}
return item ;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetCriticReviews request )
{
var result = GetCriticReviews ( request ) ;
return ToOptimizedSerializedResultUsingCache ( result ) ;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetItemCounts request )
{
2015-01-25 07:34:50 +01:00
var filteredItems = GetAllLibraryItems ( request . UserId , _userManager , _libraryManager , null , i = > i . LocationType ! = LocationType . Virtual & & FilterItem ( i , request , request . UserId ) ) ;
2014-02-21 06:04:11 +01:00
var counts = new ItemCounts
{
2015-01-25 07:34:50 +01:00
AlbumCount = filteredItems . Count ( i = > i is MusicAlbum ) ,
EpisodeCount = filteredItems . Count ( i = > i is Episode ) ,
GameCount = filteredItems . Count ( i = > i is Game ) ,
GameSystemCount = filteredItems . Count ( i = > i is GameSystem ) ,
MovieCount = filteredItems . Count ( i = > i is Movie ) ,
SeriesCount = filteredItems . Count ( i = > i is Series ) ,
SongCount = filteredItems . Count ( i = > i is Audio ) ,
MusicVideoCount = filteredItems . Count ( i = > i is MusicVideo ) ,
BoxSetCount = filteredItems . Count ( i = > i is BoxSet ) ,
BookCount = filteredItems . Count ( i = > i is Book ) ,
UniqueTypes = filteredItems . Select ( i = > i . GetClientTypeName ( ) ) . Distinct ( ) . ToList ( )
2014-02-21 06:04:11 +01:00
} ;
return ToOptimizedSerializedResultUsingCache ( counts ) ;
}
2015-11-10 19:58:05 +01:00
private IList < BaseItem > GetAllLibraryItems ( string userId , IUserManager userManager , ILibraryManager libraryManager , string parentId , Func < BaseItem , bool > filter )
{
if ( ! string . IsNullOrEmpty ( parentId ) )
{
var folder = ( Folder ) libraryManager . GetItemById ( new Guid ( parentId ) ) ;
if ( ! string . IsNullOrWhiteSpace ( userId ) )
{
var user = userManager . GetUserById ( userId ) ;
if ( user = = null )
{
throw new ArgumentException ( "User not found" ) ;
}
return folder
. GetRecursiveChildren ( user , filter )
. ToList ( ) ;
}
return folder
. GetRecursiveChildren ( filter ) ;
}
if ( ! string . IsNullOrWhiteSpace ( userId ) )
{
var user = userManager . GetUserById ( userId ) ;
if ( user = = null )
{
throw new ArgumentException ( "User not found" ) ;
}
return userManager
. GetUserById ( userId )
. RootFolder
. GetRecursiveChildren ( user , filter )
. ToList ( ) ;
}
return libraryManager
. RootFolder
. GetRecursiveChildren ( filter ) ;
}
2015-05-30 01:51:33 +02:00
private bool FilterItem ( BaseItem item , GetItemCounts request , string userId )
2014-02-21 06:04:11 +01:00
{
2015-05-30 01:51:33 +02:00
if ( ! string . IsNullOrWhiteSpace ( userId ) )
2014-02-21 06:04:11 +01:00
{
2015-01-25 07:34:50 +01:00
if ( request . IsFavorite . HasValue )
{
var val = request . IsFavorite . Value ;
2014-02-21 06:04:11 +01:00
2015-05-30 01:51:33 +02:00
if ( _userDataManager . GetUserData ( userId , item . GetUserDataKey ( ) ) . IsFavorite ! = val )
2015-01-25 07:34:50 +01:00
{
return false ;
}
}
2014-02-21 06:04:11 +01:00
}
2015-01-25 07:34:50 +01:00
return true ;
2014-02-21 06:04:11 +01:00
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
2014-04-04 00:50:04 +02:00
public void Post ( RefreshLibrary request )
2014-02-21 06:04:11 +01:00
{
try
{
2014-04-04 00:50:04 +02:00
_libraryManager . ValidateMediaLibrary ( new Progress < double > ( ) , CancellationToken . None ) ;
2014-02-21 06:04:11 +01:00
}
catch ( Exception ex )
{
Logger . ErrorException ( "Error refreshing library" , ex ) ;
}
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
2014-08-18 16:20:02 +02:00
public void Delete ( DeleteItem request )
2014-02-21 06:04:11 +01:00
{
2014-04-25 22:15:50 +02:00
var item = _libraryManager . GetItemById ( request . Id ) ;
2014-11-19 03:45:12 +01:00
var auth = _authContext . GetAuthorizationInfo ( Request ) ;
var user = _userManager . GetUserById ( auth . UserId ) ;
2015-02-06 06:39:07 +01:00
if ( ! item . CanDelete ( user ) )
2014-11-19 03:45:12 +01:00
{
2015-09-26 04:31:13 +02:00
throw new SecurityException ( "Unauthorized access" ) ;
2014-11-19 03:45:12 +01:00
}
2015-08-03 04:12:52 +02:00
if ( item is ILiveTvRecording )
{
var task = _liveTv . DeleteRecording ( request . Id ) ;
Task . WaitAll ( task ) ;
}
else
{
var task = _libraryManager . DeleteItem ( item ) ;
Task . WaitAll ( task ) ;
}
2014-02-21 06:04:11 +01:00
}
/// <summary>
/// Gets the critic reviews async.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>Task{ItemReviewsResult}.</returns>
private QueryResult < ItemReview > GetCriticReviews ( GetCriticReviews request )
{
var reviews = _itemRepo . GetCriticReviews ( new Guid ( request . Id ) ) ;
var reviewsArray = reviews . ToArray ( ) ;
var result = new QueryResult < ItemReview >
{
TotalRecordCount = reviewsArray . Length
} ;
if ( request . StartIndex . HasValue )
{
reviewsArray = reviewsArray . Skip ( request . StartIndex . Value ) . ToArray ( ) ;
}
if ( request . Limit . HasValue )
{
reviewsArray = reviewsArray . Take ( request . Limit . Value ) . ToArray ( ) ;
}
result . Items = reviewsArray ;
return result ;
}
public object Get ( GetThemeMedia request )
{
var themeSongs = GetThemeSongs ( new GetThemeSongs
{
InheritFromParent = request . InheritFromParent ,
Id = request . Id ,
UserId = request . UserId
} ) ;
var themeVideos = GetThemeVideos ( new GetThemeVideos
{
InheritFromParent = request . InheritFromParent ,
Id = request . Id ,
UserId = request . UserId
} ) ;
return ToOptimizedSerializedResultUsingCache ( new AllThemeMediaResult
{
ThemeSongsResult = themeSongs ,
ThemeVideosResult = themeVideos ,
2015-04-26 05:25:07 +02:00
SoundtrackSongsResult = new ThemeMediaResult ( )
2014-02-21 06:04:11 +01:00
} ) ;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetThemeSongs request )
{
var result = GetThemeSongs ( request ) ;
return ToOptimizedSerializedResultUsingCache ( result ) ;
}
private ThemeMediaResult GetThemeSongs ( GetThemeSongs request )
{
2015-05-30 01:51:33 +02:00
var user = ! string . IsNullOrWhiteSpace ( request . UserId ) ? _userManager . GetUserById ( request . UserId ) : null ;
2014-02-21 06:04:11 +01:00
var item = string . IsNullOrEmpty ( request . Id )
2015-05-30 01:51:33 +02:00
? ( ! string . IsNullOrWhiteSpace ( request . UserId )
2014-02-21 06:04:11 +01:00
? user . RootFolder
: ( Folder ) _libraryManager . RootFolder )
2014-04-25 22:15:50 +02:00
: _libraryManager . GetItemById ( request . Id ) ;
2014-02-21 06:04:11 +01:00
2015-11-11 15:56:31 +01:00
while ( GetThemeSongIds ( item ) . Count = = 0 & & request . InheritFromParent & & item . GetParent ( ) ! = null )
2014-02-21 06:04:11 +01:00
{
2015-11-11 15:56:31 +01:00
item = item . GetParent ( ) ;
2014-02-21 06:04:11 +01:00
}
2015-01-24 20:03:55 +01:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-12-27 06:08:39 +01:00
2015-05-04 16:35:38 +02:00
var dtos = GetThemeSongIds ( item ) . Select ( _libraryManager . GetItemById )
2014-02-21 06:04:11 +01:00
. OrderBy ( i = > i . SortName )
2014-12-27 06:08:39 +01:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user , item ) ) ;
2014-02-21 06:04:11 +01:00
var items = dtos . ToArray ( ) ;
return new ThemeMediaResult
{
Items = items ,
TotalRecordCount = items . Length ,
OwnerId = _dtoService . GetDtoId ( item )
} ;
}
/// <summary>
/// Gets the specified request.
/// </summary>
/// <param name="request">The request.</param>
/// <returns>System.Object.</returns>
public object Get ( GetThemeVideos request )
{
var result = GetThemeVideos ( request ) ;
return ToOptimizedSerializedResultUsingCache ( result ) ;
}
public ThemeMediaResult GetThemeVideos ( GetThemeVideos request )
{
2015-05-30 01:51:33 +02:00
var user = ! string . IsNullOrWhiteSpace ( request . UserId ) ? _userManager . GetUserById ( request . UserId ) : null ;
2014-02-21 06:04:11 +01:00
var item = string . IsNullOrEmpty ( request . Id )
2015-05-30 01:51:33 +02:00
? ( ! string . IsNullOrWhiteSpace ( request . UserId )
2014-02-21 06:04:11 +01:00
? user . RootFolder
: ( Folder ) _libraryManager . RootFolder )
2014-04-25 22:15:50 +02:00
: _libraryManager . GetItemById ( request . Id ) ;
2014-02-21 06:04:11 +01:00
2015-11-11 15:56:31 +01:00
while ( GetThemeVideoIds ( item ) . Count = = 0 & & request . InheritFromParent & & item . GetParent ( ) ! = null )
2014-02-21 06:04:11 +01:00
{
2015-11-11 15:56:31 +01:00
item = item . GetParent ( ) ;
2014-02-21 06:04:11 +01:00
}
2015-01-24 20:03:55 +01:00
var dtoOptions = GetDtoOptions ( request ) ;
2014-12-27 06:08:39 +01:00
2015-05-04 16:35:38 +02:00
var dtos = GetThemeVideoIds ( item ) . Select ( _libraryManager . GetItemById )
2014-02-21 06:04:11 +01:00
. OrderBy ( i = > i . SortName )
2014-12-27 06:08:39 +01:00
. Select ( i = > _dtoService . GetBaseItemDto ( i , dtoOptions , user , item ) ) ;
2014-02-21 06:04:11 +01:00
var items = dtos . ToArray ( ) ;
return new ThemeMediaResult
{
Items = items ,
TotalRecordCount = items . Length ,
OwnerId = _dtoService . GetDtoId ( item )
} ;
}
private List < Guid > GetThemeVideoIds ( BaseItem item )
{
var i = item as IHasThemeMedia ;
if ( i ! = null )
{
return i . ThemeVideoIds ;
}
return new List < Guid > ( ) ;
}
private List < Guid > GetThemeSongIds ( BaseItem item )
{
var i = item as IHasThemeMedia ;
if ( i ! = null )
{
return i . ThemeSongIds ;
}
return new List < Guid > ( ) ;
}
private readonly CultureInfo _usCulture = new CultureInfo ( "en-US" ) ;
public object Get ( GetYearIndex request )
{
2015-01-25 07:34:50 +01:00
var includeTypes = string . IsNullOrWhiteSpace ( request . IncludeItemTypes )
? new string [ ] { }
: request . IncludeItemTypes . Split ( ',' ) ;
2014-02-21 06:04:11 +01:00
2015-01-25 07:34:50 +01:00
Func < BaseItem , bool > filter = i = >
2014-02-21 06:04:11 +01:00
{
2015-01-25 07:34:50 +01:00
if ( includeTypes . Length > 0 )
{
if ( ! includeTypes . Contains ( i . GetType ( ) . Name , StringComparer . OrdinalIgnoreCase ) )
{
return false ;
}
}
return true ;
} ;
IEnumerable < BaseItem > items = GetAllLibraryItems ( request . UserId , _userManager , _libraryManager , null , filter ) ;
2014-02-21 06:04:11 +01:00
var lookup = items
. ToLookup ( i = > i . ProductionYear ? ? - 1 )
. OrderBy ( i = > i . Key )
. Select ( i = > new ItemIndex
{
ItemCount = i . Count ( ) ,
Name = i . Key = = - 1 ? string . Empty : i . Key . ToString ( _usCulture )
} )
. ToList ( ) ;
return ToOptimizedSerializedResultUsingCache ( lookup ) ;
}
2013-02-21 02:33:05 +01:00
}
}