FolderSyncProvider.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Controller.Library;
  3. using MediaBrowser.Controller.Sync;
  4. using MediaBrowser.Model.Sync;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Server.Implementations.Sync.FolderSync
  12. {
  13. public class FolderSyncProvider : IServerSyncProvider
  14. {
  15. private readonly IApplicationPaths _appPaths;
  16. private readonly IUserManager _userManager;
  17. public FolderSyncProvider(IApplicationPaths appPaths, IUserManager userManager)
  18. {
  19. _appPaths = appPaths;
  20. _userManager = userManager;
  21. }
  22. public Task SendFile(string inputFile, string path, SyncTarget target, IProgress<double> progress, CancellationToken cancellationToken)
  23. {
  24. return Task.Run(() => File.Copy(inputFile, path, true), cancellationToken);
  25. }
  26. public Task DeleteFile(string path, SyncTarget target, CancellationToken cancellationToken)
  27. {
  28. return Task.Run(() => File.Delete(path), cancellationToken);
  29. }
  30. public Task<Stream> GetFile(string path, SyncTarget target, IProgress<double> progress, CancellationToken cancellationToken)
  31. {
  32. return Task.FromResult((Stream)File.OpenRead(path));
  33. }
  34. public string GetFullPath(IEnumerable<string> paths, SyncTarget target)
  35. {
  36. var account = GetSyncAccounts()
  37. .FirstOrDefault(i => string.Equals(i.Id, target.Id, StringComparison.OrdinalIgnoreCase));
  38. if (account == null)
  39. {
  40. throw new ArgumentException("Invalid SyncTarget supplied.");
  41. }
  42. var list = paths.ToList();
  43. list.Insert(0, account.Path);
  44. return Path.Combine(list.ToArray());
  45. }
  46. public string GetParentDirectoryPath(string path, SyncTarget target)
  47. {
  48. return Path.GetDirectoryName(path);
  49. }
  50. public Task<List<DeviceFileInfo>> GetFileSystemEntries(string path, SyncTarget target)
  51. {
  52. List<FileInfo> files;
  53. try
  54. {
  55. files = new DirectoryInfo(path).EnumerateFiles("*", SearchOption.TopDirectoryOnly).ToList();
  56. }
  57. catch (DirectoryNotFoundException)
  58. {
  59. files = new List<FileInfo>();
  60. }
  61. return Task.FromResult(files.Select(i => new DeviceFileInfo
  62. {
  63. Name = i.Name,
  64. Path = i.FullName
  65. }).ToList());
  66. }
  67. public ISyncDataProvider GetDataProvider()
  68. {
  69. // If single instances are needed, manage them here
  70. return new FolderSyncDataProvider();
  71. }
  72. public string Name
  73. {
  74. get { return "Folder Sync"; }
  75. }
  76. public IEnumerable<SyncTarget> GetSyncTargets(string userId)
  77. {
  78. return GetSyncAccounts()
  79. .Where(i => i.UserIds.Contains(userId, StringComparer.OrdinalIgnoreCase))
  80. .Select(GetSyncTarget);
  81. }
  82. public IEnumerable<SyncTarget> GetAllSyncTargets()
  83. {
  84. return GetSyncAccounts().Select(GetSyncTarget);
  85. }
  86. private SyncTarget GetSyncTarget(SyncAccount account)
  87. {
  88. return new SyncTarget
  89. {
  90. Id = account.Id,
  91. Name = account.Name
  92. };
  93. }
  94. private IEnumerable<SyncAccount> GetSyncAccounts()
  95. {
  96. // Dummy this up
  97. return _userManager
  98. .Users
  99. .Select(i => new SyncAccount
  100. {
  101. Id = i.Id.ToString("N"),
  102. UserIds = new List<string> { i.Id.ToString("N") },
  103. Path = Path.Combine(_appPaths.DataPath, "foldersync", i.Id.ToString("N")),
  104. Name = i.Name + "'s Folder Sync"
  105. });
  106. }
  107. // An internal class to manage all configured Folder Sync accounts for differnet users
  108. class SyncAccount
  109. {
  110. public string Id { get; set; }
  111. public string Name { get; set; }
  112. public string Path { get; set; }
  113. public List<string> UserIds { get; set; }
  114. public SyncAccount()
  115. {
  116. UserIds = new List<string>();
  117. }
  118. }
  119. }
  120. }