aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Server.Implementations/Sync/FolderSync/FolderSyncProvider.cs
blob: 9cf23410648dd4a20bf377595b7e7139812a7a3a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
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>();
            }
        }
    }
}