diff --git a/Emby.Server.Implementations/Services/SwaggerService.cs b/Emby.Server.Implementations/Services/SwaggerService.cs index be3b4cbba4..fc2bdbd557 100644 --- a/Emby.Server.Implementations/Services/SwaggerService.cs +++ b/Emby.Server.Implementations/Services/SwaggerService.cs @@ -23,6 +23,19 @@ namespace Emby.Server.Implementations.Services public SwaggerTag[] tags { get; set; } public IDictionary> paths { get; set; } public Dictionary definitions { get; set; } + public SwaggerComponents components { get; set; } + } + + public class SwaggerComponents + { + public Dictionary securitySchemes { get; set; } + } + + public class SwaggerSecurityScheme + { + public string name { get; set; } + public string type { get; set; } + public string @in { get; set; } } public class SwaggerInfo @@ -38,6 +51,8 @@ namespace Emby.Server.Implementations.Services public class SwaggerConcactInfo { public string email { get; set; } + public string name { get; set; } + public string url { get; set; } } public class SwaggerTag @@ -56,6 +71,7 @@ namespace Emby.Server.Implementations.Services public string[] produces { get; set; } public SwaggerParam[] parameters { get; set; } public Dictionary responses { get; set; } + public Dictionary[] security { get; set; } } public class SwaggerParam @@ -111,6 +127,15 @@ namespace Emby.Server.Implementations.Services host = uri.Host; } + var securitySchemes = new Dictionary(); + + securitySchemes["api_key"] = new SwaggerSecurityScheme + { + name = "api_key", + type = "apiKey", + @in = "query" + }; + var spec = new SwaggerSpec { schemes = new[] { "http" }, @@ -123,14 +148,20 @@ namespace Emby.Server.Implementations.Services description = "Explore the Emby Server API", contact = new SwaggerConcactInfo { - email = "api@emby.media" + name = "Emby Developer Community", + url = "https://emby.media/community/index.php?/forum/47-developer-api" }, termsOfService = "https://emby.media/terms" }, paths = GetPaths(), definitions = GetDefinitions(), basePath = "/emby", - host = host + host = host, + + components = new SwaggerComponents + { + securitySchemes = securitySchemes + } }; return spec; @@ -193,6 +224,13 @@ namespace Emby.Server.Implementations.Services description = "OK" }; + var security = new List>(); + + var apiKeySecurity = new Dictionary(); + apiKeySecurity["api_key"] = new string[] { }; + + security.Add(apiKeySecurity); + result[verb.ToLower()] = new SwaggerMethod { summary = info.Summary, @@ -210,7 +248,9 @@ namespace Emby.Server.Implementations.Services parameters = new SwaggerParam[] { }, - responses = responses + responses = responses, + + security = security.ToArray() }; } diff --git a/MediaBrowser.Api/MediaBrowser.Api.csproj b/MediaBrowser.Api/MediaBrowser.Api.csproj index 602a697bf2..ddb187f3d9 100644 --- a/MediaBrowser.Api/MediaBrowser.Api.csproj +++ b/MediaBrowser.Api/MediaBrowser.Api.csproj @@ -50,28 +50,6 @@ - - - - - - - - - - - - - - - - - - - - - - diff --git a/MediaBrowser.Api/Reports/Activities/ReportActivitiesBuilder.cs b/MediaBrowser.Api/Reports/Activities/ReportActivitiesBuilder.cs deleted file mode 100644 index 8fe5246f62..0000000000 --- a/MediaBrowser.Api/Reports/Activities/ReportActivitiesBuilder.cs +++ /dev/null @@ -1,256 +0,0 @@ -using MediaBrowser.Model.Activity; -using MediaBrowser.Model.Querying; -using System.Collections.Generic; -using System.Linq; -using MediaBrowser.Controller.Library; -namespace MediaBrowser.Api.Reports -{ - /// A report activities builder. - /// - public class ReportActivitiesBuilder : ReportBuilderBase - { - #region [Constructors] - - /// - /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportActivitiesBuilder class. - /// Manager for library. - /// Manager for user. - public ReportActivitiesBuilder(ILibraryManager libraryManager, IUserManager userManager) - : base(libraryManager) - { - _userManager = userManager; - } - - #endregion - - #region [Private Fields] - - private readonly IUserManager _userManager; ///< Manager for user - - #endregion - - #region [Public Methods] - - /// Gets a result. - /// The query result. - /// The request. - /// The result. - public ReportResult GetResult(QueryResult queryResult, IReportsQuery request) - { - ReportDisplayType displayType = ReportHelper.GetReportDisplayType(request.DisplayType); - List> options = this.GetReportOptions(request, - () => this.GetDefaultHeaderMetadata(), - (hm) => this.GetOption(hm)).Where(x => this.DisplayTypeVisible(x.Header.DisplayType, displayType)).ToList(); - - var headers = GetHeaders(options); - var rows = GetReportRows(queryResult.Items, options); - - ReportResult result = new ReportResult { Headers = headers }; - HeaderMetadata groupBy = ReportHelper.GetHeaderMetadataType(request.GroupBy); - int i = headers.FindIndex(x => x.FieldName == groupBy); - if (groupBy != HeaderMetadata.None && i >= 0) - { - var rowsGroup = rows.SelectMany(x => x.Columns[i].Name.Split(';'), (x, g) => new { Group = g.Trim(), Rows = x }) - .GroupBy(x => x.Group) - .OrderBy(x => x.Key) - .Select(x => new ReportGroup { Name = x.Key, Rows = x.Select(r => r.Rows).ToList() }); - - result.Groups = rowsGroup.ToList(); - result.IsGrouped = true; - } - else - { - result.Rows = rows; - result.IsGrouped = false; - } - - return result; - } - - #endregion - - #region [Protected Internal Methods] - - /// Gets the headers. - /// Type of the header. - /// The request. - /// The headers. - /// - protected internal override List GetHeaders(H request) - { - return this.GetHeaders(request, () => this.GetDefaultHeaderMetadata(), (hm) => this.GetOption(hm)); - } - - #endregion - - #region [Private Methods] - - /// Gets default header metadata. - /// The default header metadata. - private List GetDefaultHeaderMetadata() - { - return new List - { - HeaderMetadata.UserPrimaryImage, - HeaderMetadata.Date, - HeaderMetadata.User, - HeaderMetadata.Type, - HeaderMetadata.Severity, - HeaderMetadata.Name, - HeaderMetadata.ShortOverview, - HeaderMetadata.Overview, - //HeaderMetadata.UserId - //HeaderMetadata.Item, - }; - } - - /// Gets an option. - /// The header. - /// The sort field. - /// The option. - private ReportOptions GetOption(HeaderMetadata header, string sortField = "") - { - HeaderMetadata internalHeader = header; - - ReportOptions option = new ReportOptions() - { - Header = new ReportHeader - { - HeaderFieldType = ReportFieldType.String, - SortField = sortField, - Type = "", - ItemViewType = ItemViewType.None - } - }; - - switch (header) - { - case HeaderMetadata.Name: - option.Column = (i, r) => i.Name; - option.Header.SortField = ""; - break; - case HeaderMetadata.Overview: - option.Column = (i, r) => i.Overview; - option.Header.SortField = ""; - option.Header.CanGroup = false; - break; - - case HeaderMetadata.ShortOverview: - option.Column = (i, r) => i.ShortOverview; - option.Header.SortField = ""; - option.Header.CanGroup = false; - break; - - case HeaderMetadata.Type: - option.Column = (i, r) => i.Type; - option.Header.SortField = ""; - break; - - case HeaderMetadata.Date: - option.Column = (i, r) => i.Date; - option.Header.SortField = ""; - option.Header.HeaderFieldType = ReportFieldType.DateTime; - option.Header.Type = ""; - break; - - case HeaderMetadata.UserPrimaryImage: - //option.Column = (i, r) => i.UserPrimaryImageTag; - option.Header.DisplayType = ReportDisplayType.Screen; - option.Header.ItemViewType = ItemViewType.UserPrimaryImage; - option.Header.ShowHeaderLabel = false; - internalHeader = HeaderMetadata.User; - option.Header.CanGroup = false; - option.Column = (i, r) => - { - if (!string.IsNullOrEmpty(i.UserId)) - { - MediaBrowser.Controller.Entities.User user = _userManager.GetUserById(i.UserId); - if (user != null) - { - var dto = _userManager.GetUserDto(user); - return dto.PrimaryImageTag; - } - } - return string.Empty; - }; - option.Header.SortField = ""; - break; - case HeaderMetadata.Severity: - option.Column = (i, r) => i.Severity; - option.Header.SortField = ""; - break; - case HeaderMetadata.Item: - option.Column = (i, r) => i.ItemId; - option.Header.SortField = ""; - break; - case HeaderMetadata.User: - option.Column = (i, r) => - { - if (!string.IsNullOrEmpty(i.UserId)) - { - MediaBrowser.Controller.Entities.User user = _userManager.GetUserById(i.UserId); - if (user != null) - return user.Name; - } - return string.Empty; - }; - option.Header.SortField = ""; - break; - case HeaderMetadata.UserId: - option.Column = (i, r) => i.UserId; - option.Header.SortField = ""; - break; - } - - option.Header.Name = GetLocalizedHeader(internalHeader); - option.Header.FieldName = header; - - return option; - } - - /// Gets report rows. - /// The items. - /// Options for controlling the operation. - /// The report rows. - private List GetReportRows(IEnumerable items, List> options) - { - var rows = new List(); - - foreach (ActivityLogEntry item in items) - { - ReportRow rRow = GetRow(item); - foreach (ReportOptions option in options) - { - object itemColumn = option.Column != null ? option.Column(item, rRow) : ""; - object itemId = option.ItemID != null ? option.ItemID(item) : ""; - ReportItem rItem = new ReportItem - { - Name = ReportHelper.ConvertToString(itemColumn, option.Header.HeaderFieldType), - Id = ReportHelper.ConvertToString(itemId, ReportFieldType.Object) - }; - rRow.Columns.Add(rItem); - } - - rows.Add(rRow); - } - - return rows; - } - - /// Gets a row. - /// The item. - /// The row. - private ReportRow GetRow(ActivityLogEntry item) - { - ReportRow rRow = new ReportRow - { - Id = item.Id, - UserId = item.UserId - }; - return rRow; - } - - #endregion - - } -} diff --git a/MediaBrowser.Api/Reports/Common/HeaderActivitiesMetadata.cs b/MediaBrowser.Api/Reports/Common/HeaderActivitiesMetadata.cs deleted file mode 100644 index 4a45f2646e..0000000000 --- a/MediaBrowser.Api/Reports/Common/HeaderActivitiesMetadata.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum HeaderActivitiesMetadata - { - None, - Name, - Overview, - ShortOverview, - Type, - Date, - UserPrimaryImageTag, - Severity, - Item, - User - } -} diff --git a/MediaBrowser.Api/Reports/Common/HeaderMetadata.cs b/MediaBrowser.Api/Reports/Common/HeaderMetadata.cs deleted file mode 100644 index e25e78802c..0000000000 --- a/MediaBrowser.Api/Reports/Common/HeaderMetadata.cs +++ /dev/null @@ -1,69 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum HeaderMetadata - { - None, - Name, - PremiereDate, - DateAdded, - ReleaseDate, - Runtime, - PlayCount, - Season, - SeasonNumber, - Series, - Network, - Year, - ParentalRating, - CommunityRating, - Trailers, - Specials, - GameSystem, - Players, - AlbumArtist, - Album, - Disc, - Track, - Audio, - EmbeddedImage, - Video, - Resolution, - Subtitles, - Genres, - Countries, - Status, - Tracks, - EpisodeSeries, - EpisodeSeason, - EpisodeNumber, - AudioAlbumArtist, - MusicArtist, - AudioAlbum, - Locked, - ImagePrimary, - ImageBackdrop, - ImageLogo, - Actor, - Studios, - Composer, - Director, - GuestStar, - Producer, - Writer, - Artist, - Years, - ParentalRatings, - CommunityRatings, - - //Activity logs - Overview, - ShortOverview, - Type, - Date, - UserPrimaryImage, - Severity, - Item, - User, - UserId - } -} diff --git a/MediaBrowser.Api/Reports/Common/ItemViewType.cs b/MediaBrowser.Api/Reports/Common/ItemViewType.cs deleted file mode 100644 index ede6705e0d..0000000000 --- a/MediaBrowser.Api/Reports/Common/ItemViewType.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum ItemViewType - { - None, - Detail, - Edit, - List, - ItemByNameDetails, - StatusImage, - EmbeddedImage, - SubtitleImage, - TrailersImage, - SpecialsImage, - LockDataImage, - TagsPrimaryImage, - TagsBackdropImage, - TagsLogoImage, - UserPrimaryImage - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs b/MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs deleted file mode 100644 index 6d5a180fb9..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportBuilderBase.cs +++ /dev/null @@ -1,365 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Entities.TV; -using MediaBrowser.Controller.Library; -using MediaBrowser.Model.Channels; -using MediaBrowser.Model.Dto; -using MediaBrowser.Model.Entities; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace MediaBrowser.Api.Reports -{ - /// A report builder base. - public abstract class ReportBuilderBase - { - - #region [Constructors] - - /// - /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportBuilderBase class. - /// Manager for library. - public ReportBuilderBase(ILibraryManager libraryManager) - { - _libraryManager = libraryManager; - } - - #endregion - - #region [Protected Fields] - - /// Manager for library. - protected readonly ILibraryManager _libraryManager; ///< Manager for library - - protected Func GetBoolString = s => s == true ? "x" : ""; ///< . - - #endregion - - #region [Protected Internal Methods] - - /// Gets the headers. - /// Type of the header. - /// The request. - /// The headers. - protected internal abstract List GetHeaders(H request) where H : IReportsHeader; - - #endregion - - #region [Protected Methods] - - /// Gets active headers. - /// Generic type parameter. - /// Options for controlling the operation. - /// The active headers. - protected List GetActiveHeaders(List> options, ReportDisplayType displayType) - { - List headers = new List(); - foreach (ReportOptions option in options.Where(x => this.DisplayTypeVisible(x.Header.DisplayType, displayType))) - { - headers.Add(option.Header); - } - - return headers; - } - - /// Gets audio stream. - /// The item. - /// The audio stream. - protected string GetAudioStream(BaseItem item) - { - var stream = GetStream(item, MediaStreamType.Audio); - if (stream != null) - return stream.Codec.ToUpper() == "DCA" ? stream.Profile : stream.Codec. - ToUpper(); - - return string.Empty; - } - - /// Gets an episode. - /// The item. - /// The episode. - protected string GetEpisode(BaseItem item) - { - - if (item.GetClientTypeName() == ChannelMediaContentType.Episode.ToString() && item.ParentIndexNumber != null) - return "Season " + item.ParentIndexNumber; - else - return item.Name; - } - - /// Gets a genre. - /// The name. - /// The genre. - protected Genre GetGenre(string name) - { - if (string.IsNullOrEmpty(name)) - return null; - return _libraryManager.GetGenre(name); - } - - /// Gets genre identifier. - /// The name. - /// The genre identifier. - protected string GetGenreID(string name) - { - if (string.IsNullOrEmpty(name)) - return string.Empty; - return string.Format("{0:N}", - GetGenre(name).Id); - } - - /// Gets the headers. - /// Generic type parameter. - /// Options for controlling the operation. - /// The headers. - protected List GetHeaders(List> options) - { - List headers = new List(); - foreach (ReportOptions option in options) - { - headers.Add(option.Header); - } - - return headers; - } - - /// Gets the headers. - /// Generic type parameter. - /// The request. - /// The get headers metadata. - /// Options for controlling the get. - /// The headers. - protected List GetHeaders(IReportsHeader request, Func> getHeadersMetadata, Func> getOptions) - { - List> options = this.GetReportOptions(request, getHeadersMetadata, getOptions); - return this.GetHeaders(options); - } - - /// Gets list as string. - /// The items. - /// The list as string. - protected string GetListAsString(List items) - { - return String.Join("; ", items); - } - - /// Gets localized header. - /// The internal header. - /// The localized header. - protected static string GetLocalizedHeader(HeaderMetadata internalHeader) - { - if (internalHeader == HeaderMetadata.EpisodeNumber) - { - return "Episode"; - } - - string headerName = ""; - if (internalHeader != HeaderMetadata.None) - { - string localHeader = "Header" + internalHeader.ToString(); - headerName = ReportHelper.GetCoreLocalizedString(localHeader); - } - return headerName; - } - - /// Gets media source information. - /// The item. - /// The media source information. - protected MediaSourceInfo GetMediaSourceInfo(BaseItem item) - { - var mediaSource = item as IHasMediaSources; - if (mediaSource != null) - return mediaSource.GetMediaSources(false).FirstOrDefault(n => n.Type == MediaSourceType.Default); - - return null; - } - - /// Gets an object. - /// Generic type parameter. - /// Type of the r. - /// The item. - /// The function. - /// The default value. - /// The object. - protected R GetObject(BaseItem item, Func function, R defaultValue = default(R)) where T : class - { - var value = item as T; - if (value != null && function != null) - return function(value); - else - return defaultValue; - } - - /// Gets a person. - /// The name. - /// The person. - protected Person GetPerson(string name) - { - if (string.IsNullOrEmpty(name)) - return null; - return _libraryManager.GetPerson(name); - } - - /// Gets person identifier. - /// The name. - /// The person identifier. - protected string GetPersonID(string name) - { - if (string.IsNullOrEmpty(name)) - return string.Empty; - return string.Format("{0:N}", - GetPerson(name).Id); - } - - /// Gets report options. - /// Generic type parameter. - /// The request. - /// The get headers metadata. - /// Options for controlling the get. - /// The report options. - protected List> GetReportOptions(IReportsHeader request, Func> getHeadersMetadata, Func> getOptions) - { - List headersMetadata = getHeadersMetadata(); - List> options = new List>(); - ReportDisplayType displayType = ReportHelper.GetReportDisplayType(request.DisplayType); - foreach (HeaderMetadata header in headersMetadata) - { - ReportOptions headerOptions = getOptions(header); - if (this.DisplayTypeVisible(headerOptions.Header.DisplayType, displayType)) - options.Add(headerOptions); - } - - if (request != null && !string.IsNullOrEmpty(request.ReportColumns)) - { - List headersMetadataFiltered = ReportHelper.GetFilteredReportHeaderMetadata(request.ReportColumns, () => headersMetadata); - foreach (ReportHeader header in options.Select(x => x.Header)) - { - - if (this.DisplayTypeVisible(header.DisplayType, displayType)) - { - - if (!headersMetadataFiltered.Contains(header.FieldName) && displayType != ReportDisplayType.Export) - { - header.DisplayType = ReportDisplayType.None; - } - } - else - header.DisplayType = ReportDisplayType.None; - } - } - - return options; - } - - /// Gets runtime date time. - /// The runtime. - /// The runtime date time. - protected double? GetRuntimeDateTime(long? runtime) - { - if (runtime.HasValue) - return Math.Ceiling(new TimeSpan(runtime.Value).TotalMinutes); - return null; - } - - /// Gets series production year. - /// The item. - /// The series production year. - protected string GetSeriesProductionYear(BaseItem item) - { - - string productionYear = item.ProductionYear.ToString(); - var series = item as Series; - if (series == null) - { - if (item.ProductionYear == null || item.ProductionYear == 0) - return string.Empty; - return productionYear; - } - - if (series.Status == SeriesStatus.Continuing) - return productionYear += "-Present"; - - if (series.EndDate != null && series.EndDate.Value.Year != series.ProductionYear) - return productionYear += "-" + series.EndDate.Value.Year; - - return productionYear; - } - - /// Gets a stream. - /// The item. - /// Type of the stream. - /// The stream. - protected MediaStream GetStream(BaseItem item, MediaStreamType streamType) - { - var itemInfo = GetMediaSourceInfo(item); - if (itemInfo != null) - return itemInfo.MediaStreams.FirstOrDefault(n => n.Type == streamType); - - return null; - } - - /// Gets a studio. - /// The name. - /// The studio. - protected Studio GetStudio(string name) - { - if (string.IsNullOrEmpty(name)) - return null; - return _libraryManager.GetStudio(name); - } - - /// Gets studio identifier. - /// The name. - /// The studio identifier. - protected string GetStudioID(string name) - { - if (string.IsNullOrEmpty(name)) - return string.Empty; - return string.Format("{0:N}", - GetStudio(name).Id); - } - - /// Gets video resolution. - /// The item. - /// The video resolution. - protected string GetVideoResolution(BaseItem item) - { - var stream = GetStream(item, - MediaStreamType.Video); - if (stream != null && stream.Width != null) - return string.Format("{0} * {1}", - stream.Width, - stream.Height != null ? stream.Height.ToString() : "-"); - - return string.Empty; - } - - /// Gets video stream. - /// The item. - /// The video stream. - protected string GetVideoStream(BaseItem item) - { - var stream = GetStream(item, MediaStreamType.Video); - if (stream != null) - return stream.Codec.ToUpper(); - - return string.Empty; - } - - /// Displays a type visible. - /// Type of the header display. - /// Type of the display. - /// true if it succeeds, false if it fails. - protected bool DisplayTypeVisible(ReportDisplayType headerDisplayType, ReportDisplayType displayType) - { - if (headerDisplayType == ReportDisplayType.None) - return false; - - bool rval = headerDisplayType == displayType || headerDisplayType == ReportDisplayType.ScreenExport && (displayType == ReportDisplayType.Screen || displayType == ReportDisplayType.Export); - return rval; - } - - #endregion - - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportDisplayType.cs b/MediaBrowser.Api/Reports/Common/ReportDisplayType.cs deleted file mode 100644 index 681bb39283..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportDisplayType.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum ReportDisplayType - { - None, - Screen, - Export, - ScreenExport - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportExportType.cs b/MediaBrowser.Api/Reports/Common/ReportExportType.cs deleted file mode 100644 index 4ac4cad21d..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportExportType.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum ReportExportType - { - CSV, - Excel - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportFieldType.cs b/MediaBrowser.Api/Reports/Common/ReportFieldType.cs deleted file mode 100644 index 63c48dff05..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportFieldType.cs +++ /dev/null @@ -1,15 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum ReportFieldType - { - String, - Boolean, - Date, - Time, - DateTime, - Int, - Image, - Object, - Minutes - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportHeaderIdType.cs b/MediaBrowser.Api/Reports/Common/ReportHeaderIdType.cs deleted file mode 100644 index 32b2bc04d1..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportHeaderIdType.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum ReportHeaderIdType - { - Row, - Item - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportHelper.cs b/MediaBrowser.Api/Reports/Common/ReportHelper.cs deleted file mode 100644 index 9dc4fbd51e..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportHelper.cs +++ /dev/null @@ -1,138 +0,0 @@ -using MediaBrowser.Controller.Entities; -using System; -using System.Collections.Generic; -using System.Linq; - -namespace MediaBrowser.Api.Reports -{ - /// A report helper. - public class ReportHelper - { - #region [Public Methods] - - /// Convert field to string. - /// Generic type parameter. - /// The value. - /// Type of the field. - /// The field converted to string. - public static string ConvertToString(T value, ReportFieldType fieldType) - { - if (value == null) - return ""; - switch (fieldType) - { - case ReportFieldType.String: - return value.ToString(); - case ReportFieldType.Boolean: - return value.ToString(); - case ReportFieldType.Date: - return string.Format("{0:d}", value); - case ReportFieldType.Time: - return string.Format("{0:t}", value); - case ReportFieldType.DateTime: - return string.Format("{0:d}", value); - case ReportFieldType.Minutes: - return string.Format("{0}mn", value); - case ReportFieldType.Int: - return string.Format("", value); - default: - if (value is Guid) - return string.Format("{0:N}", value); - return value.ToString(); - } - } - - /// Gets filtered report header metadata. - /// The report columns. - /// The default return value. - /// The filtered report header metadata. - public static List GetFilteredReportHeaderMetadata(string reportColumns, Func> defaultReturnValue = null) - { - if (!string.IsNullOrEmpty(reportColumns)) - { - var s = reportColumns.Split('|').Select(x => ReportHelper.GetHeaderMetadataType(x)).Where(x => x != HeaderMetadata.None); - return s.ToList(); - } - else - if (defaultReturnValue != null) - return defaultReturnValue(); - else - return new List(); - } - - /// Gets header metadata type. - /// The header. - /// The header metadata type. - public static HeaderMetadata GetHeaderMetadataType(string header) - { - if (string.IsNullOrEmpty(header)) - return HeaderMetadata.None; - - HeaderMetadata rType; - - if (!Enum.TryParse(header, out rType)) - return HeaderMetadata.None; - - return rType; - } - - /// Gets report view type. - /// The type. - /// The report view type. - public static ReportViewType GetReportViewType(string rowType) - { - if (string.IsNullOrEmpty(rowType)) - return ReportViewType.ReportData; - - ReportViewType rType; - - if (!Enum.TryParse(rowType, out rType)) - return ReportViewType.ReportData; - - return rType; - } - - /// Gets row type. - /// The type. - /// The row type. - public static ReportIncludeItemTypes GetRowType(string rowType) - { - if (string.IsNullOrEmpty(rowType)) - return ReportIncludeItemTypes.BaseItem; - - ReportIncludeItemTypes rType; - - if (!Enum.TryParse(rowType, out rType)) - return ReportIncludeItemTypes.BaseItem; - - return rType; - } - - /// Gets report display type. - /// Type of the display. - /// The report display type. - public static ReportDisplayType GetReportDisplayType(string displayType) - { - if (string.IsNullOrEmpty(displayType)) - return ReportDisplayType.ScreenExport; - - ReportDisplayType rType; - - if (!Enum.TryParse(displayType, out rType)) - return ReportDisplayType.ScreenExport; - - return rType; - } - - /// Gets core localized string. - /// The phrase. - /// The core localized string. - public static string GetCoreLocalizedString(string phrase) - { - return BaseItem.LocalizationManager.GetLocalizedString(phrase); - } - - #endregion - - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportIncludeItemTypes.cs b/MediaBrowser.Api/Reports/Common/ReportIncludeItemTypes.cs deleted file mode 100644 index 65cc2b686c..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportIncludeItemTypes.cs +++ /dev/null @@ -1,21 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum ReportIncludeItemTypes - { - MusicArtist, - MusicAlbum, - Book, - BoxSet, - Episode, - Game, - Video, - Movie, - MusicVideo, - Trailer, - Season, - Series, - Audio, - BaseItem, - Artist - } -} diff --git a/MediaBrowser.Api/Reports/Common/ReportViewType.cs b/MediaBrowser.Api/Reports/Common/ReportViewType.cs deleted file mode 100644 index 8593c8e843..0000000000 --- a/MediaBrowser.Api/Reports/Common/ReportViewType.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MediaBrowser.Api.Reports -{ - public enum ReportViewType - { - ReportData, - ReportActivities - - } -} diff --git a/MediaBrowser.Api/Reports/Data/ReportBuilder.cs b/MediaBrowser.Api/Reports/Data/ReportBuilder.cs deleted file mode 100644 index 6a1502c7e6..0000000000 --- a/MediaBrowser.Api/Reports/Data/ReportBuilder.cs +++ /dev/null @@ -1,626 +0,0 @@ -using MediaBrowser.Controller.Entities; -using MediaBrowser.Controller.Entities.Audio; -using MediaBrowser.Controller.Entities.TV; -using MediaBrowser.Controller.Library; -using MediaBrowser.Model.Entities; -using System.Collections.Generic; -using System.Linq; - -namespace MediaBrowser.Api.Reports -{ - /// A report builder. - /// - public class ReportBuilder : ReportBuilderBase - { - - #region [Constructors] - - /// - /// Initializes a new instance of the MediaBrowser.Api.Reports.ReportBuilder class. - /// Manager for library. - public ReportBuilder(ILibraryManager libraryManager) - : base(libraryManager) - { - } - - #endregion - - #region [Public Methods] - - /// Gets report result. - /// The items. - /// The request. - /// The report result. - public ReportResult GetResult(BaseItem[] items, IReportsQuery request) - { - ReportIncludeItemTypes reportRowType = ReportHelper.GetRowType(request.IncludeItemTypes); - ReportDisplayType displayType = ReportHelper.GetReportDisplayType(request.DisplayType); - - List> options = this.GetReportOptions(request, - () => this.GetDefaultHeaderMetadata(reportRowType), - (hm) => this.GetOption(hm)).Where(x => this.DisplayTypeVisible(x.Header.DisplayType, displayType)).ToList(); - - var headers = GetHeaders(options); - var rows = GetReportRows(items, options); - - ReportResult result = new ReportResult { Headers = headers }; - HeaderMetadata groupBy = ReportHelper.GetHeaderMetadataType(request.GroupBy); - int i = headers.FindIndex(x => x.FieldName == groupBy); - if (groupBy != HeaderMetadata.None && i >= 0) - { - var rowsGroup = rows.SelectMany(x => x.Columns[i].Name.Split(';'), (x, g) => new { Group = g.Trim(), Rows = x }) - .GroupBy(x => x.Group) - .OrderBy(x => x.Key) - .Select(x => new ReportGroup { Name = x.Key, Rows = x.Select(r => r.Rows).ToList() }); - - result.Groups = rowsGroup.ToList(); - result.IsGrouped = true; - } - else - { - result.Rows = rows; - result.IsGrouped = false; - } - - return result; - } - - #endregion - - #region [Protected Internal Methods] - - /// Gets the headers. - /// Type of the header. - /// The request. - /// The headers. - /// - protected internal override List GetHeaders(H request) - { - ReportIncludeItemTypes reportRowType = ReportHelper.GetRowType(request.IncludeItemTypes); - return this.GetHeaders(request, () => this.GetDefaultHeaderMetadata(reportRowType), (hm) => this.GetOption(hm)); - } - - #endregion - - #region [Private Methods] - - /// Gets default report header metadata. - /// Type of the report row. - /// The default report header metadata. - private List GetDefaultHeaderMetadata(ReportIncludeItemTypes reportIncludeItemTypes) - { - switch (reportIncludeItemTypes) - { - case ReportIncludeItemTypes.Season: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Series, - HeaderMetadata.Season, - HeaderMetadata.SeasonNumber, - HeaderMetadata.DateAdded, - HeaderMetadata.Year, - HeaderMetadata.Genres - }; - - case ReportIncludeItemTypes.Series: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.Network, - HeaderMetadata.DateAdded, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating, - HeaderMetadata.Runtime, - HeaderMetadata.Trailers, - HeaderMetadata.Specials - }; - - case ReportIncludeItemTypes.MusicAlbum: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.AlbumArtist, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.Tracks, - HeaderMetadata.Year, - HeaderMetadata.Genres - }; - - case ReportIncludeItemTypes.MusicArtist: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.MusicArtist, - HeaderMetadata.Countries, - HeaderMetadata.DateAdded, - HeaderMetadata.Year, - HeaderMetadata.Genres - }; - - case ReportIncludeItemTypes.Game: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.GameSystem, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating, - HeaderMetadata.Players, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.Trailers - }; - - case ReportIncludeItemTypes.Movie: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating, - HeaderMetadata.Runtime, - HeaderMetadata.Video, - HeaderMetadata.Resolution, - HeaderMetadata.Audio, - HeaderMetadata.Subtitles, - HeaderMetadata.Trailers, - HeaderMetadata.Specials - }; - - case ReportIncludeItemTypes.Book: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating - }; - - case ReportIncludeItemTypes.BoxSet: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating, - HeaderMetadata.Trailers - }; - - case ReportIncludeItemTypes.Audio: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.AudioAlbumArtist, - HeaderMetadata.AudioAlbum, - HeaderMetadata.Disc, - HeaderMetadata.Track, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating, - HeaderMetadata.Runtime, - HeaderMetadata.Audio - }; - - case ReportIncludeItemTypes.Episode: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.EpisodeSeries, - HeaderMetadata.Season, - HeaderMetadata.EpisodeNumber, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating, - HeaderMetadata.Runtime, - HeaderMetadata.Video, - HeaderMetadata.Resolution, - HeaderMetadata.Audio, - HeaderMetadata.Subtitles, - HeaderMetadata.Trailers, - HeaderMetadata.Specials - }; - - case ReportIncludeItemTypes.Video: - case ReportIncludeItemTypes.MusicVideo: - case ReportIncludeItemTypes.Trailer: - case ReportIncludeItemTypes.BaseItem: - default: - return new List - { - HeaderMetadata.Status, - HeaderMetadata.Locked, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.ImagePrimary, - HeaderMetadata.ImageBackdrop, - HeaderMetadata.ImageLogo, - HeaderMetadata.Name, - HeaderMetadata.DateAdded, - HeaderMetadata.ReleaseDate, - HeaderMetadata.Year, - HeaderMetadata.Genres, - HeaderMetadata.ParentalRating, - HeaderMetadata.CommunityRating, - HeaderMetadata.Runtime, - HeaderMetadata.Video, - HeaderMetadata.Resolution, - HeaderMetadata.Audio, - HeaderMetadata.Subtitles, - HeaderMetadata.Trailers, - HeaderMetadata.Specials - }; - - } - - } - - /// Gets report option. - /// The header. - /// The sort field. - /// The report option. - private ReportOptions GetOption(HeaderMetadata header, string sortField = "") - { - HeaderMetadata internalHeader = header; - - ReportOptions option = new ReportOptions() - { - Header = new ReportHeader - { - HeaderFieldType = ReportFieldType.String, - SortField = sortField, - Type = "", - ItemViewType = ItemViewType.None - } - }; - - switch (header) - { - case HeaderMetadata.Status: - option.Header.ItemViewType = ItemViewType.StatusImage; - internalHeader = HeaderMetadata.Status; - option.Header.CanGroup = false; - option.Header.DisplayType = ReportDisplayType.Screen; - break; - case HeaderMetadata.Locked: - option.Column = (i, r) => this.GetBoolString(r.HasLockData); - option.Header.ItemViewType = ItemViewType.LockDataImage; - option.Header.CanGroup = false; - option.Header.DisplayType = ReportDisplayType.Export; - break; - case HeaderMetadata.ImagePrimary: - option.Column = (i, r) => this.GetBoolString(r.HasImageTagsPrimary); - option.Header.ItemViewType = ItemViewType.TagsPrimaryImage; - option.Header.CanGroup = false; - option.Header.DisplayType = ReportDisplayType.Export; - break; - case HeaderMetadata.ImageBackdrop: - option.Column = (i, r) => this.GetBoolString(r.HasImageTagsBackdrop); - option.Header.ItemViewType = ItemViewType.TagsBackdropImage; - option.Header.CanGroup = false; - option.Header.DisplayType = ReportDisplayType.Export; - break; - case HeaderMetadata.ImageLogo: - option.Column = (i, r) => this.GetBoolString(r.HasImageTagsLogo); - option.Header.ItemViewType = ItemViewType.TagsLogoImage; - option.Header.CanGroup = false; - option.Header.DisplayType = ReportDisplayType.Export; - break; - - case HeaderMetadata.Name: - option.Column = (i, r) => i.Name; - option.Header.ItemViewType = ItemViewType.Detail; - option.Header.SortField = "SortName"; - break; - - case HeaderMetadata.DateAdded: - option.Column = (i, r) => i.DateCreated; - option.Header.SortField = "DateCreated,SortName"; - option.Header.HeaderFieldType = ReportFieldType.DateTime; - option.Header.Type = ""; - break; - - case HeaderMetadata.PremiereDate: - case HeaderMetadata.ReleaseDate: - option.Column = (i, r) => i.PremiereDate; - option.Header.HeaderFieldType = ReportFieldType.DateTime; - option.Header.SortField = "ProductionYear,PremiereDate,SortName"; - break; - - case HeaderMetadata.Runtime: - option.Column = (i, r) => this.GetRuntimeDateTime(i.RunTimeTicks); - option.Header.HeaderFieldType = ReportFieldType.Minutes; - option.Header.SortField = "Runtime,SortName"; - break; - - case HeaderMetadata.PlayCount: - option.Header.HeaderFieldType = ReportFieldType.Int; - break; - - case HeaderMetadata.Season: - option.Column = (i, r) => this.GetEpisode(i); - option.Header.ItemViewType = ItemViewType.Detail; - option.Header.SortField = "SortName"; - break; - - case HeaderMetadata.SeasonNumber: - option.Column = (i, r) => this.GetObject(i, (x) => x.IndexNumber == null ? "" : x.IndexNumber.ToString()); - option.Header.SortField = "IndexNumber"; - option.Header.HeaderFieldType = ReportFieldType.Int; - break; - - case HeaderMetadata.Series: - option.Column = (i, r) => this.GetObject(i, (x) => x.SeriesName); - option.Header.ItemViewType = ItemViewType.Detail; - option.Header.SortField = "SeriesSortName,SortName"; - break; - - case HeaderMetadata.EpisodeSeries: - option.Column = (i, r) => this.GetObject(i, (x) => x.SeriesName); - option.Header.ItemViewType = ItemViewType.Detail; - option.ItemID = (i) => - { - Series series = this.GetObject(i, (x) => x.Series); - if (series == null) - return string.Empty; - return series.Id; - }; - option.Header.SortField = "SeriesSortName,SortName"; - internalHeader = HeaderMetadata.Series; - break; - - case HeaderMetadata.EpisodeSeason: - option.Column = (i, r) => this.GetObject(i, (x) => x.SeriesName); - option.Header.ItemViewType = ItemViewType.Detail; - option.ItemID = (i) => - { - Season season = this.GetObject(i, (x) => x.Season); - if (season == null) - return string.Empty; - return season.Id; - }; - option.Header.SortField = "SortName"; - internalHeader = HeaderMetadata.Season; - break; - - case HeaderMetadata.EpisodeNumber: - option.Column = (i, r) => this.GetObject(i, (x) => x.IndexNumber == null ? "" : x.IndexNumber.ToString()); - //option.Header.SortField = "IndexNumber"; - //option.Header.HeaderFieldType = ReportFieldType.Int; - break; - - case HeaderMetadata.Network: - option.Column = (i, r) => this.GetListAsString(i.Studios.ToList()); - option.ItemID = (i) => this.GetStudioID(i.Studios.FirstOrDefault()); - option.Header.ItemViewType = ItemViewType.ItemByNameDetails; - option.Header.SortField = "Studio,SortName"; - break; - - case HeaderMetadata.Year: - option.Column = (i, r) => this.GetSeriesProductionYear(i); - option.Header.SortField = "ProductionYear,PremiereDate,SortName"; - break; - - case HeaderMetadata.ParentalRating: - option.Column = (i, r) => i.OfficialRating; - option.Header.SortField = "OfficialRating,SortName"; - break; - - case HeaderMetadata.CommunityRating: - option.Column = (i, r) => i.CommunityRating; - option.Header.SortField = "CommunityRating,SortName"; - break; - - case HeaderMetadata.Trailers: - option.Column = (i, r) => this.GetBoolString(r.HasLocalTrailer); - option.Header.ItemViewType = ItemViewType.TrailersImage; - break; - - case HeaderMetadata.Specials: - option.Column = (i, r) => this.GetBoolString(r.HasSpecials); - option.Header.ItemViewType = ItemViewType.SpecialsImage; - break; - - case HeaderMetadata.GameSystem: - option.Column = (i, r) => this.GetObject(i, (x) => x.GameSystem); - option.Header.SortField = "GameSystem,SortName"; - break; - - case HeaderMetadata.Players: - option.Column = (i, r) => this.GetObject(i, (x) => x.PlayersSupported); - option.Header.SortField = "Players,GameSystem,SortName"; - break; - - case HeaderMetadata.AlbumArtist: - option.Column = (i, r) => this.GetObject(i, (x) => x.AlbumArtist); - option.ItemID = (i) => this.GetPersonID(this.GetObject(i, (x) => x.AlbumArtist)); - option.Header.ItemViewType = ItemViewType.Detail; - option.Header.SortField = "AlbumArtist,Album,SortName"; - - break; - case HeaderMetadata.MusicArtist: - option.Column = (i, r) => this.GetObject(i, (x) => x.GetLookupInfo().Name); - option.Header.ItemViewType = ItemViewType.Detail; - option.Header.SortField = "AlbumArtist,Album,SortName"; - internalHeader = HeaderMetadata.AlbumArtist; - break; - case HeaderMetadata.AudioAlbumArtist: - option.Column = (i, r) => this.GetListAsString(this.GetObject>(i, (x) => x.AlbumArtists.ToList())); - option.Header.SortField = "AlbumArtist,Album,SortName"; - internalHeader = HeaderMetadata.AlbumArtist; - break; - - case HeaderMetadata.AudioAlbum: - option.Column = (i, r) => this.GetObject(i, (x) => x.Album); - option.Header.SortField = "Album,SortName"; - internalHeader = HeaderMetadata.Album; - break; - - case HeaderMetadata.Disc: - option.Column = (i, r) => i.ParentIndexNumber; - break; - - case HeaderMetadata.Track: - option.Column = (i, r) => i.IndexNumber; - break; - - case HeaderMetadata.Tracks: - option.Column = (i, r) => this.GetObject>(i, (x) => x.Tracks.Cast