Welcome to mirror list, hosted at ThFree Co, Russian Federation.

IDriveBackend.cs « IDrive « Backend « Library « Duplicati - github.com/duplicati/duplicati.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ba856128940361eb483e62cee768e2499d5c18b9 (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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//  Copyright (C) 2015, The Duplicati Team
//  http://www.duplicati.com, info@duplicati.com
//
//  This library is free software; you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation; either version 2.1 of the
//  License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful, but
//  WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using Duplicati.Library.Common.IO;
using Duplicati.Library.Interface;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace Duplicati.Library.Backend.IDrive
{
    // ReSharper disable once UnusedMember.Global
    // This class is instantiated dynamically in the BackendLoader.
    public class IDriveBackend : IBackend, IStreamingBackend
    {
        private readonly string _username = null;
        private readonly string _password = null;
        private readonly string _baseDirectoryPath = null;
        public string DisplayName => Strings.IDriveBackend.DisplayName;
        public string Description => Strings.IDriveBackend.Description;
        public string[] DNSName => null;
        public string ProtocolKey => "idrive";
        CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();

        public IList<ICommandLineArgument> SupportedCommands
        {
            get
            {
                return new List<ICommandLineArgument>(new ICommandLineArgument[] {
                    new CommandLineArgument("auth-password", CommandLineArgument.ArgumentType.Password, Strings.IDriveBackend.AuthPasswordDescriptionShort, Strings.IDriveBackend.AuthPasswordDescriptionLong),
                    new CommandLineArgument("auth-username", CommandLineArgument.ArgumentType.String, Strings.IDriveBackend.AuthUsernameDescriptionShort, Strings.IDriveBackend.AuthUsernameDescriptionLong)
                });
            }
        }

        private Dictionary<string, FileEntry> _fileCache;
        protected Dictionary<string, FileEntry> FileCache
        {
            get
            {
                if (_fileCache == null)
                    ResetFileCacheAsync().Wait();

                return _fileCache;
            }
            set
            {
                _fileCache = value;
            }
        }

        private IDriveApiClient _client;
        protected IDriveApiClient Client
        {
            get
            {
                if (_client == null)
                {
                    var cl = new IDriveApiClient();
                    cl.LoginAsync(_username, _password).Wait();
                    _client = cl;
                }

                return _client;
            }
        }

        public IDriveBackend()
        {
        }

        public IDriveBackend(string url, Dictionary<string, string> options)
        {
            var uri = new Utility.Uri(url); // Sample url: idrive://Directory1/SubDirectory1?auth-username=MyUsername&auth-password=MyPassword
            _username = uri.Username;
            _password = uri.Password;

            if (string.IsNullOrEmpty(_username) && options.TryGetValue("auth-username", out string username))
                _username = username;
            if (string.IsNullOrEmpty(_password) && options.TryGetValue("auth-password", out string password))
                _password = password;

            if (string.IsNullOrEmpty(_username))
                throw new UserInformationException(Strings.IDriveBackend.NoUsernameError, "IDriveNoUsername");
            if (string.IsNullOrEmpty(_password))
                throw new UserInformationException(Strings.IDriveBackend.NoPasswordError, "IDriveNoPassword");

            _baseDirectoryPath = ("/" + (uri.HostAndPath ?? "").Trim('/') + "/").Replace("//", "/");
        }

        private async Task ResetFileCacheAsync()
        {
            FileCache = (await Client.GetFileEntryListAsync(_baseDirectoryPath))
                .Where(x => !x.IsFolder)
                .ToDictionary(x => x.Name, x => x);
        }

        public IEnumerable<IFileEntry> List()
        {
            return FileCache.Values;
        }

        public void Get(string filename, string localFilePath)
        {
            using (var fileStream = File.Create(localFilePath))
                Get(filename, fileStream);
        }

        public void Get(string filename, Stream stream)
        {
            Client.DownloadAsync(Path.Combine(_baseDirectoryPath, filename), stream).Wait();
        }

        public async Task PutAsync(string filename, string localFilePath, CancellationToken cancellationToken)
        {
            using (var fileStream = File.OpenRead(localFilePath))
                await PutAsync(filename, fileStream, cancellationToken);
        }

        public async Task PutAsync(string filename, Stream stream, CancellationToken cancellationToken)
        {
            try
            {
                var fileEntry = await Client.UploadAsync(stream, filename, _baseDirectoryPath, cancellationToken);
                FileCache[filename] = fileEntry;
            }
            catch
            {
                FileCache = null;
                throw;
            }
        }

        public void Delete(string filename)
        {
            try
            {
                if (!FileCache.ContainsKey(filename))
                {
                    ResetFileCacheAsync().Wait();

                    if (!FileCache.ContainsKey(filename))
                        throw new FileMissingException();
                }

                Client.DeleteAsync(Path.Combine(_baseDirectoryPath, filename), _cancellationTokenSource.Token, false).Wait();

                FileCache.Remove(filename);
            }
            catch
            {
                FileCache = null;
                throw;
            }
        }

        public void CreateFolder()
        {
            var directoryParts = _baseDirectoryPath.Split('/').Where(d => !string.IsNullOrEmpty(d));
            string baseDirectory = "/";

            foreach (string directoryPart in directoryParts)
            {
                Client.CreateDirectoryAsync(directoryPart, baseDirectory, _cancellationTokenSource.Token).Wait();
                baseDirectory += directoryPart + "/";
            }
        }

        public void Test()
        {
            this.TestList();
        }

        #region IDisposable Support
        private bool _disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposedValue)
            {
                if (disposing)
                {
                }

                _disposedValue = true;
            }
        }

        // This code added to correctly implement the disposable pattern.
        public void Dispose()
        {
            // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
            Dispose(true);
        }
        #endregion
    }
}