consolidate ibn api a bit

This commit is contained in:
Luke Pulverenti 2014-02-22 15:20:22 -05:00
parent 014c06ea43
commit b52d99568b
8 changed files with 56 additions and 322 deletions

View File

@ -123,7 +123,6 @@
<Compile Include="UserLibrary\BaseItemsRequest.cs" />
<Compile Include="UserLibrary\GameGenresService.cs" />
<Compile Include="UserLibrary\GenresService.cs" />
<Compile Include="UserLibrary\ItemByNameUserDataService.cs" />
<Compile Include="UserLibrary\ItemsService.cs" />
<Compile Include="UserLibrary\MusicGenresService.cs" />
<Compile Include="UserLibrary\PersonsService.cs" />

View File

@ -1,264 +0,0 @@
using MediaBrowser.Controller.Dto;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Dto;
using MediaBrowser.Model.Entities;
using ServiceStack;
using System;
using System.Threading;
using System.Threading.Tasks;
using ServiceStack.Text.Controller;
namespace MediaBrowser.Api.UserLibrary
{
/// <summary>
/// Class MarkItemByNameFavorite
/// </summary>
[Route("/Users/{UserId}/Favorites/Artists/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/Persons/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/Studios/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/Genres/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/MusicGenres/{Name}", "POST")]
[Route("/Users/{UserId}/Favorites/GameGenres/{Name}", "POST")]
[Api(Description = "Marks something as a favorite")]
public class MarkItemByNameFavorite : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Name { get; set; }
}
/// <summary>
/// Class UnmarkItemByNameFavorite
/// </summary>
[Route("/Users/{UserId}/Favorites/Artists/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/Persons/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/Studios/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/Genres/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/MusicGenres/{Name}", "DELETE")]
[Route("/Users/{UserId}/Favorites/GameGenres/{Name}", "DELETE")]
[Api(Description = "Unmarks something as a favorite")]
public class UnmarkItemByNameFavorite : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
/// <summary>
/// Class UpdateItemByNameRating
/// </summary>
[Route("/Users/{UserId}/Ratings/Artists/{Name}", "POST")]
[Route("/Users/{UserId}/Ratings/Persons/{Name}", "POST")]
[Route("/Users/{UserId}/Ratings/Studios/{Name}", "POST")]
[Route("/Users/{UserId}/Ratings/Genres/{Name}", "POST")]
[Route("/Users/{UserId}/Ratings/MusicGenres/{Name}", "POST")]
[Api(Description = "Updates a user's rating for an item")]
public class UpdateItemByNameRating : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The name", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
public string Name { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this <see cref="UpdateUserItemRating" /> is likes.
/// </summary>
/// <value><c>true</c> if likes; otherwise, <c>false</c>.</value>
[ApiMember(Name = "Likes", Description = "Whether the user likes the item or not. true/false", IsRequired = true, DataType = "boolean", ParameterType = "query", Verb = "POST")]
public bool Likes { get; set; }
}
/// <summary>
/// Class DeleteItemByNameRating
/// </summary>
[Route("/Users/{UserId}/Ratings/Artists/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/Persons/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/Studios/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/Genres/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/MusicGenres/{Name}", "DELETE")]
[Route("/Users/{UserId}/Ratings/GameGenres/{Name}", "DELETE")]
[Api(Description = "Deletes a user's saved personal rating for an item")]
public class DeleteItemByNameRating : IReturn<UserItemDataDto>
{
/// <summary>
/// Gets or sets the user id.
/// </summary>
/// <value>The user id.</value>
[ApiMember(Name = "UserId", Description = "User Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public Guid UserId { get; set; }
/// <summary>
/// Gets or sets the name.
/// </summary>
/// <value>The name.</value>
[ApiMember(Name = "Name", Description = "The item name (genre, person, year, studio, artist)", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
public string Name { get; set; }
}
/// <summary>
/// Class ItemByNameUserDataService
/// </summary>
public class ItemByNameUserDataService : BaseApiService
{
/// <summary>
/// The user data repository
/// </summary>
protected readonly IUserDataManager UserDataRepository;
/// <summary>
/// The library manager
/// </summary>
protected readonly ILibraryManager LibraryManager;
private readonly IDtoService _dtoService;
/// <summary>
/// Initializes a new instance of the <see cref="ItemByNameUserDataService" /> class.
/// </summary>
/// <param name="userDataRepository">The user data repository.</param>
/// <param name="libraryManager">The library manager.</param>
public ItemByNameUserDataService(IUserDataManager userDataRepository, ILibraryManager libraryManager, IDtoService dtoService)
{
UserDataRepository = userDataRepository;
LibraryManager = libraryManager;
_dtoService = dtoService;
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Post(MarkItemByNameFavorite request)
{
var pathInfo = PathInfo.Parse(Request.PathInfo);
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkFavorite(request.UserId, type, request.Name, true);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Posts the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Post(UpdateItemByNameRating request)
{
var pathInfo = PathInfo.Parse(Request.PathInfo);
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkLike(request.UserId, type, request.Name, request.Likes);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Delete(UnmarkItemByNameFavorite request)
{
var pathInfo = PathInfo.Parse(Request.PathInfo);
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkFavorite(request.UserId, type, request.Name, false);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Deletes the specified request.
/// </summary>
/// <param name="request">The request.</param>
public object Delete(DeleteItemByNameRating request)
{
var pathInfo = PathInfo.Parse(Request.PathInfo);
var type = pathInfo.GetArgumentValue<string>(3);
var task = MarkLike(request.UserId, type, request.Name, null);
return ToOptimizedResult(task.Result);
}
/// <summary>
/// Marks the favorite.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="isFavorite">if set to <c>true</c> [is favorite].</param>
/// <returns>Task.</returns>
protected async Task<UserItemDataDto> MarkFavorite(Guid userId, string type, string name, bool isFavorite)
{
var item = GetItemByName(name, type, LibraryManager);
var key = item.GetUserDataKey();
// Get the user data for this item
var data = UserDataRepository.GetUserData(userId, key);
// Set favorite status
data.IsFavorite = isFavorite;
await UserDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = UserDataRepository.GetUserData(userId, key);
return _dtoService.GetUserItemDataDto(data);
}
/// <summary>
/// Marks the like.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="type">The type.</param>
/// <param name="name">The name.</param>
/// <param name="likes">if set to <c>true</c> [likes].</param>
/// <returns>Task.</returns>
protected async Task<UserItemDataDto> MarkLike(Guid userId, string type, string name, bool? likes)
{
var item = GetItemByName(name, type, LibraryManager);
var key = item.GetUserDataKey();
// Get the user data for this item
var data = UserDataRepository.GetUserData(userId, key);
data.Likes = likes;
await UserDataRepository.SaveUserData(userId, item, data, UserDataSaveReason.UpdateUserRating, CancellationToken.None).ConfigureAwait(false);
data = UserDataRepository.GetUserData(userId, key);
return _dtoService.GetUserItemDataDto(data);
}
}
}

View File

@ -424,7 +424,7 @@ namespace MediaBrowser.Controller.Entities
{
BaseItem currentChild;
if (currentChildren.TryGetValue(child.Id, out currentChild))
if (currentChildren.TryGetValue(child.Id, out currentChild) && child.IsInMixedFolder == currentChild.IsInMixedFolder)
{
var currentChildLocationType = currentChild.LocationType;
if (currentChildLocationType != LocationType.Remote &&
@ -433,7 +433,6 @@ namespace MediaBrowser.Controller.Entities
currentChild.DateModified = child.DateModified;
}
currentChild.IsInMixedFolder = child.IsInMixedFolder;
currentChild.IsOffline = false;
}
else

View File

@ -818,15 +818,6 @@ namespace MediaBrowser.Model.ApiClient
/// <exception cref="ArgumentNullException">name</exception>
string GetPersonImageUrl(string name, ImageOptions options);
/// <summary>
/// Gets the year image URL.
/// </summary>
/// <param name="item">The item.</param>
/// <param name="options">The options.</param>
/// <returns>System.String.</returns>
/// <exception cref="ArgumentNullException">item</exception>
string GetYearImageUrl(BaseItemDto item, ImageOptions options);
/// <summary>
/// Gets an image url that can be used to download an image from the api
/// </summary>

View File

@ -119,13 +119,13 @@ namespace MediaBrowser.Providers.Manager
var stream = _fileSystem.GetFileStream(response.Path, FileMode.Open, FileAccess.Read, FileShare.Read, true);
await _providerManager.SaveImage((BaseItem)item, stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
await _providerManager.SaveImage(item, stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
}
else
{
var mimeType = "image/" + response.Format.ToString().ToLower();
await _providerManager.SaveImage((BaseItem)item, response.Stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
await _providerManager.SaveImage(item, response.Stream, mimeType, imageType, null, cancellationToken).ConfigureAwait(false);
}
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
@ -325,7 +325,7 @@ namespace MediaBrowser.Providers.Manager
{
var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
await _providerManager.SaveImage((BaseItem)item, response.Content, response.ContentType, type, null, cancellationToken).ConfigureAwait(false);
await _providerManager.SaveImage(item, response.Content, response.ContentType, type, null, cancellationToken).ConfigureAwait(false);
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
break;
@ -362,7 +362,7 @@ namespace MediaBrowser.Providers.Manager
{
var response = await provider.GetImageResponse(url, cancellationToken).ConfigureAwait(false);
await _providerManager.SaveImage((BaseItem)item, response.Content, response.ContentType, imageType, null, cancellationToken).ConfigureAwait(false);
await _providerManager.SaveImage(item, response.Content, response.ContentType, imageType, null, cancellationToken).ConfigureAwait(false);
result.UpdateType = result.UpdateType | ItemUpdateType.ImageUpdate;
break;
}

View File

@ -183,7 +183,10 @@ namespace MediaBrowser.Providers.MediaInfo
audio.ProductionYear = FFProbeHelpers.GetDictionaryNumericValue(tags, "date");
// Several different forms of retaildate
audio.PremiereDate = FFProbeHelpers.GetDictionaryDateTime(tags, "retaildate") ?? FFProbeHelpers.GetDictionaryDateTime(tags, "retail date") ?? FFProbeHelpers.GetDictionaryDateTime(tags, "retail_date");
audio.PremiereDate = FFProbeHelpers.GetDictionaryDateTime(tags, "retaildate") ??
FFProbeHelpers.GetDictionaryDateTime(tags, "retail date") ??
FFProbeHelpers.GetDictionaryDateTime(tags, "retail_date") ??
FFProbeHelpers.GetDictionaryDateTime(tags, "date");
// If we don't have a ProductionYear try and get it from PremiereDate
if (audio.PremiereDate.HasValue && !audio.ProductionYear.HasValue)

View File

@ -516,8 +516,6 @@ namespace MediaBrowser.Providers.MediaInfo
var path = mount == null ? item.Path : mount.MountedPath;
var dvd = new Dvd(path);
item.RunTimeTicks = dvd.Titles.Select(GetRuntime).Max();
var primaryTitle = dvd.Titles.OrderByDescending(GetRuntime).FirstOrDefault();
uint? titleNumber = null;
@ -525,6 +523,7 @@ namespace MediaBrowser.Providers.MediaInfo
if (primaryTitle != null)
{
titleNumber = primaryTitle.TitleNumber;
item.RunTimeTicks = GetRuntime(primaryTitle);
}
item.PlayableStreamFileNames = GetPrimaryPlaylistVobFiles(item, mount, titleNumber)

View File

@ -279,7 +279,7 @@ namespace MediaBrowser.Server.Implementations.Library
if (ibnPathChanged)
{
_itemsByName.Clear();
RemoveItemsByNameFromCache();
}
var newSeasonZeroName = ConfigurationManager.Configuration.SeasonZeroDisplayName;
@ -302,6 +302,32 @@ namespace MediaBrowser.Server.Implementations.Library
});
}
private void RemoveItemsByNameFromCache()
{
RemoveItemsFromCache(i => i is Person);
RemoveItemsFromCache(i => i is Year);
RemoveItemsFromCache(i => i is Genre);
RemoveItemsFromCache(i => i is MusicGenre);
RemoveItemsFromCache(i => i is GameGenre);
RemoveItemsFromCache(i => i is Studio);
RemoveItemsFromCache(i =>
{
var artist = i as MusicArtist;
return artist != null && artist.IsAccessedByName;
});
}
private void RemoveItemsFromCache(Func<BaseItem, bool> remove)
{
var items = _libraryItemsCache.ToList().Where(i => remove(i.Value)).ToList();
foreach (var item in items)
{
BaseItem value;
_libraryItemsCache.TryRemove(item.Key, out value);
}
}
/// <summary>
/// Updates the season zero names.
/// </summary>
@ -378,28 +404,17 @@ namespace MediaBrowser.Server.Implementations.Library
/// <param name="item">The item.</param>
private void UpdateItemInLibraryCache(BaseItem item)
{
if (item is IItemByName)
{
var hasDualAccess = item as IHasDualAccess;
if (hasDualAccess != null)
{
if (hasDualAccess.IsAccessedByName)
{
return;
}
}
else
{
return;
}
}
RegisterItem(item);
}
public void RegisterItem(BaseItem item)
{
LibraryItemsCache.AddOrUpdate(item.Id, item, delegate { return item; });
RegisterItem(item.Id, item);
}
private void RegisterItem(Guid id, BaseItem item)
{
LibraryItemsCache.AddOrUpdate(id, item, delegate { return item; });
}
public async Task DeleteItem(BaseItem item, DeleteOptions options)
@ -790,11 +805,6 @@ namespace MediaBrowser.Server.Implementations.Library
return GetItemByName<MusicArtist>(ConfigurationManager.ApplicationPaths.ArtistsPath, name);
}
/// <summary>
/// The images by name item cache
/// </summary>
private readonly ConcurrentDictionary<string, BaseItem> _itemsByName = new ConcurrentDictionary<string, BaseItem>(StringComparer.OrdinalIgnoreCase);
private T GetItemByName<T>(string path, string name)
where T : BaseItem, new()
{
@ -812,24 +822,26 @@ namespace MediaBrowser.Server.Implementations.Library
string subFolderPrefix = null;
if (typeof(T) == typeof(Person) && ConfigurationManager.Configuration.EnablePeoplePrefixSubFolders)
var type = typeof(T);
if (type == typeof(Person) && ConfigurationManager.Configuration.EnablePeoplePrefixSubFolders)
{
subFolderPrefix = validFilename.Substring(0, 1);
}
var key = string.IsNullOrEmpty(subFolderPrefix) ?
var fullPath = string.IsNullOrEmpty(subFolderPrefix) ?
Path.Combine(path, validFilename) :
Path.Combine(path, subFolderPrefix, validFilename);
var id = fullPath.GetMBId(type);
BaseItem obj;
if (!_itemsByName.TryGetValue(key, out obj))
if (!_libraryItemsCache.TryGetValue(id, out obj))
{
var tuple = CreateItemByName<T>(key, name);
obj = CreateItemByName<T>(fullPath, name, id);
obj = tuple.Item2;
_itemsByName.AddOrUpdate(key, obj, (keyName, oldValue) => obj);
RegisterItem(id, obj);
}
return obj as T;
@ -843,7 +855,7 @@ namespace MediaBrowser.Server.Implementations.Library
/// <param name="name">The name.</param>
/// <returns>Task{``0}.</returns>
/// <exception cref="System.IO.IOException">Path not created: + path</exception>
private Tuple<bool, T> CreateItemByName<T>(string path, string name)
private T CreateItemByName<T>(string path, string name, Guid id)
where T : BaseItem, new()
{
var isArtist = typeof(T) == typeof(MusicArtist);
@ -856,7 +868,7 @@ namespace MediaBrowser.Server.Implementations.Library
if (existing != null)
{
return new Tuple<bool, T>(false, existing);
return existing;
}
}
@ -877,10 +889,6 @@ namespace MediaBrowser.Server.Implementations.Library
isNew = true;
}
var type = typeof(T);
var id = path.GetMBId(type);
var item = isNew ? null : RetrieveItem(id) as T;
if (item == null)
@ -893,7 +901,6 @@ namespace MediaBrowser.Server.Implementations.Library
DateModified = _fileSystem.GetLastWriteTimeUtc(fileInfo),
Path = path
};
isNew = true;
}
if (isArtist)
@ -901,7 +908,7 @@ namespace MediaBrowser.Server.Implementations.Library
(item as MusicArtist).IsAccessedByName = true;
}
return new Tuple<bool, T>(isNew, item);
return item;
}
/// <summary>