FileOrganizationService.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System.Collections.Generic;
  2. using MediaBrowser.Controller.FileOrganization;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Model.FileOrganization;
  5. using MediaBrowser.Model.Querying;
  6. using ServiceStack;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Model.Dto;
  9. using MediaBrowser.Model.Serialization;
  10. namespace MediaBrowser.Api.Library
  11. {
  12. [Route("/Library/FileOrganization", "GET", Summary = "Gets file organization results")]
  13. public class GetFileOrganizationActivity : IReturn<QueryResult<FileOrganizationResult>>
  14. {
  15. /// <summary>
  16. /// Skips over a given number of items within the results. Use for paging.
  17. /// </summary>
  18. /// <value>The start index.</value>
  19. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  20. public int? StartIndex { get; set; }
  21. /// <summary>
  22. /// The maximum number of items to return
  23. /// </summary>
  24. /// <value>The limit.</value>
  25. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  26. public int? Limit { get; set; }
  27. }
  28. [Route("/Library/FileOrganizations", "DELETE", Summary = "Clears the activity log")]
  29. public class ClearOrganizationLog : IReturnVoid
  30. {
  31. }
  32. [Route("/Library/FileOrganizations/{Id}/File", "DELETE", Summary = "Deletes the original file of a organizer result")]
  33. public class DeleteOriginalFile : IReturnVoid
  34. {
  35. /// <summary>
  36. /// Gets or sets the id.
  37. /// </summary>
  38. /// <value>The id.</value>
  39. [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  40. public string Id { get; set; }
  41. }
  42. [Route("/Library/FileOrganizations/{Id}/Organize", "POST", Summary = "Performs an organization")]
  43. public class PerformOrganization : IReturn<QueryResult<FileOrganizationResult>>
  44. {
  45. /// <summary>
  46. /// Gets or sets the id.
  47. /// </summary>
  48. /// <value>The id.</value>
  49. [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  50. public string Id { get; set; }
  51. }
  52. [Route("/Library/FileOrganizations/{Id}/Episode/Organize", "POST", Summary = "Performs organization of a tv episode")]
  53. public class OrganizeEpisode
  54. {
  55. [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  56. public string Id { get; set; }
  57. [ApiMember(Name = "SeriesId", Description = "Series Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  58. public string SeriesId { get; set; }
  59. [ApiMember(Name = "SeasonNumber", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "POST")]
  60. public int SeasonNumber { get; set; }
  61. [ApiMember(Name = "EpisodeNumber", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "POST")]
  62. public int EpisodeNumber { get; set; }
  63. [ApiMember(Name = "EndingEpisodeNumber", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  64. public int? EndingEpisodeNumber { get; set; }
  65. [ApiMember(Name = "RememberCorrection", Description = "Whether or not to apply the same correction to future episodes of the same series.", IsRequired = false, DataType = "bool", ParameterType = "query", Verb = "POST")]
  66. public bool RememberCorrection { get; set; }
  67. [ApiMember(Name = "NewSeriesProviderIds", Description = "A list of provider IDs identifying a new series.", IsRequired = false, DataType = "Dictionary<string, string>", ParameterType = "query", Verb = "POST")]
  68. public Dictionary<string, string> NewSeriesProviderIds { get; set; }
  69. [ApiMember(Name = "NewSeriesName", Description = "Name of a series to add.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  70. public string NewSeriesName { get; set; }
  71. [ApiMember(Name = "NewSeriesYear", Description = "Year of a series to add.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  72. public string NewSeriesYear { get; set; }
  73. [ApiMember(Name = "TargetFolder", Description = "Target Folder", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  74. public string TargetFolder { get; set; }
  75. }
  76. [Route("/Library/FileOrganizations/SmartMatches", "GET", Summary = "Gets smart match entries")]
  77. public class GetSmartMatchInfos : IReturn<QueryResult<SmartMatchInfo>>
  78. {
  79. /// <summary>
  80. /// Skips over a given number of items within the results. Use for paging.
  81. /// </summary>
  82. /// <value>The start index.</value>
  83. [ApiMember(Name = "StartIndex", Description = "Optional. The record index to start at. All items with a lower index will be dropped from the results.", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  84. public int? StartIndex { get; set; }
  85. /// <summary>
  86. /// The maximum number of items to return
  87. /// </summary>
  88. /// <value>The limit.</value>
  89. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  90. public int? Limit { get; set; }
  91. }
  92. [Route("/Library/FileOrganizations/SmartMatches/Delete", "POST", Summary = "Deletes a smart match entry")]
  93. public class DeleteSmartMatchEntry
  94. {
  95. [ApiMember(Name = "Entries", Description = "SmartMatch Entry", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  96. public List<NameValuePair> Entries { get; set; }
  97. }
  98. [Authenticated(Roles = "Admin")]
  99. public class FileOrganizationService : BaseApiService
  100. {
  101. private readonly IFileOrganizationService _iFileOrganizationService;
  102. private readonly IJsonSerializer _jsonSerializer;
  103. public FileOrganizationService(IFileOrganizationService iFileOrganizationService, IJsonSerializer jsonSerializer)
  104. {
  105. _iFileOrganizationService = iFileOrganizationService;
  106. _jsonSerializer = jsonSerializer;
  107. }
  108. public object Get(GetFileOrganizationActivity request)
  109. {
  110. var result = _iFileOrganizationService.GetResults(new FileOrganizationResultQuery
  111. {
  112. Limit = request.Limit,
  113. StartIndex = request.StartIndex
  114. });
  115. return ToOptimizedSerializedResultUsingCache(result);
  116. }
  117. public void Delete(DeleteOriginalFile request)
  118. {
  119. var task = _iFileOrganizationService.DeleteOriginalFile(request.Id);
  120. Task.WaitAll(task);
  121. }
  122. public void Delete(ClearOrganizationLog request)
  123. {
  124. var task = _iFileOrganizationService.ClearLog();
  125. Task.WaitAll(task);
  126. }
  127. public void Post(PerformOrganization request)
  128. {
  129. // Don't await this
  130. var task = _iFileOrganizationService.PerformOrganization(request.Id);
  131. // Async processing (close dialog early instead of waiting until the file has been copied)
  132. // Wait 2s for exceptions that may occur to have them forwarded to the client for immediate error display
  133. task.Wait(2000);
  134. }
  135. public void Post(OrganizeEpisode request)
  136. {
  137. var dicNewProviderIds = new Dictionary<string, string>();
  138. if (request.NewSeriesProviderIds != null)
  139. {
  140. dicNewProviderIds = request.NewSeriesProviderIds;
  141. }
  142. // Don't await this
  143. var task = _iFileOrganizationService.PerformEpisodeOrganization(new EpisodeFileOrganizationRequest
  144. {
  145. EndingEpisodeNumber = request.EndingEpisodeNumber,
  146. EpisodeNumber = request.EpisodeNumber,
  147. RememberCorrection = request.RememberCorrection,
  148. ResultId = request.Id,
  149. SeasonNumber = request.SeasonNumber,
  150. SeriesId = request.SeriesId,
  151. NewSeriesName = request.NewSeriesName,
  152. NewSeriesYear = request.NewSeriesYear,
  153. NewSeriesProviderIds = dicNewProviderIds,
  154. TargetFolder = request.TargetFolder
  155. });
  156. // Async processing (close dialog early instead of waiting until the file has been copied)
  157. // Wait 2s for exceptions that may occur to have them forwarded to the client for immediate error display
  158. task.Wait(2000);
  159. }
  160. public object Get(GetSmartMatchInfos request)
  161. {
  162. var result = _iFileOrganizationService.GetSmartMatchInfos(new FileOrganizationResultQuery
  163. {
  164. Limit = request.Limit,
  165. StartIndex = request.StartIndex
  166. });
  167. return ToOptimizedSerializedResultUsingCache(result);
  168. }
  169. public void Post(DeleteSmartMatchEntry request)
  170. {
  171. request.Entries.ForEach(entry =>
  172. {
  173. _iFileOrganizationService.DeleteSmartMatchEntry(entry.Name, entry.Value);
  174. });
  175. }
  176. }
  177. }