From 74520804f80974ec018a6c45f4e221276ded4cd3 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Sun, 21 Dec 2014 13:58:17 -0500 Subject: [PATCH] start on content type setting --- MediaBrowser.Api/ItemUpdateService.cs | 104 ++++++++++++++++++ .../Configuration/BaseConfigurationManager.cs | 20 +++- .../Serialization/XmlSerializer.cs | 17 ++- MediaBrowser.Controller/Entities/Folder.cs | 2 +- .../Library/ILibraryManager.cs | 9 +- .../MediaBrowser.Model.Portable.csproj | 3 + .../MediaBrowser.Model.net35.csproj | 3 + MediaBrowser.Model/Dto/MetadataEditorInfo.cs | 4 + MediaBrowser.Model/Dto/NameValuePair.cs | 17 +++ MediaBrowser.Model/MediaBrowser.Model.csproj | 1 + .../Library/LibraryManager.cs | 17 ++- .../Localization/Server/server.json | 12 ++ 12 files changed, 197 insertions(+), 12 deletions(-) create mode 100644 MediaBrowser.Model/Dto/NameValuePair.cs diff --git a/MediaBrowser.Api/ItemUpdateService.cs b/MediaBrowser.Api/ItemUpdateService.cs index 601feabd5..953f7b3bb 100644 --- a/MediaBrowser.Api/ItemUpdateService.cs +++ b/MediaBrowser.Api/ItemUpdateService.cs @@ -6,6 +6,7 @@ using MediaBrowser.Controller.Localization; using MediaBrowser.Controller.Net; using MediaBrowser.Controller.Providers; using MediaBrowser.Model.Dto; +using MediaBrowser.Model.Entities; using ServiceStack; using System; using System.Collections.Generic; @@ -28,6 +29,16 @@ namespace MediaBrowser.Api [ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")] public string ItemId { get; set; } } + + [Route("/Items/{ItemId}/ContentType", "POST", Summary = "Updates an item's content type")] + public class UpdateItemContentType : IReturnVoid + { + [ApiMember(Name = "ItemId", Description = "The id of the item", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")] + public string ItemId { get; set; } + + [ApiMember(Name = "ContentType", Description = "The content type of the item", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")] + public string ContentType { get; set; } + } [Authenticated] public class ItemUpdateService : BaseApiService @@ -55,9 +66,102 @@ namespace MediaBrowser.Api Cultures = _localizationManager.GetCultures().ToList() }; + var locationType = item.LocationType; + if (locationType == LocationType.FileSystem || + locationType == LocationType.Offline) + { + var collectionType = _libraryManager.GetInheritedContentType(item); + if (string.IsNullOrWhiteSpace(collectionType)) + { + info.ContentTypeOptions = GetContentTypeOptions(true); + info.ContentType = _libraryManager.GetContentType(item); + } + } + return ToOptimizedResult(info); } + public void Post(UpdateItemContentType request) + { + + } + + private List GetContentTypeOptions(bool isForItem) + { + var list = new List(); + + if (isForItem) + { + list.Add(new NameValuePair + { + Name = "FolderTypeInherit", + Value = "" + }); + } + + list.Add(new NameValuePair + { + Name = "FolderTypeMovies", + Value = "movies" + }); + list.Add(new NameValuePair + { + Name = "FolderTypeMusic", + Value = "music" + }); + list.Add(new NameValuePair + { + Name = "FolderTypeTvShows", + Value = "tvshows" + }); + + if (!isForItem) + { + list.Add(new NameValuePair + { + Name = "FolderTypeBooks", + Value = "books" + }); + list.Add(new NameValuePair + { + Name = "FolderTypeGames", + Value = "games" + }); + } + + list.Add(new NameValuePair + { + Name = "FolderTypeHomeVideos", + Value = "homevideos" + }); + list.Add(new NameValuePair + { + Name = "FolderTypeMusicVideos", + Value = "musicvideos" + }); + list.Add(new NameValuePair + { + Name = "FolderTypePhotos", + Value = "photos" + }); + + if (!isForItem) + { + list.Add(new NameValuePair + { + Name = "FolderTypeMixed", + Value = "" + }); + } + + foreach (var val in list) + { + val.Name = _localizationManager.GetLocalizedString(val.Name); + } + + return list; + } + public void Post(UpdateItem request) { var task = UpdateItem(request); diff --git a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs index 4a70697fa..a061420d7 100644 --- a/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs +++ b/MediaBrowser.Common.Implementations/Configuration/BaseConfigurationManager.cs @@ -208,11 +208,29 @@ namespace MediaBrowser.Common.Implementations.Configuration lock (_configurationSyncLock) { - return ConfigurationHelper.GetXmlConfiguration(configurationType, file, XmlSerializer); + return LoadConfiguration(file, configurationType); } }); } + private object LoadConfiguration(string path, Type configurationType) + { + try + { + return XmlSerializer.DeserializeFromFile(configurationType, path); + } + catch (FileNotFoundException) + { + return Activator.CreateInstance(configurationType); + } + catch (Exception ex) + { + Logger.ErrorException("Error loading configuration file: {0}", ex, path); + + return Activator.CreateInstance(configurationType); + } + } + public void SaveConfiguration(string key, object configuration) { var configurationType = GetConfigurationType(key); diff --git a/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs b/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs index cef744753..04030522f 100644 --- a/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs +++ b/MediaBrowser.Common.Implementations/Serialization/XmlSerializer.cs @@ -1,5 +1,6 @@ using MediaBrowser.Model.Serialization; using System; +using System.Collections.Concurrent; using System.IO; using System.Xml; @@ -10,6 +11,17 @@ namespace MediaBrowser.Common.Implementations.Serialization /// public class XmlSerializer : IXmlSerializer { + // Need to cache these + // http://dotnetcodebox.blogspot.com/2013/01/xmlserializer-class-may-result-in.html + private readonly ConcurrentDictionary _serializers = + new ConcurrentDictionary(); + + private System.Xml.Serialization.XmlSerializer GetSerializer(Type type) + { + var key = type.FullName; + return _serializers.GetOrAdd(key, k => new System.Xml.Serialization.XmlSerializer(type)); + } + /// /// Serializes to writer. /// @@ -18,7 +30,7 @@ namespace MediaBrowser.Common.Implementations.Serialization private void SerializeToWriter(object obj, XmlTextWriter writer) { writer.Formatting = Formatting.Indented; - var netSerializer = new System.Xml.Serialization.XmlSerializer(obj.GetType()); + var netSerializer = GetSerializer(obj.GetType()); netSerializer.Serialize(writer, obj); } @@ -32,8 +44,7 @@ namespace MediaBrowser.Common.Implementations.Serialization { using (var reader = new XmlTextReader(stream)) { - var netSerializer = new System.Xml.Serialization.XmlSerializer(type); - + var netSerializer = GetSerializer(type); return netSerializer.Deserialize(reader); } } diff --git a/MediaBrowser.Controller/Entities/Folder.cs b/MediaBrowser.Controller/Entities/Folder.cs index e8582a52a..2761aa5d7 100644 --- a/MediaBrowser.Controller/Entities/Folder.cs +++ b/MediaBrowser.Controller/Entities/Folder.cs @@ -708,7 +708,7 @@ namespace MediaBrowser.Controller.Entities /// IEnumerable{BaseItem}. protected virtual IEnumerable GetNonCachedChildren(IDirectoryService directoryService) { - var collectionType = LibraryManager.FindCollectionType(this); + var collectionType = LibraryManager.GetContentType(this); return LibraryManager.ResolvePaths(GetFileSystemChildren(directoryService), directoryService, this, collectionType); } diff --git a/MediaBrowser.Controller/Library/ILibraryManager.cs b/MediaBrowser.Controller/Library/ILibraryManager.cs index 6d8f89226..7cdfb7788 100644 --- a/MediaBrowser.Controller/Library/ILibraryManager.cs +++ b/MediaBrowser.Controller/Library/ILibraryManager.cs @@ -259,8 +259,15 @@ namespace MediaBrowser.Controller.Library /// /// The item. /// System.String. - string FindCollectionType(BaseItem item); + string GetContentType(BaseItem item); + /// + /// Gets the type of the inherited content. + /// + /// The item. + /// System.String. + string GetInheritedContentType(BaseItem item); + /// /// Normalizes the root path list. /// diff --git a/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj b/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj index ec41ffe0b..f18c53cd3 100644 --- a/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj +++ b/MediaBrowser.Model.Portable/MediaBrowser.Model.Portable.csproj @@ -464,6 +464,9 @@ Dto\MetadataEditorInfo.cs + + Dto\NameValuePair.cs + Dto\RatingType.cs diff --git a/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj b/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj index 01aaad2ac..f17215988 100644 --- a/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj +++ b/MediaBrowser.Model.net35/MediaBrowser.Model.net35.csproj @@ -429,6 +429,9 @@ Dto\MetadataEditorInfo.cs + + Dto\NameValuePair.cs + Dto\RatingType.cs diff --git a/MediaBrowser.Model/Dto/MetadataEditorInfo.cs b/MediaBrowser.Model/Dto/MetadataEditorInfo.cs index 66bdb8ce3..9bd15fc8f 100644 --- a/MediaBrowser.Model/Dto/MetadataEditorInfo.cs +++ b/MediaBrowser.Model/Dto/MetadataEditorInfo.cs @@ -12,12 +12,16 @@ namespace MediaBrowser.Model.Dto public List Cultures { get; set; } public List ExternalIdInfos { get; set; } + public string ContentType { get; set; } + public List ContentTypeOptions { get; set; } + public MetadataEditorInfo() { ParentalRatingOptions = new List(); Countries = new List(); Cultures = new List(); ExternalIdInfos = new List(); + ContentTypeOptions = new List(); } } } diff --git a/MediaBrowser.Model/Dto/NameValuePair.cs b/MediaBrowser.Model/Dto/NameValuePair.cs new file mode 100644 index 000000000..2d55e8f2a --- /dev/null +++ b/MediaBrowser.Model/Dto/NameValuePair.cs @@ -0,0 +1,17 @@ + +namespace MediaBrowser.Model.Dto +{ + public class NameValuePair + { + /// + /// Gets or sets the name. + /// + /// The name. + public string Name { get; set; } + /// + /// Gets or sets the value. + /// + /// The value. + public string Value { get; set; } + } +} diff --git a/MediaBrowser.Model/MediaBrowser.Model.csproj b/MediaBrowser.Model/MediaBrowser.Model.csproj index 0dd5442ed..759c671e9 100644 --- a/MediaBrowser.Model/MediaBrowser.Model.csproj +++ b/MediaBrowser.Model/MediaBrowser.Model.csproj @@ -129,6 +129,7 @@ + diff --git a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs index 9647461bf..ab50c30fb 100644 --- a/MediaBrowser.Server.Implementations/Library/LibraryManager.cs +++ b/MediaBrowser.Server.Implementations/Library/LibraryManager.cs @@ -1546,12 +1546,17 @@ namespace MediaBrowser.Server.Implementations.Library return ItemRepository.RetrieveItem(id); } - /// - /// Finds the type of the collection. - /// - /// The item. - /// System.String. - public string FindCollectionType(BaseItem item) + public string GetContentType(BaseItem item) + { + return GetInheritedContentType(item); + } + + public string GetInheritedContentType(BaseItem item) + { + return GetTopFolderContentType(item); + } + + private string GetTopFolderContentType(BaseItem item) { while (!(item.Parent is AggregateFolder) && item.Parent != null) { diff --git a/MediaBrowser.Server.Implementations/Localization/Server/server.json b/MediaBrowser.Server.Implementations/Localization/Server/server.json index 9bf06c50b..9a515d492 100644 --- a/MediaBrowser.Server.Implementations/Localization/Server/server.json +++ b/MediaBrowser.Server.Implementations/Localization/Server/server.json @@ -37,6 +37,18 @@ "ButtonOk": "Ok", "ButtonCancel": "Cancel", "ButtonNew": "New", + "FolderTypeMixed": "Mixed videos", + "FolderTypeMovies": "Movies", + "FolderTypeMusic": "Music", + "FolderTypeAdultVideos": "Adult videos", + "FolderTypePhotos": "Photos", + "FolderTypeMusicVideos": "Music videos", + "FolderTypeHomeVideos": "Home videos", + "FolderTypeGames": "Games", + "FolderTypeBooks": "Books", + "FolderTypeTvShows": "TV", + "FolderTypeInherit": "Inherit", + "LabelContentType": "Content type:", "HeaderSetupLibrary": "Setup your media library", "ButtonAddMediaFolder": "Add media folder", "LabelFolderType": "Folder type:",