SyncService.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. using MediaBrowser.Controller.Dto;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Net;
  4. using MediaBrowser.Controller.Sync;
  5. using MediaBrowser.Model.Dto;
  6. using MediaBrowser.Model.Querying;
  7. using MediaBrowser.Model.Sync;
  8. using ServiceStack;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Threading.Tasks;
  13. namespace MediaBrowser.Api.Sync
  14. {
  15. [Route("/Sync/Jobs/{Id}", "DELETE", Summary = "Cancels a sync job.")]
  16. public class CancelSyncJob : IReturnVoid
  17. {
  18. [ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  19. public string Id { get; set; }
  20. }
  21. [Route("/Sync/Jobs/{Id}", "GET", Summary = "Gets a sync job.")]
  22. public class GetSyncJob : IReturn<SyncJob>
  23. {
  24. [ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  25. public string Id { get; set; }
  26. }
  27. [Route("/Sync/JobItems", "GET", Summary = "Gets sync job items.")]
  28. public class GetSyncJobItems : SyncJobItemQuery, IReturn<QueryResult<SyncJobItem>>
  29. {
  30. }
  31. [Route("/Sync/Jobs", "GET", Summary = "Gets sync jobs.")]
  32. public class GetSyncJobs : SyncJobQuery, IReturn<QueryResult<SyncJob>>
  33. {
  34. }
  35. [Route("/Sync/Jobs", "POST", Summary = "Gets sync jobs.")]
  36. public class CreateSyncJob : SyncJobRequest, IReturn<SyncJobCreationResult>
  37. {
  38. }
  39. [Route("/Sync/Targets", "GET", Summary = "Gets a list of available sync targets.")]
  40. public class GetSyncTargets : IReturn<List<SyncTarget>>
  41. {
  42. [ApiMember(Name = "UserId", Description = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  43. public string UserId { get; set; }
  44. }
  45. [Route("/Sync/Options", "GET", Summary = "Gets a list of available sync targets.")]
  46. public class GetSyncDialogOptions : IReturn<SyncDialogOptions>
  47. {
  48. [ApiMember(Name = "UserId", Description = "UserId", IsRequired = true, DataType = "string", ParameterType = "query", Verb = "GET")]
  49. public string UserId { get; set; }
  50. [ApiMember(Name = "ItemIds", Description = "ItemIds", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  51. public string ItemIds { get; set; }
  52. [ApiMember(Name = "ParentId", Description = "ParentId", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  53. public string ParentId { get; set; }
  54. [ApiMember(Name = "Category", Description = "Category", IsRequired = false, DataType = "string", ParameterType = "query", Verb = "GET")]
  55. public SyncCategory? Category { get; set; }
  56. }
  57. [Route("/Sync/JobItems/{Id}/Transferred", "POST", Summary = "Reports that a sync job item has successfully been transferred.")]
  58. public class ReportSyncJobItemTransferred : IReturnVoid
  59. {
  60. [ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
  61. public string Id { get; set; }
  62. }
  63. [Route("/Sync/JobItems/{Id}/File", "GET", Summary = "Gets a sync job item file")]
  64. public class GetSyncJobItemFile
  65. {
  66. [ApiMember(Name = "Id", Description = "Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
  67. public string Id { get; set; }
  68. }
  69. [Authenticated]
  70. public class SyncService : BaseApiService
  71. {
  72. private readonly ISyncManager _syncManager;
  73. private readonly IDtoService _dtoService;
  74. private readonly ILibraryManager _libraryManager;
  75. public SyncService(ISyncManager syncManager, IDtoService dtoService, ILibraryManager libraryManager)
  76. {
  77. _syncManager = syncManager;
  78. _dtoService = dtoService;
  79. _libraryManager = libraryManager;
  80. }
  81. public object Get(GetSyncTargets request)
  82. {
  83. var result = _syncManager.GetSyncTargets(request.UserId);
  84. return ToOptimizedResult(result);
  85. }
  86. public object Get(GetSyncJobs request)
  87. {
  88. var result = _syncManager.GetJobs(request);
  89. return ToOptimizedResult(result);
  90. }
  91. public object Get(GetSyncJobItems request)
  92. {
  93. var result = _syncManager.GetJobItems(request);
  94. return ToOptimizedResult(result);
  95. }
  96. public object Get(GetSyncJob request)
  97. {
  98. var result = _syncManager.GetJob(request.Id);
  99. return ToOptimizedResult(result);
  100. }
  101. public void Delete(CancelSyncJob request)
  102. {
  103. var task = _syncManager.CancelJob(request.Id);
  104. Task.WaitAll(task);
  105. }
  106. public async Task<object> Post(CreateSyncJob request)
  107. {
  108. var result = await _syncManager.CreateJob(request).ConfigureAwait(false);
  109. return ToOptimizedResult(result);
  110. }
  111. public void Post(ReportSyncJobItemTransferred request)
  112. {
  113. var task = _syncManager.ReportSyncJobItemTransferred(request.Id);
  114. Task.WaitAll(task);
  115. }
  116. public object Get(GetSyncJobItemFile request)
  117. {
  118. var jobItem = _syncManager.GetJobItem(request.Id);
  119. if (jobItem.Status != SyncJobItemStatus.Transferring)
  120. {
  121. throw new ArgumentException("The job item is not yet ready for transfer.");
  122. }
  123. return ToStaticFileResult(jobItem.OutputPath);
  124. }
  125. public object Get(GetSyncDialogOptions request)
  126. {
  127. var result = new SyncDialogOptions();
  128. result.Targets = _syncManager.GetSyncTargets(request.UserId)
  129. .ToList();
  130. if (request.Category.HasValue)
  131. {
  132. result.Options = SyncHelper.GetSyncOptions(request.Category.Value);
  133. }
  134. else
  135. {
  136. var dtos = request.ItemIds.Split(',')
  137. .Select(_libraryManager.GetItemById)
  138. .Where(i => i != null)
  139. .Select(i => _dtoService.GetBaseItemDto(i, new DtoOptions
  140. {
  141. Fields = new List<ItemFields>
  142. {
  143. ItemFields.SyncInfo
  144. }
  145. }))
  146. .ToList();
  147. result.Options = SyncHelper.GetSyncOptions(dtos);
  148. }
  149. return ToOptimizedResult(result);
  150. }
  151. }
  152. }