| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.ComponentModel.DataAnnotations; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Linq; |
| | | 7 | | using System.Threading; |
| | | 8 | | using System.Threading.Tasks; |
| | | 9 | | using Jellyfin.Api.Attributes; |
| | | 10 | | using Jellyfin.Api.Extensions; |
| | | 11 | | using Jellyfin.Api.Helpers; |
| | | 12 | | using Jellyfin.Api.ModelBinders; |
| | | 13 | | using Jellyfin.Api.Models.LibraryDtos; |
| | | 14 | | using Jellyfin.Data.Enums; |
| | | 15 | | using Jellyfin.Database.Implementations.Entities; |
| | | 16 | | using Jellyfin.Database.Implementations.Enums; |
| | | 17 | | using Jellyfin.Extensions; |
| | | 18 | | using MediaBrowser.Common.Api; |
| | | 19 | | using MediaBrowser.Common.Extensions; |
| | | 20 | | using MediaBrowser.Controller.Collections; |
| | | 21 | | using MediaBrowser.Controller.Configuration; |
| | | 22 | | using MediaBrowser.Controller.Dto; |
| | | 23 | | using MediaBrowser.Controller.Entities; |
| | | 24 | | using MediaBrowser.Controller.Entities.Audio; |
| | | 25 | | using MediaBrowser.Controller.Entities.Movies; |
| | | 26 | | using MediaBrowser.Controller.Entities.TV; |
| | | 27 | | using MediaBrowser.Controller.IO; |
| | | 28 | | using MediaBrowser.Controller.Library; |
| | | 29 | | using MediaBrowser.Controller.Providers; |
| | | 30 | | using MediaBrowser.Model.Activity; |
| | | 31 | | using MediaBrowser.Model.Configuration; |
| | | 32 | | using MediaBrowser.Model.Dto; |
| | | 33 | | using MediaBrowser.Model.Entities; |
| | | 34 | | using MediaBrowser.Model.Globalization; |
| | | 35 | | using MediaBrowser.Model.Net; |
| | | 36 | | using MediaBrowser.Model.Querying; |
| | | 37 | | using Microsoft.AspNetCore.Authorization; |
| | | 38 | | using Microsoft.AspNetCore.Http; |
| | | 39 | | using Microsoft.AspNetCore.Mvc; |
| | | 40 | | using Microsoft.Extensions.Logging; |
| | | 41 | | |
| | | 42 | | namespace Jellyfin.Api.Controllers; |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Library Controller. |
| | | 46 | | /// </summary> |
| | | 47 | | [Route("")] |
| | | 48 | | public class LibraryController : BaseJellyfinApiController |
| | | 49 | | { |
| | | 50 | | private readonly IProviderManager _providerManager; |
| | | 51 | | private readonly ISimilarItemsManager _similarItemsManager; |
| | | 52 | | private readonly ILibraryManager _libraryManager; |
| | | 53 | | private readonly IUserManager _userManager; |
| | | 54 | | private readonly ICollectionManager _collectionManager; |
| | | 55 | | private readonly IDtoService _dtoService; |
| | | 56 | | private readonly IActivityManager _activityManager; |
| | | 57 | | private readonly ILocalizationManager _localization; |
| | | 58 | | private readonly ILibraryMonitor _libraryMonitor; |
| | | 59 | | private readonly ILogger<LibraryController> _logger; |
| | | 60 | | private readonly IServerConfigurationManager _serverConfigurationManager; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Initializes a new instance of the <see cref="LibraryController"/> class. |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="providerManager">Instance of the <see cref="IProviderManager"/> interface.</param> |
| | | 66 | | /// <param name="similarItemsManager">Instance of the <see cref="ISimilarItemsManager"/> interface.</param> |
| | | 67 | | /// <param name="libraryManager">Instance of the <see cref="ILibraryManager"/> interface.</param> |
| | | 68 | | /// <param name="userManager">Instance of the <see cref="IUserManager"/> interface.</param> |
| | | 69 | | /// <param name="collectionManager">Instance of the <see cref="ICollectionManager"/> interface.</param> |
| | | 70 | | /// <param name="dtoService">Instance of the <see cref="IDtoService"/> interface.</param> |
| | | 71 | | /// <param name="activityManager">Instance of the <see cref="IActivityManager"/> interface.</param> |
| | | 72 | | /// <param name="localization">Instance of the <see cref="ILocalizationManager"/> interface.</param> |
| | | 73 | | /// <param name="libraryMonitor">Instance of the <see cref="ILibraryMonitor"/> interface.</param> |
| | | 74 | | /// <param name="logger">Instance of the <see cref="ILogger{LibraryController}"/> interface.</param> |
| | | 75 | | /// <param name="serverConfigurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</p |
| | 15 | 76 | | public LibraryController( |
| | 15 | 77 | | IProviderManager providerManager, |
| | 15 | 78 | | ISimilarItemsManager similarItemsManager, |
| | 15 | 79 | | ILibraryManager libraryManager, |
| | 15 | 80 | | IUserManager userManager, |
| | 15 | 81 | | ICollectionManager collectionManager, |
| | 15 | 82 | | IDtoService dtoService, |
| | 15 | 83 | | IActivityManager activityManager, |
| | 15 | 84 | | ILocalizationManager localization, |
| | 15 | 85 | | ILibraryMonitor libraryMonitor, |
| | 15 | 86 | | ILogger<LibraryController> logger, |
| | 15 | 87 | | IServerConfigurationManager serverConfigurationManager) |
| | | 88 | | { |
| | 15 | 89 | | _providerManager = providerManager; |
| | 15 | 90 | | _similarItemsManager = similarItemsManager; |
| | 15 | 91 | | _libraryManager = libraryManager; |
| | 15 | 92 | | _userManager = userManager; |
| | 15 | 93 | | _collectionManager = collectionManager; |
| | 15 | 94 | | _dtoService = dtoService; |
| | 15 | 95 | | _activityManager = activityManager; |
| | 15 | 96 | | _localization = localization; |
| | 15 | 97 | | _libraryMonitor = libraryMonitor; |
| | 15 | 98 | | _logger = logger; |
| | 15 | 99 | | _serverConfigurationManager = serverConfigurationManager; |
| | 15 | 100 | | } |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Get the original file of an item. |
| | | 104 | | /// </summary> |
| | | 105 | | /// <param name="itemId">The item id.</param> |
| | | 106 | | /// <response code="200">File stream returned.</response> |
| | | 107 | | /// <response code="404">Item not found.</response> |
| | | 108 | | /// <returns>A <see cref="FileStreamResult"/> with the original file.</returns> |
| | | 109 | | [HttpGet("Items/{itemId}/File")] |
| | | 110 | | [Authorize] |
| | | 111 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 112 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 113 | | [ProducesFile("video/*", "audio/*")] |
| | | 114 | | public ActionResult GetFile([FromRoute, Required] Guid itemId) |
| | | 115 | | { |
| | 1 | 116 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, User.GetUserId()); |
| | 1 | 117 | | if (item is null) |
| | | 118 | | { |
| | 1 | 119 | | return NotFound(); |
| | | 120 | | } |
| | | 121 | | |
| | 0 | 122 | | var filePath = item.Path; |
| | 0 | 123 | | if (item.IsFileProtocol) |
| | | 124 | | { |
| | | 125 | | // PhysicalFile does not work well with symlinks at the moment. |
| | 0 | 126 | | var resolved = FileSystemHelper.ResolveLinkTarget(filePath, returnFinalTarget: true); |
| | 0 | 127 | | if (resolved is not null && resolved.Exists) |
| | | 128 | | { |
| | 0 | 129 | | filePath = resolved.FullName; |
| | | 130 | | } |
| | | 131 | | } |
| | | 132 | | |
| | 0 | 133 | | return PhysicalFile(filePath, MimeTypes.GetMimeType(filePath), true); |
| | | 134 | | } |
| | | 135 | | |
| | | 136 | | /// <summary> |
| | | 137 | | /// Get theme songs for an item. |
| | | 138 | | /// </summary> |
| | | 139 | | /// <param name="itemId">The item id.</param> |
| | | 140 | | /// <param name="userId">Optional. Filter by user id, and attach user data.</param> |
| | | 141 | | /// <param name="inheritFromParent">Optional. Determines whether or not parent items should be searched for theme me |
| | | 142 | | /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Ar |
| | | 143 | | /// <param name="sortOrder">Optional. Sort Order - Ascending, Descending.</param> |
| | | 144 | | /// <response code="200">Theme songs returned.</response> |
| | | 145 | | /// <response code="404">Item not found.</response> |
| | | 146 | | /// <returns>The item theme songs.</returns> |
| | | 147 | | [HttpGet("Items/{itemId}/ThemeSongs")] |
| | | 148 | | [Authorize] |
| | | 149 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 150 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 151 | | public ActionResult<ThemeMediaResult> GetThemeSongs( |
| | | 152 | | [FromRoute, Required] Guid itemId, |
| | | 153 | | [FromQuery] Guid? userId, |
| | | 154 | | [FromQuery] bool inheritFromParent = false, |
| | | 155 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemSortBy[]? sortBy = null, |
| | | 156 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] SortOrder[]? sortOrder = null) |
| | | 157 | | { |
| | 2 | 158 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 2 | 159 | | var user = userId.IsNullOrEmpty() |
| | 2 | 160 | | ? null |
| | 2 | 161 | | : _userManager.GetUserById(userId.Value); |
| | | 162 | | |
| | 2 | 163 | | var item = itemId.IsEmpty() |
| | 2 | 164 | | ? (userId.IsNullOrEmpty() |
| | 2 | 165 | | ? _libraryManager.RootFolder |
| | 2 | 166 | | : _libraryManager.GetUserRootFolder()) |
| | 2 | 167 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 2 | 168 | | if (item is null) |
| | | 169 | | { |
| | 2 | 170 | | return NotFound(); |
| | | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | sortOrder ??= []; |
| | 0 | 174 | | sortBy ??= []; |
| | 0 | 175 | | var orderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder); |
| | | 176 | | |
| | | 177 | | IReadOnlyList<BaseItem> themeItems; |
| | | 178 | | |
| | 0 | 179 | | while (true) |
| | | 180 | | { |
| | 0 | 181 | | themeItems = item.GetThemeSongs(user, orderBy); |
| | | 182 | | |
| | 0 | 183 | | if (themeItems.Count > 0 || !inheritFromParent) |
| | | 184 | | { |
| | | 185 | | break; |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | var parent = item.GetParent(); |
| | 0 | 189 | | if (parent is null) |
| | | 190 | | { |
| | | 191 | | break; |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | item = parent; |
| | | 195 | | } |
| | | 196 | | |
| | 0 | 197 | | var dtoOptions = new DtoOptions(); |
| | 0 | 198 | | var items = themeItems |
| | 0 | 199 | | .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)) |
| | 0 | 200 | | .ToArray(); |
| | | 201 | | |
| | 0 | 202 | | return new ThemeMediaResult |
| | 0 | 203 | | { |
| | 0 | 204 | | Items = items, |
| | 0 | 205 | | TotalRecordCount = items.Length, |
| | 0 | 206 | | OwnerId = item.Id |
| | 0 | 207 | | }; |
| | | 208 | | } |
| | | 209 | | |
| | | 210 | | /// <summary> |
| | | 211 | | /// Get theme videos for an item. |
| | | 212 | | /// </summary> |
| | | 213 | | /// <param name="itemId">The item id.</param> |
| | | 214 | | /// <param name="userId">Optional. Filter by user id, and attach user data.</param> |
| | | 215 | | /// <param name="inheritFromParent">Optional. Determines whether or not parent items should be searched for theme me |
| | | 216 | | /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Ar |
| | | 217 | | /// <param name="sortOrder">Optional. Sort Order - Ascending, Descending.</param> |
| | | 218 | | /// <response code="200">Theme videos returned.</response> |
| | | 219 | | /// <response code="404">Item not found.</response> |
| | | 220 | | /// <returns>The item theme videos.</returns> |
| | | 221 | | [HttpGet("Items/{itemId}/ThemeVideos")] |
| | | 222 | | [Authorize] |
| | | 223 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 224 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 225 | | public ActionResult<ThemeMediaResult> GetThemeVideos( |
| | | 226 | | [FromRoute, Required] Guid itemId, |
| | | 227 | | [FromQuery] Guid? userId, |
| | | 228 | | [FromQuery] bool inheritFromParent = false, |
| | | 229 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemSortBy[]? sortBy = null, |
| | | 230 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] SortOrder[]? sortOrder = null) |
| | | 231 | | { |
| | 2 | 232 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 2 | 233 | | var user = userId.IsNullOrEmpty() |
| | 2 | 234 | | ? null |
| | 2 | 235 | | : _userManager.GetUserById(userId.Value); |
| | 2 | 236 | | var item = itemId.IsEmpty() |
| | 2 | 237 | | ? (userId.IsNullOrEmpty() |
| | 2 | 238 | | ? _libraryManager.RootFolder |
| | 2 | 239 | | : _libraryManager.GetUserRootFolder()) |
| | 2 | 240 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 2 | 241 | | if (item is null) |
| | | 242 | | { |
| | 2 | 243 | | return NotFound(); |
| | | 244 | | } |
| | | 245 | | |
| | 0 | 246 | | sortOrder ??= []; |
| | 0 | 247 | | sortBy ??= []; |
| | 0 | 248 | | var orderBy = RequestHelpers.GetOrderBy(sortBy, sortOrder); |
| | | 249 | | |
| | | 250 | | IEnumerable<BaseItem> themeItems; |
| | | 251 | | |
| | 0 | 252 | | while (true) |
| | | 253 | | { |
| | 0 | 254 | | themeItems = item.GetThemeVideos(user, orderBy); |
| | | 255 | | |
| | 0 | 256 | | if (themeItems.Any() || !inheritFromParent) |
| | | 257 | | { |
| | | 258 | | break; |
| | | 259 | | } |
| | | 260 | | |
| | 0 | 261 | | var parent = item.GetParent(); |
| | 0 | 262 | | if (parent is null) |
| | | 263 | | { |
| | | 264 | | break; |
| | | 265 | | } |
| | | 266 | | |
| | 0 | 267 | | item = parent; |
| | | 268 | | } |
| | | 269 | | |
| | 0 | 270 | | var dtoOptions = new DtoOptions(); |
| | 0 | 271 | | var items = themeItems |
| | 0 | 272 | | .Select(i => _dtoService.GetBaseItemDto(i, dtoOptions, user, item)) |
| | 0 | 273 | | .ToArray(); |
| | | 274 | | |
| | 0 | 275 | | return new ThemeMediaResult |
| | 0 | 276 | | { |
| | 0 | 277 | | Items = items, |
| | 0 | 278 | | TotalRecordCount = items.Length, |
| | 0 | 279 | | OwnerId = item.Id |
| | 0 | 280 | | }; |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | /// <summary> |
| | | 284 | | /// Get theme songs and videos for an item. |
| | | 285 | | /// </summary> |
| | | 286 | | /// <param name="itemId">The item id.</param> |
| | | 287 | | /// <param name="userId">Optional. Filter by user id, and attach user data.</param> |
| | | 288 | | /// <param name="inheritFromParent">Optional. Determines whether or not parent items should be searched for theme me |
| | | 289 | | /// <param name="sortBy">Optional. Specify one or more sort orders, comma delimited. Options: Album, AlbumArtist, Ar |
| | | 290 | | /// <param name="sortOrder">Optional. Sort Order - Ascending, Descending.</param> |
| | | 291 | | /// <response code="200">Theme songs and videos returned.</response> |
| | | 292 | | /// <response code="404">Item not found.</response> |
| | | 293 | | /// <returns>The item theme videos.</returns> |
| | | 294 | | [HttpGet("Items/{itemId}/ThemeMedia")] |
| | | 295 | | [Authorize] |
| | | 296 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 297 | | public ActionResult<AllThemeMediaResult> GetThemeMedia( |
| | | 298 | | [FromRoute, Required] Guid itemId, |
| | | 299 | | [FromQuery] Guid? userId, |
| | | 300 | | [FromQuery] bool inheritFromParent = false, |
| | | 301 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemSortBy[]? sortBy = null, |
| | | 302 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] SortOrder[]? sortOrder = null) |
| | | 303 | | { |
| | 1 | 304 | | var themeSongs = GetThemeSongs( |
| | 1 | 305 | | itemId, |
| | 1 | 306 | | userId, |
| | 1 | 307 | | inheritFromParent, |
| | 1 | 308 | | sortBy, |
| | 1 | 309 | | sortOrder); |
| | | 310 | | |
| | 1 | 311 | | var themeVideos = GetThemeVideos( |
| | 1 | 312 | | itemId, |
| | 1 | 313 | | userId, |
| | 1 | 314 | | inheritFromParent, |
| | 1 | 315 | | sortBy, |
| | 1 | 316 | | sortOrder); |
| | | 317 | | |
| | 1 | 318 | | if (themeSongs.Result is StatusCodeResult { StatusCode: StatusCodes.Status404NotFound } |
| | 1 | 319 | | || themeVideos.Result is StatusCodeResult { StatusCode: StatusCodes.Status404NotFound }) |
| | | 320 | | { |
| | 1 | 321 | | return NotFound(); |
| | | 322 | | } |
| | | 323 | | |
| | 0 | 324 | | return new AllThemeMediaResult |
| | 0 | 325 | | { |
| | 0 | 326 | | ThemeSongsResult = themeSongs.Value, |
| | 0 | 327 | | ThemeVideosResult = themeVideos.Value, |
| | 0 | 328 | | SoundtrackSongsResult = new ThemeMediaResult() |
| | 0 | 329 | | }; |
| | | 330 | | } |
| | | 331 | | |
| | | 332 | | /// <summary> |
| | | 333 | | /// Starts a library scan. |
| | | 334 | | /// </summary> |
| | | 335 | | /// <response code="204">Library scan started.</response> |
| | | 336 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 337 | | [HttpPost("Library/Refresh")] |
| | | 338 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 339 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 340 | | public async Task<ActionResult> RefreshLibrary() |
| | | 341 | | { |
| | | 342 | | try |
| | | 343 | | { |
| | 0 | 344 | | await _libraryManager.ValidateMediaLibrary(new Progress<double>(), CancellationToken.None).ConfigureAwait(fa |
| | 0 | 345 | | } |
| | 0 | 346 | | catch (Exception ex) |
| | | 347 | | { |
| | 0 | 348 | | _logger.LogError(ex, "Error refreshing library"); |
| | 0 | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | return NoContent(); |
| | 0 | 352 | | } |
| | | 353 | | |
| | | 354 | | /// <summary> |
| | | 355 | | /// Deletes an item from the library and filesystem. |
| | | 356 | | /// </summary> |
| | | 357 | | /// <param name="itemId">The item id.</param> |
| | | 358 | | /// <response code="204">Item deleted.</response> |
| | | 359 | | /// <response code="401">Unauthorized access.</response> |
| | | 360 | | /// <response code="404">Item not found.</response> |
| | | 361 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 362 | | [HttpDelete("Items/{itemId}")] |
| | | 363 | | [Authorize] |
| | | 364 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 365 | | [ProducesResponseType(StatusCodes.Status401Unauthorized)] |
| | | 366 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 367 | | public ActionResult DeleteItem(Guid itemId) |
| | | 368 | | { |
| | 1 | 369 | | var userId = User.GetUserId(); |
| | 1 | 370 | | var isApiKey = User.GetIsApiKey(); |
| | 1 | 371 | | var user = userId.IsEmpty() && isApiKey |
| | 1 | 372 | | ? null |
| | 1 | 373 | | : _userManager.GetUserById(userId); |
| | | 374 | | |
| | 1 | 375 | | if (user is null && !isApiKey) |
| | | 376 | | { |
| | 0 | 377 | | return NotFound(); |
| | | 378 | | } |
| | | 379 | | |
| | 1 | 380 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 1 | 381 | | if (item is null) |
| | | 382 | | { |
| | 1 | 383 | | return NotFound(); |
| | | 384 | | } |
| | | 385 | | |
| | 0 | 386 | | if (user is not null && !item.CanDelete(user)) |
| | | 387 | | { |
| | 0 | 388 | | return Unauthorized("Unauthorized access"); |
| | | 389 | | } |
| | | 390 | | |
| | 0 | 391 | | _libraryManager.DeleteItem( |
| | 0 | 392 | | item, |
| | 0 | 393 | | new DeleteOptions { DeleteFileLocation = true }, |
| | 0 | 394 | | true); |
| | | 395 | | |
| | 0 | 396 | | return NoContent(); |
| | | 397 | | } |
| | | 398 | | |
| | | 399 | | /// <summary> |
| | | 400 | | /// Deletes items from the library and filesystem. |
| | | 401 | | /// </summary> |
| | | 402 | | /// <param name="ids">The item ids.</param> |
| | | 403 | | /// <response code="204">Items deleted.</response> |
| | | 404 | | /// <response code="401">Unauthorized access.</response> |
| | | 405 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 406 | | [HttpDelete("Items")] |
| | | 407 | | [Authorize] |
| | | 408 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 409 | | [ProducesResponseType(StatusCodes.Status401Unauthorized)] |
| | | 410 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 411 | | public ActionResult DeleteItems([FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] Guid[] ids) |
| | | 412 | | { |
| | 1 | 413 | | var isApiKey = User.GetIsApiKey(); |
| | 1 | 414 | | var userId = User.GetUserId(); |
| | 1 | 415 | | var user = !isApiKey && !userId.IsEmpty() |
| | 1 | 416 | | ? _userManager.GetUserById(userId) ?? throw new ResourceNotFoundException() |
| | 1 | 417 | | : null; |
| | | 418 | | |
| | 1 | 419 | | if (!isApiKey && user is null) |
| | | 420 | | { |
| | 0 | 421 | | return Unauthorized("Unauthorized access"); |
| | | 422 | | } |
| | | 423 | | |
| | 3 | 424 | | foreach (var i in ids) |
| | | 425 | | { |
| | 1 | 426 | | var item = _libraryManager.GetItemById<BaseItem>(i, user); |
| | 1 | 427 | | if (item is null) |
| | | 428 | | { |
| | 1 | 429 | | return NotFound(); |
| | | 430 | | } |
| | | 431 | | |
| | 0 | 432 | | if (user is not null && !item.CanDelete(user)) |
| | | 433 | | { |
| | 0 | 434 | | return Unauthorized("Unauthorized access"); |
| | | 435 | | } |
| | | 436 | | |
| | 0 | 437 | | _libraryManager.DeleteItem( |
| | 0 | 438 | | item, |
| | 0 | 439 | | new DeleteOptions { DeleteFileLocation = true }, |
| | 0 | 440 | | true); |
| | | 441 | | } |
| | | 442 | | |
| | 0 | 443 | | return NoContent(); |
| | | 444 | | } |
| | | 445 | | |
| | | 446 | | /// <summary> |
| | | 447 | | /// Get item counts. |
| | | 448 | | /// </summary> |
| | | 449 | | /// <param name="userId">Optional. Get counts from a specific user's library.</param> |
| | | 450 | | /// <param name="isFavorite">Optional. Get counts of favorite items.</param> |
| | | 451 | | /// <response code="200">Item counts returned.</response> |
| | | 452 | | /// <returns>Item counts.</returns> |
| | | 453 | | [HttpGet("Items/Counts")] |
| | | 454 | | [Authorize] |
| | | 455 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 456 | | public ActionResult<ItemCounts> GetItemCounts( |
| | | 457 | | [FromQuery] Guid? userId, |
| | | 458 | | [FromQuery] bool? isFavorite) |
| | | 459 | | { |
| | 0 | 460 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 0 | 461 | | var user = userId.IsNullOrEmpty() |
| | 0 | 462 | | ? null |
| | 0 | 463 | | : _userManager.GetUserById(userId.Value); |
| | | 464 | | |
| | 0 | 465 | | var query = new InternalItemsQuery(user) |
| | 0 | 466 | | { |
| | 0 | 467 | | Recursive = true, |
| | 0 | 468 | | IsVirtualItem = false, |
| | 0 | 469 | | IsFavorite = isFavorite, |
| | 0 | 470 | | DtoOptions = new DtoOptions(false) |
| | 0 | 471 | | { |
| | 0 | 472 | | EnableImages = false |
| | 0 | 473 | | } |
| | 0 | 474 | | }; |
| | | 475 | | |
| | 0 | 476 | | return _libraryManager.GetItemCounts(query); |
| | | 477 | | } |
| | | 478 | | |
| | | 479 | | /// <summary> |
| | | 480 | | /// Gets all parents of an item. |
| | | 481 | | /// </summary> |
| | | 482 | | /// <param name="itemId">The item id.</param> |
| | | 483 | | /// <param name="userId">Optional. Filter by user id, and attach user data.</param> |
| | | 484 | | /// <response code="200">Item parents returned.</response> |
| | | 485 | | /// <response code="404">Item not found.</response> |
| | | 486 | | /// <returns>Item parents.</returns> |
| | | 487 | | [HttpGet("Items/{itemId}/Ancestors")] |
| | | 488 | | [Authorize] |
| | | 489 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 490 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 491 | | public ActionResult<IEnumerable<BaseItemDto>> GetAncestors([FromRoute, Required] Guid itemId, [FromQuery] Guid? user |
| | | 492 | | { |
| | 1 | 493 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 1 | 494 | | var user = userId.IsNullOrEmpty() |
| | 1 | 495 | | ? null |
| | 1 | 496 | | : _userManager.GetUserById(userId.Value); |
| | 1 | 497 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 1 | 498 | | if (item is null) |
| | | 499 | | { |
| | 1 | 500 | | return NotFound(); |
| | | 501 | | } |
| | | 502 | | |
| | 0 | 503 | | var baseItemDtos = new List<BaseItemDto>(); |
| | | 504 | | |
| | 0 | 505 | | var dtoOptions = new DtoOptions(); |
| | 0 | 506 | | BaseItem? parent = item.GetParent(); |
| | | 507 | | |
| | 0 | 508 | | while (parent is not null) |
| | | 509 | | { |
| | 0 | 510 | | if (user is not null) |
| | | 511 | | { |
| | 0 | 512 | | parent = TranslateParentItem(parent, user); |
| | 0 | 513 | | if (parent is null) |
| | | 514 | | { |
| | | 515 | | break; |
| | | 516 | | } |
| | | 517 | | } |
| | | 518 | | |
| | 0 | 519 | | baseItemDtos.Add(_dtoService.GetBaseItemDto(parent, dtoOptions, user)); |
| | | 520 | | |
| | 0 | 521 | | parent = parent.GetParent(); |
| | | 522 | | } |
| | | 523 | | |
| | 0 | 524 | | return baseItemDtos; |
| | | 525 | | } |
| | | 526 | | |
| | | 527 | | /// <summary> |
| | | 528 | | /// Gets a list of physical paths from virtual folders. |
| | | 529 | | /// </summary> |
| | | 530 | | /// <response code="200">Physical paths returned.</response> |
| | | 531 | | /// <returns>List of physical paths.</returns> |
| | | 532 | | [HttpGet("Library/PhysicalPaths")] |
| | | 533 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 534 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 535 | | public ActionResult<IEnumerable<string>> GetPhysicalPaths() |
| | | 536 | | { |
| | 0 | 537 | | return Ok(_libraryManager.RootFolder.Children |
| | 0 | 538 | | .SelectMany(c => c.PhysicalLocations)); |
| | | 539 | | } |
| | | 540 | | |
| | | 541 | | /// <summary> |
| | | 542 | | /// Gets all user media folders. |
| | | 543 | | /// </summary> |
| | | 544 | | /// <param name="isHidden">Optional. Filter by folders that are marked hidden, or not.</param> |
| | | 545 | | /// <response code="200">Media folders returned.</response> |
| | | 546 | | /// <returns>List of user media folders.</returns> |
| | | 547 | | [HttpGet("Library/MediaFolders")] |
| | | 548 | | [Authorize(Policy = Policies.RequiresElevation)] |
| | | 549 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 550 | | public ActionResult<QueryResult<BaseItemDto>> GetMediaFolders([FromQuery] bool? isHidden) |
| | | 551 | | { |
| | 0 | 552 | | var items = _libraryManager.GetUserRootFolder().Children |
| | 0 | 553 | | .Concat(_libraryManager.RootFolder.VirtualChildren) |
| | 0 | 554 | | .Where(i => _libraryManager.GetLibraryOptions(i).Enabled) |
| | 0 | 555 | | .OrderBy(i => i.SortName) |
| | 0 | 556 | | .ToList(); |
| | | 557 | | |
| | 0 | 558 | | if (isHidden.HasValue) |
| | | 559 | | { |
| | 0 | 560 | | var val = isHidden.Value; |
| | | 561 | | |
| | 0 | 562 | | items = items.Where(i => i.IsHidden == val).ToList(); |
| | | 563 | | } |
| | | 564 | | |
| | 0 | 565 | | var dtoOptions = new DtoOptions(); |
| | 0 | 566 | | var resultArray = _dtoService.GetBaseItemDtos(items, dtoOptions); |
| | 0 | 567 | | return new QueryResult<BaseItemDto>(resultArray); |
| | | 568 | | } |
| | | 569 | | |
| | | 570 | | /// <summary> |
| | | 571 | | /// Reports that new episodes of a series have been added by an external source. |
| | | 572 | | /// </summary> |
| | | 573 | | /// <param name="tvdbId">The tvdbId.</param> |
| | | 574 | | /// <response code="204">Report success.</response> |
| | | 575 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 576 | | [HttpPost("Library/Series/Added", Name = "PostAddedSeries")] |
| | | 577 | | [HttpPost("Library/Series/Updated")] |
| | | 578 | | [Authorize] |
| | | 579 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 580 | | public ActionResult PostUpdatedSeries([FromQuery] string? tvdbId) |
| | | 581 | | { |
| | 0 | 582 | | var series = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 583 | | { |
| | 0 | 584 | | IncludeItemTypes = new[] { BaseItemKind.Series }, |
| | 0 | 585 | | DtoOptions = new DtoOptions(false) |
| | 0 | 586 | | { |
| | 0 | 587 | | EnableImages = false |
| | 0 | 588 | | } |
| | 0 | 589 | | }).Where(i => string.Equals(tvdbId, i.GetProviderId(MediaBrowser.Model.Entities.MetadataProvider.Tvdb), StringCo |
| | | 590 | | |
| | 0 | 591 | | foreach (var item in series) |
| | | 592 | | { |
| | 0 | 593 | | _libraryMonitor.ReportFileSystemChanged(item.Path); |
| | | 594 | | } |
| | | 595 | | |
| | 0 | 596 | | return NoContent(); |
| | | 597 | | } |
| | | 598 | | |
| | | 599 | | /// <summary> |
| | | 600 | | /// Reports that new movies have been added by an external source. |
| | | 601 | | /// </summary> |
| | | 602 | | /// <param name="tmdbId">The tmdbId.</param> |
| | | 603 | | /// <param name="imdbId">The imdbId.</param> |
| | | 604 | | /// <response code="204">Report success.</response> |
| | | 605 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 606 | | [HttpPost("Library/Movies/Added", Name = "PostAddedMovies")] |
| | | 607 | | [HttpPost("Library/Movies/Updated")] |
| | | 608 | | [Authorize] |
| | | 609 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 610 | | public ActionResult PostUpdatedMovies([FromQuery] string? tmdbId, [FromQuery] string? imdbId) |
| | | 611 | | { |
| | 0 | 612 | | var movies = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 613 | | { |
| | 0 | 614 | | IncludeItemTypes = new[] { BaseItemKind.Movie }, |
| | 0 | 615 | | DtoOptions = new DtoOptions(false) |
| | 0 | 616 | | { |
| | 0 | 617 | | EnableImages = false |
| | 0 | 618 | | } |
| | 0 | 619 | | }); |
| | | 620 | | |
| | 0 | 621 | | if (!string.IsNullOrWhiteSpace(imdbId)) |
| | | 622 | | { |
| | 0 | 623 | | movies = movies.Where(i => string.Equals(imdbId, i.GetProviderId(MediaBrowser.Model.Entities.MetadataProvide |
| | | 624 | | } |
| | 0 | 625 | | else if (!string.IsNullOrWhiteSpace(tmdbId)) |
| | | 626 | | { |
| | 0 | 627 | | movies = movies.Where(i => string.Equals(tmdbId, i.GetProviderId(MediaBrowser.Model.Entities.MetadataProvide |
| | | 628 | | } |
| | | 629 | | else |
| | | 630 | | { |
| | 0 | 631 | | movies = new List<BaseItem>(); |
| | | 632 | | } |
| | | 633 | | |
| | 0 | 634 | | foreach (var item in movies) |
| | | 635 | | { |
| | 0 | 636 | | _libraryMonitor.ReportFileSystemChanged(item.Path); |
| | | 637 | | } |
| | | 638 | | |
| | 0 | 639 | | return NoContent(); |
| | | 640 | | } |
| | | 641 | | |
| | | 642 | | /// <summary> |
| | | 643 | | /// Reports that new movies have been added by an external source. |
| | | 644 | | /// </summary> |
| | | 645 | | /// <param name="dto">The update paths.</param> |
| | | 646 | | /// <response code="204">Report success.</response> |
| | | 647 | | /// <returns>A <see cref="NoContentResult"/>.</returns> |
| | | 648 | | [HttpPost("Library/Media/Updated")] |
| | | 649 | | [Authorize] |
| | | 650 | | [ProducesResponseType(StatusCodes.Status204NoContent)] |
| | | 651 | | public ActionResult PostUpdatedMedia([FromBody, Required] MediaUpdateInfoDto dto) |
| | | 652 | | { |
| | 0 | 653 | | foreach (var item in dto.Updates) |
| | | 654 | | { |
| | 0 | 655 | | _libraryMonitor.ReportFileSystemChanged(item.Path ?? throw new ArgumentException("Item path can't be null.") |
| | | 656 | | } |
| | | 657 | | |
| | 0 | 658 | | return NoContent(); |
| | | 659 | | } |
| | | 660 | | |
| | | 661 | | /// <summary> |
| | | 662 | | /// Downloads item media. |
| | | 663 | | /// </summary> |
| | | 664 | | /// <param name="itemId">The item id.</param> |
| | | 665 | | /// <response code="200">Media downloaded.</response> |
| | | 666 | | /// <response code="404">Item not found.</response> |
| | | 667 | | /// <returns>A <see cref="FileResult"/> containing the media stream.</returns> |
| | | 668 | | /// <exception cref="ArgumentException">User can't download or item can't be downloaded.</exception> |
| | | 669 | | [HttpGet("Items/{itemId}/Download")] |
| | | 670 | | [Authorize(Policy = Policies.Download)] |
| | | 671 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 672 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 673 | | [ProducesFile("video/*", "audio/*")] |
| | | 674 | | public async Task<ActionResult> GetDownload([FromRoute, Required] Guid itemId) |
| | | 675 | | { |
| | 1 | 676 | | var userId = User.GetUserId(); |
| | 1 | 677 | | var user = userId.IsEmpty() |
| | 1 | 678 | | ? null |
| | 1 | 679 | | : _userManager.GetUserById(userId); |
| | 1 | 680 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 1 | 681 | | if (item is null) |
| | | 682 | | { |
| | 1 | 683 | | return NotFound(); |
| | | 684 | | } |
| | | 685 | | |
| | 0 | 686 | | if (user is not null) |
| | | 687 | | { |
| | 0 | 688 | | if (!item.CanDownload(user)) |
| | | 689 | | { |
| | 0 | 690 | | throw new ArgumentException("Item does not support downloading"); |
| | | 691 | | } |
| | | 692 | | } |
| | | 693 | | else |
| | | 694 | | { |
| | 0 | 695 | | if (!item.CanDownload()) |
| | | 696 | | { |
| | 0 | 697 | | throw new ArgumentException("Item does not support downloading"); |
| | | 698 | | } |
| | | 699 | | } |
| | | 700 | | |
| | 0 | 701 | | if (user is not null) |
| | | 702 | | { |
| | 0 | 703 | | await LogDownloadAsync(item, user).ConfigureAwait(false); |
| | | 704 | | } |
| | | 705 | | |
| | | 706 | | // Quotes are valid in linux. They'll possibly cause issues here. |
| | 0 | 707 | | var filename = Path.GetFileName(item.Path)?.Replace("\"", string.Empty, StringComparison.Ordinal); |
| | | 708 | | |
| | 0 | 709 | | var filePath = item.Path; |
| | 0 | 710 | | if (item.IsFileProtocol) |
| | | 711 | | { |
| | | 712 | | // PhysicalFile does not work well with symlinks at the moment. |
| | 0 | 713 | | var resolved = FileSystemHelper.ResolveLinkTarget(filePath, returnFinalTarget: true); |
| | 0 | 714 | | if (resolved is not null && resolved.Exists) |
| | | 715 | | { |
| | 0 | 716 | | filePath = resolved.FullName; |
| | | 717 | | } |
| | | 718 | | } |
| | | 719 | | |
| | 0 | 720 | | return PhysicalFile(filePath, MimeTypes.GetMimeType(filePath), filename, true); |
| | 1 | 721 | | } |
| | | 722 | | |
| | | 723 | | /// <summary> |
| | | 724 | | /// Gets the collections that include the specified item. |
| | | 725 | | /// </summary> |
| | | 726 | | /// <param name="itemId">The item id.</param> |
| | | 727 | | /// <param name="userId">Optional. Filter by user id, and attach user data.</param> |
| | | 728 | | /// <param name="startIndex">Optional. The index of the first record in the output.</param> |
| | | 729 | | /// <param name="limit">Optional. The maximum number of records to return.</param> |
| | | 730 | | /// <param name="fields">Optional. Specify additional fields of information to return in the output.</param> |
| | | 731 | | /// <response code="200">Collections returned.</response> |
| | | 732 | | /// <response code="401">User context missing.</response> |
| | | 733 | | /// <response code="404">Item not found.</response> |
| | | 734 | | /// <returns>The collections that contain the requested item.</returns> |
| | | 735 | | [HttpGet("Items/{itemId}/Collections")] |
| | | 736 | | [Authorize] |
| | | 737 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 738 | | [ProducesResponseType(StatusCodes.Status404NotFound)] |
| | | 739 | | public ActionResult<QueryResult<BaseItemDto>> GetItemCollections( |
| | | 740 | | [FromRoute, Required] Guid itemId, |
| | | 741 | | [FromQuery] Guid? userId, |
| | | 742 | | [FromQuery] int? startIndex, |
| | | 743 | | [FromQuery] int? limit, |
| | | 744 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields) |
| | | 745 | | { |
| | 1 | 746 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 1 | 747 | | var user = userId.IsNullOrEmpty() |
| | 1 | 748 | | ? null |
| | 1 | 749 | | : _userManager.GetUserById(userId.Value); |
| | | 750 | | |
| | 1 | 751 | | if (user is null) |
| | | 752 | | { |
| | 0 | 753 | | return Unauthorized(); |
| | | 754 | | } |
| | | 755 | | |
| | 1 | 756 | | var item = _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 1 | 757 | | if (item is null) |
| | | 758 | | { |
| | 1 | 759 | | return NotFound(); |
| | | 760 | | } |
| | | 761 | | |
| | 0 | 762 | | var dtoOptions = new DtoOptions { Fields = fields }; |
| | | 763 | | |
| | 0 | 764 | | var visibleCollections = _collectionManager |
| | 0 | 765 | | .GetCollectionsContainingItem(user, item.Id) |
| | 0 | 766 | | .OrderBy(i => i.SortName, StringComparer.OrdinalIgnoreCase) |
| | 0 | 767 | | .ThenBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 768 | | .ToList(); |
| | | 769 | | |
| | 0 | 770 | | IEnumerable<BaseItem> pagedCollections = visibleCollections; |
| | 0 | 771 | | if (startIndex.HasValue) |
| | | 772 | | { |
| | 0 | 773 | | pagedCollections = pagedCollections.Skip(startIndex.Value); |
| | | 774 | | } |
| | | 775 | | |
| | 0 | 776 | | if (limit.HasValue) |
| | | 777 | | { |
| | 0 | 778 | | pagedCollections = pagedCollections.Take(limit.Value); |
| | | 779 | | } |
| | | 780 | | |
| | 0 | 781 | | var dtos = _dtoService.GetBaseItemDtos(pagedCollections.ToList(), dtoOptions, user); |
| | | 782 | | |
| | 0 | 783 | | return new QueryResult<BaseItemDto>( |
| | 0 | 784 | | startIndex, |
| | 0 | 785 | | visibleCollections.Count, |
| | 0 | 786 | | dtos); |
| | | 787 | | } |
| | | 788 | | |
| | | 789 | | /// <summary> |
| | | 790 | | /// Gets similar items. |
| | | 791 | | /// </summary> |
| | | 792 | | /// <param name="itemId">The item id.</param> |
| | | 793 | | /// <param name="excludeArtistIds">Exclude artist ids.</param> |
| | | 794 | | /// <param name="userId">Optional. Filter by user id, and attach user data.</param> |
| | | 795 | | /// <param name="limit">Optional. The maximum number of records to return.</param> |
| | | 796 | | /// <param name="fields">Optional. Specify additional fields of information to return in the output. This allows mul |
| | | 797 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 798 | | /// <response code="200">Similar items returned.</response> |
| | | 799 | | /// <returns>A <see cref="QueryResult{BaseItemDto}"/> containing the similar items.</returns> |
| | | 800 | | [HttpGet("Artists/{itemId}/Similar", Name = "GetSimilarArtists")] |
| | | 801 | | [HttpGet("Items/{itemId}/Similar")] |
| | | 802 | | [HttpGet("Albums/{itemId}/Similar", Name = "GetSimilarAlbums")] |
| | | 803 | | [HttpGet("Shows/{itemId}/Similar", Name = "GetSimilarShows")] |
| | | 804 | | [HttpGet("Movies/{itemId}/Similar", Name = "GetSimilarMovies")] |
| | | 805 | | [HttpGet("Trailers/{itemId}/Similar", Name = "GetSimilarTrailers")] |
| | | 806 | | [Authorize] |
| | | 807 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 808 | | public async Task<ActionResult<QueryResult<BaseItemDto>>> GetSimilarItems( |
| | | 809 | | [FromRoute, Required] Guid itemId, |
| | | 810 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] Guid[] excludeArtistIds, |
| | | 811 | | [FromQuery] Guid? userId, |
| | | 812 | | [FromQuery] int? limit, |
| | | 813 | | [FromQuery, ModelBinder(typeof(CommaDelimitedCollectionModelBinder))] ItemFields[] fields, |
| | | 814 | | CancellationToken cancellationToken) |
| | | 815 | | { |
| | 6 | 816 | | userId = RequestHelpers.GetUserId(User, userId); |
| | 6 | 817 | | var user = userId.IsNullOrEmpty() |
| | 6 | 818 | | ? null |
| | 6 | 819 | | : _userManager.GetUserById(userId.Value); |
| | 6 | 820 | | var item = itemId.IsEmpty() |
| | 6 | 821 | | ? (user is null |
| | 6 | 822 | | ? _libraryManager.RootFolder |
| | 6 | 823 | | : _libraryManager.GetUserRootFolder()) |
| | 6 | 824 | | : _libraryManager.GetItemById<BaseItem>(itemId, user); |
| | 6 | 825 | | if (item is null) |
| | | 826 | | { |
| | 6 | 827 | | return NotFound(); |
| | | 828 | | } |
| | | 829 | | |
| | 0 | 830 | | if (item is Episode || (item is IItemByName && item is not MusicArtist)) |
| | | 831 | | { |
| | 0 | 832 | | return new QueryResult<BaseItemDto>(); |
| | | 833 | | } |
| | | 834 | | |
| | 0 | 835 | | var dtoOptions = new DtoOptions { Fields = fields }; |
| | | 836 | | |
| | | 837 | | // Get library options for provider configuration |
| | 0 | 838 | | var libraryOptions = _libraryManager.GetLibraryOptions(item); |
| | | 839 | | |
| | 0 | 840 | | var itemsResult = await _similarItemsManager.GetSimilarItemsAsync( |
| | 0 | 841 | | item, |
| | 0 | 842 | | excludeArtistIds, |
| | 0 | 843 | | user, |
| | 0 | 844 | | dtoOptions, |
| | 0 | 845 | | limit, |
| | 0 | 846 | | libraryOptions, |
| | 0 | 847 | | cancellationToken).ConfigureAwait(false); |
| | | 848 | | |
| | 0 | 849 | | var returnList = _dtoService.GetBaseItemDtos(itemsResult, dtoOptions, user); |
| | | 850 | | |
| | 0 | 851 | | return new QueryResult<BaseItemDto>( |
| | 0 | 852 | | 0, |
| | 0 | 853 | | itemsResult.Count, |
| | 0 | 854 | | returnList); |
| | 6 | 855 | | } |
| | | 856 | | |
| | | 857 | | /// <summary> |
| | | 858 | | /// Gets the library options info. |
| | | 859 | | /// </summary> |
| | | 860 | | /// <param name="libraryContentType">Library content type.</param> |
| | | 861 | | /// <param name="isNewLibrary">Whether this is a new library.</param> |
| | | 862 | | /// <response code="200">Library options info returned.</response> |
| | | 863 | | /// <returns>Library options info.</returns> |
| | | 864 | | [HttpGet("Libraries/AvailableOptions")] |
| | | 865 | | [Authorize(Policy = Policies.FirstTimeSetupOrDefault)] |
| | | 866 | | [ProducesResponseType(StatusCodes.Status200OK)] |
| | | 867 | | public ActionResult<LibraryOptionsResultDto> GetLibraryOptionsInfo( |
| | | 868 | | [FromQuery] CollectionType? libraryContentType, |
| | | 869 | | [FromQuery] bool isNewLibrary = false) |
| | | 870 | | { |
| | 0 | 871 | | var result = new LibraryOptionsResultDto(); |
| | | 872 | | |
| | 0 | 873 | | var types = GetRepresentativeItemTypes(libraryContentType); |
| | 0 | 874 | | var typesList = types.ToList(); |
| | | 875 | | |
| | 0 | 876 | | var plugins = _providerManager.GetAllMetadataPlugins() |
| | 0 | 877 | | .Where(i => types.Contains(i.ItemType, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 878 | | .OrderBy(i => typesList.IndexOf(i.ItemType)) |
| | 0 | 879 | | .ToList(); |
| | | 880 | | |
| | 0 | 881 | | result.MetadataSavers = plugins |
| | 0 | 882 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.MetadataSaver)) |
| | 0 | 883 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 884 | | { |
| | 0 | 885 | | Name = i.Name, |
| | 0 | 886 | | DefaultEnabled = IsSaverEnabledByDefault(i.Name, types, isNewLibrary) |
| | 0 | 887 | | }) |
| | 0 | 888 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 889 | | .ToArray(); |
| | | 890 | | |
| | 0 | 891 | | result.MetadataReaders = plugins |
| | 0 | 892 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.LocalMetadataProvider)) |
| | 0 | 893 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 894 | | { |
| | 0 | 895 | | Name = i.Name, |
| | 0 | 896 | | DefaultEnabled = true |
| | 0 | 897 | | }) |
| | 0 | 898 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 899 | | .ToArray(); |
| | | 900 | | |
| | 0 | 901 | | result.SubtitleFetchers = plugins |
| | 0 | 902 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.SubtitleFetcher)) |
| | 0 | 903 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 904 | | { |
| | 0 | 905 | | Name = i.Name, |
| | 0 | 906 | | DefaultEnabled = true |
| | 0 | 907 | | }) |
| | 0 | 908 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 909 | | .ToArray(); |
| | | 910 | | |
| | 0 | 911 | | result.LyricFetchers = plugins |
| | 0 | 912 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.LyricFetcher)) |
| | 0 | 913 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 914 | | { |
| | 0 | 915 | | Name = i.Name, |
| | 0 | 916 | | DefaultEnabled = true |
| | 0 | 917 | | }) |
| | 0 | 918 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 919 | | .ToArray(); |
| | | 920 | | |
| | 0 | 921 | | result.MediaSegmentProviders = plugins |
| | 0 | 922 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.MediaSegmentProvider)) |
| | 0 | 923 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 924 | | { |
| | 0 | 925 | | Name = i.Name, |
| | 0 | 926 | | DefaultEnabled = true |
| | 0 | 927 | | }) |
| | 0 | 928 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 929 | | .ToArray(); |
| | | 930 | | |
| | 0 | 931 | | var typeOptions = new List<LibraryTypeOptionsDto>(); |
| | | 932 | | |
| | 0 | 933 | | foreach (var type in types) |
| | | 934 | | { |
| | 0 | 935 | | TypeOptions.DefaultImageOptions.TryGetValue(type, out var defaultImageOptions); |
| | | 936 | | |
| | 0 | 937 | | typeOptions.Add(new LibraryTypeOptionsDto |
| | 0 | 938 | | { |
| | 0 | 939 | | Type = type, |
| | 0 | 940 | | |
| | 0 | 941 | | MetadataFetchers = plugins |
| | 0 | 942 | | .Where(i => string.Equals(i.ItemType, type, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 943 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.MetadataFetcher)) |
| | 0 | 944 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 945 | | { |
| | 0 | 946 | | Name = i.Name, |
| | 0 | 947 | | DefaultEnabled = IsMetadataFetcherEnabledByDefault(i.Name, type, isNewLibrary) |
| | 0 | 948 | | }) |
| | 0 | 949 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 950 | | .ToArray(), |
| | 0 | 951 | | |
| | 0 | 952 | | ImageFetchers = plugins |
| | 0 | 953 | | .Where(i => string.Equals(i.ItemType, type, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 954 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.ImageFetcher)) |
| | 0 | 955 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 956 | | { |
| | 0 | 957 | | Name = i.Name, |
| | 0 | 958 | | DefaultEnabled = IsImageFetcherEnabledByDefault(i.Name, type, isNewLibrary) |
| | 0 | 959 | | }) |
| | 0 | 960 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 961 | | .ToArray(), |
| | 0 | 962 | | |
| | 0 | 963 | | SimilarItemProviders = plugins |
| | 0 | 964 | | .Where(i => string.Equals(i.ItemType, type, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 965 | | .SelectMany(i => i.Plugins.Where(p => p.Type == MetadataPluginType.LocalSimilarityProvider || p.Type |
| | 0 | 966 | | .Select(i => new LibraryOptionInfoDto |
| | 0 | 967 | | { |
| | 0 | 968 | | Name = i.Name, |
| | 0 | 969 | | DefaultEnabled = i.Type == MetadataPluginType.LocalSimilarityProvider |
| | 0 | 970 | | }) |
| | 0 | 971 | | .DistinctBy(i => i.Name, StringComparer.OrdinalIgnoreCase) |
| | 0 | 972 | | .ToArray(), |
| | 0 | 973 | | |
| | 0 | 974 | | SupportedImageTypes = plugins |
| | 0 | 975 | | .Where(i => string.Equals(i.ItemType, type, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 976 | | .SelectMany(i => i.SupportedImageTypes ?? Array.Empty<ImageType>()) |
| | 0 | 977 | | .Distinct() |
| | 0 | 978 | | .ToArray(), |
| | 0 | 979 | | |
| | 0 | 980 | | DefaultImageOptions = defaultImageOptions ?? Array.Empty<ImageOption>() |
| | 0 | 981 | | }); |
| | | 982 | | } |
| | | 983 | | |
| | 0 | 984 | | result.TypeOptions = typeOptions.ToArray(); |
| | | 985 | | |
| | 0 | 986 | | return result; |
| | | 987 | | } |
| | | 988 | | |
| | | 989 | | private BaseItem? TranslateParentItem(BaseItem item, User user) |
| | | 990 | | { |
| | 0 | 991 | | return item.GetParent() is AggregateFolder |
| | 0 | 992 | | ? _libraryManager.GetUserRootFolder().GetChildren(user, true) |
| | 0 | 993 | | .FirstOrDefault(i => i.PhysicalLocations.Contains(item.Path)) |
| | 0 | 994 | | : item; |
| | | 995 | | } |
| | | 996 | | |
| | | 997 | | private async Task LogDownloadAsync(BaseItem item, User user) |
| | | 998 | | { |
| | | 999 | | try |
| | | 1000 | | { |
| | 0 | 1001 | | await _activityManager.CreateAsync(new ActivityLog( |
| | 0 | 1002 | | string.Format(CultureInfo.InvariantCulture, _localization.GetServerLocalizedString("UserDownloadingItemW |
| | 0 | 1003 | | "UserDownloadingContent", |
| | 0 | 1004 | | User.GetUserId()) |
| | 0 | 1005 | | { |
| | 0 | 1006 | | ShortOverview = string.Format(CultureInfo.InvariantCulture, _localization.GetServerLocalizedString("AppD |
| | 0 | 1007 | | ItemId = item.Id.ToString("N", CultureInfo.InvariantCulture) |
| | 0 | 1008 | | }).ConfigureAwait(false); |
| | 0 | 1009 | | } |
| | 0 | 1010 | | catch |
| | | 1011 | | { |
| | | 1012 | | // Logged at lower levels |
| | 0 | 1013 | | } |
| | 0 | 1014 | | } |
| | | 1015 | | |
| | | 1016 | | private static string[] GetRepresentativeItemTypes(CollectionType? contentType) |
| | | 1017 | | { |
| | 0 | 1018 | | return contentType switch |
| | 0 | 1019 | | { |
| | 0 | 1020 | | CollectionType.boxsets => new[] { "BoxSet" }, |
| | 0 | 1021 | | CollectionType.playlists => new[] { "Playlist" }, |
| | 0 | 1022 | | CollectionType.movies => new[] { "Movie" }, |
| | 0 | 1023 | | CollectionType.tvshows => new[] { "Series", "Season", "Episode" }, |
| | 0 | 1024 | | CollectionType.books => new[] { "Book", "AudioBook" }, |
| | 0 | 1025 | | CollectionType.music => new[] { "MusicArtist", "MusicAlbum", "Audio", "MusicVideo" }, |
| | 0 | 1026 | | CollectionType.homevideos => new[] { "Video", "Photo" }, |
| | 0 | 1027 | | CollectionType.photos => new[] { "Video", "Photo" }, |
| | 0 | 1028 | | CollectionType.musicvideos => new[] { "MusicVideo" }, |
| | 0 | 1029 | | _ => new[] { "Series", "Season", "Episode", "Movie" } |
| | 0 | 1030 | | }; |
| | | 1031 | | } |
| | | 1032 | | |
| | | 1033 | | private bool IsSaverEnabledByDefault(string name, string[] itemTypes, bool isNewLibrary) |
| | | 1034 | | { |
| | 0 | 1035 | | if (isNewLibrary) |
| | | 1036 | | { |
| | 0 | 1037 | | return false; |
| | | 1038 | | } |
| | | 1039 | | |
| | 0 | 1040 | | var metadataOptions = _serverConfigurationManager.Configuration.MetadataOptions |
| | 0 | 1041 | | .Where(i => itemTypes.Contains(i.ItemType ?? string.Empty, StringComparison.OrdinalIgnoreCase)) |
| | 0 | 1042 | | .ToArray(); |
| | | 1043 | | |
| | 0 | 1044 | | return metadataOptions.Length == 0 || metadataOptions.Any(i => !i.DisabledMetadataSavers.Contains(name, StringCo |
| | | 1045 | | } |
| | | 1046 | | |
| | | 1047 | | private bool IsMetadataFetcherEnabledByDefault(string name, string type, bool isNewLibrary) |
| | | 1048 | | { |
| | 0 | 1049 | | if (isNewLibrary) |
| | | 1050 | | { |
| | 0 | 1051 | | if (string.Equals(name, "TheMovieDb", StringComparison.OrdinalIgnoreCase)) |
| | | 1052 | | { |
| | 0 | 1053 | | return !(string.Equals(type, "Season", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1054 | | || string.Equals(type, "Episode", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1055 | | || string.Equals(type, "MusicVideo", StringComparison.OrdinalIgnoreCase)); |
| | | 1056 | | } |
| | | 1057 | | |
| | 0 | 1058 | | return string.Equals(name, "TheTVDB", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1059 | | || string.Equals(name, "TheAudioDB", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1060 | | || string.Equals(name, "MusicBrainz", StringComparison.OrdinalIgnoreCase); |
| | | 1061 | | } |
| | | 1062 | | |
| | 0 | 1063 | | var metadataOptions = _serverConfigurationManager.GetMetadataOptionsForType(type); |
| | 0 | 1064 | | return metadataOptions is null || !metadataOptions.DisabledMetadataFetchers.Contains(name, StringComparison.Ordi |
| | | 1065 | | } |
| | | 1066 | | |
| | | 1067 | | private bool IsImageFetcherEnabledByDefault(string name, string type, bool isNewLibrary) |
| | | 1068 | | { |
| | 0 | 1069 | | if (isNewLibrary) |
| | | 1070 | | { |
| | 0 | 1071 | | if (string.Equals(name, "TheMovieDb", StringComparison.OrdinalIgnoreCase)) |
| | | 1072 | | { |
| | 0 | 1073 | | return !string.Equals(type, "Series", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1074 | | && !string.Equals(type, "Season", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1075 | | && !string.Equals(type, "Episode", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1076 | | && !string.Equals(type, "MusicVideo", StringComparison.OrdinalIgnoreCase); |
| | | 1077 | | } |
| | | 1078 | | |
| | 0 | 1079 | | return string.Equals(name, "TheTVDB", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1080 | | || string.Equals(name, "Screen Grabber", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1081 | | || string.Equals(name, "TheAudioDB", StringComparison.OrdinalIgnoreCase) |
| | 0 | 1082 | | || string.Equals(name, "Image Extractor", StringComparison.OrdinalIgnoreCase); |
| | | 1083 | | } |
| | | 1084 | | |
| | 0 | 1085 | | var metadataOptions = _serverConfigurationManager.GetMetadataOptionsForType(type); |
| | 0 | 1086 | | return metadataOptions is null || !metadataOptions.DisabledImageFetchers.Contains(name, StringComparison.Ordinal |
| | | 1087 | | } |
| | | 1088 | | } |