FileOrganizationService.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. namespace MediaBrowser.Api.Library
  10. {
  11. [Route("/Library/FileOrganization", "GET", Summary = "Gets file organization results")]
  12. public class GetFileOrganizationActivity : IReturn<QueryResult<FileOrganizationResult>>
  13. {
  14. /// <summary>
  15. /// Skips over a given number of items within the results. Use for paging.
  16. /// </summary>
  17. /// <value>The start index.</value>
  18. [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")]
  19. public int? StartIndex { get; set; }
  20. /// <summary>
  21. /// The maximum number of items to return
  22. /// </summary>
  23. /// <value>The limit.</value>
  24. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  25. public int? Limit { get; set; }
  26. }
  27. [Route("/Library/FileOrganizations", "DELETE", Summary = "Clears the activity log")]
  28. public class ClearOrganizationLog : IReturnVoid
  29. {
  30. }
  31. [Route("/Library/FileOrganizations/{Id}/File", "DELETE", Summary = "Deletes the original file of a organizer result")]
  32. public class DeleteOriginalFile : IReturnVoid
  33. {
  34. /// <summary>
  35. /// Gets or sets the id.
  36. /// </summary>
  37. /// <value>The id.</value>
  38. [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "DELETE")]
  39. public string Id { get; set; }
  40. }
  41. [Route("/Library/FileOrganizations/{Id}/Organize", "POST", Summary = "Performs an organization")]
  42. public class PerformOrganization : IReturn<QueryResult<FileOrganizationResult>>
  43. {
  44. /// <summary>
  45. /// Gets or sets the id.
  46. /// </summary>
  47. /// <value>The id.</value>
  48. [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  49. public string Id { get; set; }
  50. }
  51. [Route("/Library/FileOrganizations/{Id}/Episode/Organize", "POST", Summary = "Performs organization of a tv episode")]
  52. public class OrganizeEpisode
  53. {
  54. [ApiMember(Name = "Id", Description = "Result Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  55. public string Id { get; set; }
  56. [ApiMember(Name = "SeriesId", Description = "Series Id", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  57. public string SeriesId { get; set; }
  58. [ApiMember(Name = "SeasonNumber", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "POST")]
  59. public int SeasonNumber { get; set; }
  60. [ApiMember(Name = "EpisodeNumber", IsRequired = true, DataType = "int", ParameterType = "query", Verb = "POST")]
  61. public int EpisodeNumber { get; set; }
  62. [ApiMember(Name = "EndingEpisodeNumber", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "POST")]
  63. public int? EndingEpisodeNumber { get; set; }
  64. [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")]
  65. public bool RememberCorrection { get; set; }
  66. [ApiMember(Name = "NewSeriesProviderIds", Description = "A list of provider IDs identifying a new series.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  67. public string NewSeriesProviderIds { get; set; }
  68. [ApiMember(Name = "NewSeriesName", Description = "Name of a series to add.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  69. public string NewSeriesName { get; set; }
  70. [ApiMember(Name = "NewSeriesYear", Description = "Year of a series to add.", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  71. public string NewSeriesYear { get; set; }
  72. [ApiMember(Name = "TargetFolder", Description = "Target Folder", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "POST")]
  73. public string TargetFolder { get; set; }
  74. }
  75. [Route("/Library/FileOrganizations/SmartMatches", "GET", Summary = "Gets smart match entries")]
  76. public class GetSmartMatchInfos : IReturn<QueryResult<SmartMatchInfo>>
  77. {
  78. /// <summary>
  79. /// Skips over a given number of items within the results. Use for paging.
  80. /// </summary>
  81. /// <value>The start index.</value>
  82. [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")]
  83. public int? StartIndex { get; set; }
  84. /// <summary>
  85. /// The maximum number of items to return
  86. /// </summary>
  87. /// <value>The limit.</value>
  88. [ApiMember(Name = "Limit", Description = "Optional. The maximum number of records to return", IsRequired = false, DataType = "int", ParameterType = "query", Verb = "GET")]
  89. public int? Limit { get; set; }
  90. }
  91. [Route("/Library/FileOrganizations/SmartMatches/Delete", "POST", Summary = "Deletes a smart match entry")]
  92. public class DeleteSmartMatchEntry
  93. {
  94. [ApiMember(Name = "Entries", Description = "SmartMatch Entry", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "POST")]
  95. public List<NameValuePair> Entries { get; set; }
  96. }
  97. [Authenticated(Roles = "Admin")]
  98. public class FileOrganizationService : BaseApiService
  99. {
  100. private readonly IFileOrganizationService _iFileOrganizationService;
  101. public FileOrganizationService(IFileOrganizationService iFileOrganizationService)
  102. {
  103. _iFileOrganizationService = iFileOrganizationService;
  104. }
  105. public object Get(GetFileOrganizationActivity request)
  106. {
  107. var result = _iFileOrganizationService.GetResults(new FileOrganizationResultQuery
  108. {
  109. Limit = request.Limit,
  110. StartIndex = request.StartIndex
  111. });
  112. return ToOptimizedSerializedResultUsingCache(result);
  113. }
  114. public void Delete(DeleteOriginalFile request)
  115. {
  116. var task = _iFileOrganizationService.DeleteOriginalFile(request.Id);
  117. Task.WaitAll(task);
  118. }
  119. public void Delete(ClearOrganizationLog request)
  120. {
  121. var task = _iFileOrganizationService.ClearLog();
  122. Task.WaitAll(task);
  123. }
  124. public void Post(PerformOrganization request)
  125. {
  126. var task = _iFileOrganizationService.PerformOrganization(request.Id);
  127. Task.WaitAll(task);
  128. }
  129. public void Post(OrganizeEpisode request)
  130. {
  131. var task = _iFileOrganizationService.PerformEpisodeOrganization(new EpisodeFileOrganizationRequest
  132. {
  133. EndingEpisodeNumber = request.EndingEpisodeNumber,
  134. EpisodeNumber = request.EpisodeNumber,
  135. RememberCorrection = request.RememberCorrection,
  136. ResultId = request.Id,
  137. SeasonNumber = request.SeasonNumber,
  138. SeriesId = request.SeriesId,
  139. NewSeriesName = request.NewSeriesName,
  140. NewSeriesYear = request.NewSeriesYear,
  141. NewSeriesProviderIds = request.NewSeriesProviderIds,
  142. TargetFolder = request.TargetFolder
  143. });
  144. // For async processing (close dialog early instead of waiting until the file has been copied)
  145. //var tasks = new Task[] { task };
  146. //Task.WaitAll(tasks, 8000);
  147. Task.WaitAll(task);
  148. }
  149. public object Get(GetSmartMatchInfos request)
  150. {
  151. var result = _iFileOrganizationService.GetSmartMatchInfos(new FileOrganizationResultQuery
  152. {
  153. Limit = request.Limit,
  154. StartIndex = request.StartIndex
  155. });
  156. return ToOptimizedSerializedResultUsingCache(result);
  157. }
  158. public void Post(DeleteSmartMatchEntry request)
  159. {
  160. request.Entries.ForEach(entry =>
  161. {
  162. _iFileOrganizationService.DeleteSmartMatchEntry(entry.Name, entry.Value);
  163. });
  164. }
  165. }
  166. }