123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- using MediaBrowser.Common.Configuration;
- using MediaBrowser.Controller.Library;
- using MediaBrowser.Controller.Sync;
- using MediaBrowser.Model.Sync;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Threading;
- using System.Threading.Tasks;
- namespace MediaBrowser.Server.Implementations.Sync.FolderSync
- {
- public class FolderSyncProvider : IServerSyncProvider
- {
- private readonly IApplicationPaths _appPaths;
- private readonly IUserManager _userManager;
- public FolderSyncProvider(IApplicationPaths appPaths, IUserManager userManager)
- {
- _appPaths = appPaths;
- _userManager = userManager;
- }
- public Task SendFile(string inputFile, string path, SyncTarget target, IProgress<double> progress, CancellationToken cancellationToken)
- {
- return Task.Run(() => File.Copy(inputFile, path, true), cancellationToken);
- }
- public Task DeleteFile(string path, SyncTarget target, CancellationToken cancellationToken)
- {
- return Task.Run(() => File.Delete(path), cancellationToken);
- }
- public Task<Stream> GetFile(string path, SyncTarget target, IProgress<double> progress, CancellationToken cancellationToken)
- {
- return Task.FromResult((Stream)File.OpenRead(path));
- }
- public string GetFullPath(IEnumerable<string> paths, SyncTarget target)
- {
- var account = GetSyncAccounts()
- .FirstOrDefault(i => string.Equals(i.Id, target.Id, StringComparison.OrdinalIgnoreCase));
- if (account == null)
- {
- throw new ArgumentException("Invalid SyncTarget supplied.");
- }
- var list = paths.ToList();
- list.Insert(0, account.Path);
- return Path.Combine(list.ToArray());
- }
- public string GetParentDirectoryPath(string path, SyncTarget target)
- {
- return Path.GetDirectoryName(path);
- }
- public Task<List<DeviceFileInfo>> GetFileSystemEntries(string path, SyncTarget target)
- {
- List<FileInfo> files;
- try
- {
- files = new DirectoryInfo(path).EnumerateFiles("*", SearchOption.TopDirectoryOnly).ToList();
- }
- catch (DirectoryNotFoundException)
- {
- files = new List<FileInfo>();
- }
- return Task.FromResult(files.Select(i => new DeviceFileInfo
- {
- Name = i.Name,
- Path = i.FullName
- }).ToList());
- }
- public ISyncDataProvider GetDataProvider()
- {
- // If single instances are needed, manage them here
- return new FolderSyncDataProvider();
- }
- public string Name
- {
- get { return "Folder Sync"; }
- }
- public IEnumerable<SyncTarget> GetSyncTargets(string userId)
- {
- return GetSyncAccounts()
- .Where(i => i.UserIds.Contains(userId, StringComparer.OrdinalIgnoreCase))
- .Select(GetSyncTarget);
- }
- public IEnumerable<SyncTarget> GetAllSyncTargets()
- {
- return GetSyncAccounts().Select(GetSyncTarget);
- }
- private SyncTarget GetSyncTarget(SyncAccount account)
- {
- return new SyncTarget
- {
- Id = account.Id,
- Name = account.Name
- };
- }
- private IEnumerable<SyncAccount> GetSyncAccounts()
- {
- // Dummy this up
- return _userManager
- .Users
- .Select(i => new SyncAccount
- {
- Id = i.Id.ToString("N"),
- UserIds = new List<string> { i.Id.ToString("N") },
- Path = Path.Combine(_appPaths.DataPath, "foldersync", i.Id.ToString("N")),
- Name = i.Name + "'s Folder Sync"
- });
- }
- // An internal class to manage all configured Folder Sync accounts for differnet users
- class SyncAccount
- {
- public string Id { get; set; }
- public string Name { get; set; }
- public string Path { get; set; }
- public List<string> UserIds { get; set; }
- public SyncAccount()
- {
- UserIds = new List<string>();
- }
- }
- }
- }
|