2025-01-07 09:40:39 +01:00
|
|
|
// Copyright (C) 2025, The Duplicati Team
|
|
|
|
|
// https://duplicati.com, hello@duplicati.com
|
|
|
|
|
//
|
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
|
//
|
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
|
//
|
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
2024-09-29 22:00:57 +02:00
|
|
|
// DEALINGS IN THE SOFTWARE.
|
2024-02-28 15:45:30 +01:00
|
|
|
using Duplicati.Library.Common.IO;
|
2019-02-23 09:14:54 -06:00
|
|
|
using Duplicati.Library.Interface;
|
|
|
|
|
using System;
|
2016-07-07 13:44:19 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
2025-02-17 16:45:51 +01:00
|
|
|
using System.Runtime.CompilerServices;
|
2019-02-22 21:58:40 -06:00
|
|
|
using System.Threading;
|
2019-02-23 09:14:54 -06:00
|
|
|
using System.Threading.Tasks;
|
2019-02-22 21:58:40 -06:00
|
|
|
|
2016-07-07 13:44:19 +02:00
|
|
|
namespace Duplicati.Library.Backend
|
|
|
|
|
{
|
2019-04-28 16:41:56 -07:00
|
|
|
// ReSharper disable once UnusedMember.Global
|
|
|
|
|
// This class is instantiated dynamically in the BackendLoader.
|
2016-07-07 13:44:19 +02:00
|
|
|
public class Dropbox : IBackend, IStreamingBackend
|
|
|
|
|
{
|
|
|
|
|
private const string AUTHID_OPTION = "authid";
|
|
|
|
|
|
2018-05-23 21:18:01 -07:00
|
|
|
private readonly string m_accesToken;
|
|
|
|
|
private readonly string m_path;
|
|
|
|
|
private readonly DropboxHelper dbx;
|
2016-07-07 13:44:19 +02:00
|
|
|
|
2019-04-28 16:42:31 -07:00
|
|
|
// ReSharper disable once UnusedMember.Global
|
|
|
|
|
// This constructor is needed by the BackendLoader.
|
2016-07-07 13:44:19 +02:00
|
|
|
public Dropbox()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2019-04-28 16:42:31 -07:00
|
|
|
// ReSharper disable once UnusedMember.Global
|
|
|
|
|
// This constructor is needed by the BackendLoader.
|
2016-07-07 13:44:19 +02:00
|
|
|
public Dropbox(string url, Dictionary<string, string> options)
|
|
|
|
|
{
|
|
|
|
|
var uri = new Utility.Uri(url);
|
|
|
|
|
|
|
|
|
|
m_path = Library.Utility.Uri.UrlDecode(uri.HostAndPath);
|
2017-11-26 10:53:14 -08:00
|
|
|
if (m_path.Length != 0 && !m_path.StartsWith("/", StringComparison.Ordinal))
|
2016-07-07 13:44:19 +02:00
|
|
|
m_path = "/" + m_path;
|
|
|
|
|
|
2017-11-26 10:53:14 -08:00
|
|
|
if (m_path.EndsWith("/", StringComparison.Ordinal))
|
2016-07-07 13:44:19 +02:00
|
|
|
m_path = m_path.Substring(0, m_path.Length - 1);
|
|
|
|
|
|
|
|
|
|
if (options.ContainsKey(AUTHID_OPTION))
|
|
|
|
|
m_accesToken = options[AUTHID_OPTION];
|
|
|
|
|
|
|
|
|
|
dbx = new DropboxHelper(m_accesToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
// do nothing
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string DisplayName
|
|
|
|
|
{
|
2016-09-15 11:39:27 +02:00
|
|
|
get { return Strings.Dropbox.DisplayName; }
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string ProtocolKey
|
|
|
|
|
{
|
|
|
|
|
get { return "dropbox"; }
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-01 17:56:48 +01:00
|
|
|
private IFileEntry ParseEntry(MetaData md)
|
2016-09-15 11:39:27 +02:00
|
|
|
{
|
|
|
|
|
var ife = new FileEntry(md.name);
|
|
|
|
|
if (md.IsFile)
|
|
|
|
|
{
|
|
|
|
|
ife.IsFolder = false;
|
|
|
|
|
ife.Size = (long)md.size;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ife.IsFolder = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try { ife.LastModification = ife.LastAccess = DateTime.Parse(md.server_modified).ToUniversalTime(); }
|
|
|
|
|
catch { }
|
|
|
|
|
|
|
|
|
|
return ife;
|
|
|
|
|
}
|
2017-09-25 23:19:31 -06:00
|
|
|
|
|
|
|
|
private T HandleListExceptions<T>(Func<T> func)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2017-09-25 23:19:31 -06:00
|
|
|
return func();
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
catch (DropboxException de)
|
|
|
|
|
{
|
|
|
|
|
if (de.errorJSON["error"][".tag"].ToString() == "path" && de.errorJSON["error"]["path"][".tag"].ToString() == "not_found")
|
|
|
|
|
throw new FolderMissingException();
|
2017-09-25 23:17:45 -06:00
|
|
|
|
2016-07-07 13:44:19 +02:00
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-17 16:45:51 +01:00
|
|
|
private async Task<T> HandleListExceptions<T>(Func<Task<T>> func)
|
2017-09-25 23:17:45 -06:00
|
|
|
{
|
2025-02-17 16:45:51 +01:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
return await func().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (DropboxException de)
|
|
|
|
|
{
|
|
|
|
|
if (de.errorJSON["error"][".tag"].ToString() == "path" && de.errorJSON["error"]["path"][".tag"].ToString() == "not_found")
|
|
|
|
|
throw new FolderMissingException();
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async IAsyncEnumerable<IFileEntry> ListAsync([EnumeratorCancellation] CancellationToken cancelToken)
|
|
|
|
|
{
|
|
|
|
|
var lfr = await HandleListExceptions(() => dbx.ListFiles(m_path, cancelToken)).ConfigureAwait(false);
|
2024-09-29 22:00:57 +02:00
|
|
|
|
2017-09-25 23:17:45 -06:00
|
|
|
foreach (var md in lfr.entries)
|
|
|
|
|
yield return ParseEntry(md);
|
|
|
|
|
|
|
|
|
|
while (lfr.has_more)
|
|
|
|
|
{
|
2025-02-17 16:45:51 +01:00
|
|
|
lfr = await HandleListExceptions(() => dbx.ListFilesContinue(lfr.cursor, cancelToken)).ConfigureAwait(false);
|
2017-09-25 23:17:45 -06:00
|
|
|
foreach (var md in lfr.entries)
|
|
|
|
|
yield return ParseEntry(md);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-12 11:30:08 -07:00
|
|
|
public async Task PutAsync(string remotename, string filename, CancellationToken cancelToken)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
2024-09-29 22:00:57 +02:00
|
|
|
using (var fs = File.OpenRead(filename))
|
|
|
|
|
await PutAsync(remotename, fs, cancelToken).ConfigureAwait(false);
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-29 22:00:57 +02:00
|
|
|
public async Task GetAsync(string remotename, string filename, CancellationToken cancelToken)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
2024-09-29 22:00:57 +02:00
|
|
|
using (var fs = File.Create(filename))
|
|
|
|
|
await GetAsync(remotename, fs, cancelToken).ConfigureAwait(false);
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
|
2024-09-29 23:04:54 +02:00
|
|
|
public async Task DeleteAsync(string remotename, CancellationToken cancelToken)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string path = String.Format("{0}/{1}", m_path, remotename);
|
2024-09-29 23:04:54 +02:00
|
|
|
await dbx.DeleteAsync(path, cancelToken).ConfigureAwait(false);
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
2017-12-17 01:34:21 +07:00
|
|
|
catch (DropboxException)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
// we can catch some events here and convert them to Duplicati exceptions
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-15 11:39:27 +02:00
|
|
|
public IList<ICommandLineArgument> SupportedCommands
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2024-09-29 22:00:57 +02:00
|
|
|
return new List<ICommandLineArgument>([
|
2016-09-15 11:39:27 +02:00
|
|
|
new CommandLineArgument(AUTHID_OPTION, CommandLineArgument.ArgumentType.Password, Strings.Dropbox.AuthidShort, Strings.Dropbox.AuthidLong(OAuthHelper.OAUTH_LOGIN_URL("dropbox"))),
|
2024-09-29 22:00:57 +02:00
|
|
|
]);
|
2016-09-15 11:39:27 +02:00
|
|
|
}
|
|
|
|
|
}
|
2016-09-07 19:26:12 +02:00
|
|
|
|
2016-09-15 11:39:27 +02:00
|
|
|
public string Description { get { return Strings.Dropbox.Description; } }
|
2016-07-07 13:44:19 +02:00
|
|
|
|
2024-10-02 09:37:52 +02:00
|
|
|
public Task<string[]> GetDNSNamesAsync(CancellationToken cancelToken) => Task.FromResult(WebApi.Dropbox.Hosts());
|
2018-02-18 00:18:44 +01:00
|
|
|
|
2024-10-01 09:30:02 +02:00
|
|
|
public Task TestAsync(CancellationToken cancelToken)
|
2025-02-17 16:45:51 +01:00
|
|
|
=> this.TestListAsync(cancelToken);
|
2016-07-07 13:44:19 +02:00
|
|
|
|
2024-10-01 09:30:02 +02:00
|
|
|
public async Task CreateFolderAsync(CancellationToken cancelToken)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-10-01 09:30:02 +02:00
|
|
|
await dbx.CreateFolderAsync(m_path, cancelToken).ConfigureAwait(false);
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
|
|
|
|
catch (DropboxException de)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if (de.errorJSON["error"][".tag"].ToString() == "path" && de.errorJSON["error"]["path"][".tag"].ToString() == "conflict")
|
|
|
|
|
throw new FolderAreadyExistedException();
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-17 18:20:14 -05:00
|
|
|
public async Task PutAsync(string remotename, Stream stream, CancellationToken cancelToken)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-02-23 09:14:54 -06:00
|
|
|
string path = $"{m_path}/{remotename}";
|
|
|
|
|
await dbx.UploadFileAsync(path, stream, cancelToken);
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
2017-12-17 01:34:21 +07:00
|
|
|
catch (DropboxException)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
// we can catch some events here and convert them to Duplicati exceptions
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-29 22:00:57 +02:00
|
|
|
public async Task GetAsync(string remotename, Stream stream, CancellationToken cancelToken)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
string path = string.Format("{0}/{1}", m_path, remotename);
|
2024-09-29 22:00:57 +02:00
|
|
|
await dbx.DownloadFileAsync(path, stream, cancelToken).ConfigureAwait(false);
|
2016-07-07 13:44:19 +02:00
|
|
|
}
|
2017-12-17 01:34:21 +07:00
|
|
|
catch (DropboxException)
|
2016-07-07 13:44:19 +02:00
|
|
|
{
|
|
|
|
|
// we can catch some events here and convert them to Duplicati exceptions
|
|
|
|
|
throw;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-04-28 16:41:56 -07:00
|
|
|
}
|