From 0b4d75dabc89a46cd09bd2ad57031aefb9540402 Mon Sep 17 00:00:00 2001 From: TopperDEL Date: Thu, 7 May 2020 22:22:34 +0200 Subject: [PATCH] feat: Working backup! --- ...uplicati.Library.Backend.Tardigrade.csproj | 1 + .../TardigradeBackend.cs | 46 ++++++++++++++++-- .../TardigradeFile.cs | 48 +++++++++++++++++++ 3 files changed, 91 insertions(+), 4 deletions(-) create mode 100644 Duplicati.Library.Backend.Tardigrade/TardigradeFile.cs diff --git a/Duplicati.Library.Backend.Tardigrade/Duplicati.Library.Backend.Tardigrade.csproj b/Duplicati.Library.Backend.Tardigrade/Duplicati.Library.Backend.Tardigrade.csproj index ab884b3bf..49acfd394 100644 --- a/Duplicati.Library.Backend.Tardigrade/Duplicati.Library.Backend.Tardigrade.csproj +++ b/Duplicati.Library.Backend.Tardigrade/Duplicati.Library.Backend.Tardigrade.csproj @@ -47,6 +47,7 @@ + diff --git a/Duplicati.Library.Backend.Tardigrade/TardigradeBackend.cs b/Duplicati.Library.Backend.Tardigrade/TardigradeBackend.cs index 690cac820..966bf2aa5 100644 --- a/Duplicati.Library.Backend.Tardigrade/TardigradeBackend.cs +++ b/Duplicati.Library.Backend.Tardigrade/TardigradeBackend.cs @@ -1,6 +1,7 @@ using Duplicati.Library.Interface; using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Threading; @@ -148,17 +149,54 @@ namespace Duplicati.Library.Backend.Tardigrade public void Get(string remotename, string filename) { - throw new NotImplementedException(); + var getTask = GetAsync(remotename, filename); + getTask.Wait(); + } + + public async Task GetAsync(string remotename, string filename) + { + var bucket = await _bucketService.EnsureBucketAsync(_bucket); + var download = await _objectService.DownloadObjectAsync(bucket, GetBasePath() + remotename, new DownloadOptions(), false); + await download.StartDownloadAsync(); + + if(download.Completed) + { + using (FileStream file = new FileStream(filename, FileMode.Create)) + { + await file.WriteAsync(download.DownloadedBytes, 0, (int)download.BytesReceived); + await file.FlushAsync(); + } + } } public IEnumerable List() { - throw new NotImplementedException(); + var listTask = ListAsync(); + listTask.Wait(); + return listTask.Result; } - public Task PutAsync(string remotename, string filename, CancellationToken cancelToken) + private async Task> ListAsync() { - throw new NotImplementedException(); + List files = new List(); + var bucket = await _bucketService.EnsureBucketAsync(_bucket); + var objects = await _objectService.ListObjectsAsync(bucket, new ListObjectsOptions() { Recursive = true, System = true, Custom = true }); + + foreach(var obj in objects.Items) + { + TardigradeFile file = new TardigradeFile(obj); + file.Name = file.Name.Replace(GetBasePath(), ""); + files.Add(file); + } + + return files; + } + + public async Task PutAsync(string remotename, string filename, CancellationToken cancelToken) + { + var bucket = await _bucketService.EnsureBucketAsync(_bucket); + var upload = await _objectService.UploadObjectAsync(bucket, GetBasePath() + remotename, new UploadOptions(), new FileStream(filename, FileMode.Open), false); + await upload.StartUploadAsync(); } public void Test() diff --git a/Duplicati.Library.Backend.Tardigrade/TardigradeFile.cs b/Duplicati.Library.Backend.Tardigrade/TardigradeFile.cs new file mode 100644 index 000000000..576617778 --- /dev/null +++ b/Duplicati.Library.Backend.Tardigrade/TardigradeFile.cs @@ -0,0 +1,48 @@ +using Duplicati.Library.Interface; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Duplicati.Library.Backend.Tardigrade +{ + public class TardigradeFile : IFileEntry + { + public const string TARDIGRADE_LAST_ACCESS = "DUPLICATI:LAST-ACCESS"; + public const string TARDIGRADE_LAST_MODIFICATION = "DUPLICATI:LAST-MODIFICATION"; + public bool IsFolder { get; set; } + + public DateTime LastAccess { get; set; } + + public DateTime LastModification { get; set; } + + public string Name { get; set; } + + public long Size { get; set; } + + public TardigradeFile() + { + + } + + public TardigradeFile(uplink.NET.Models.Object tardigradeObject) + { + IsFolder = tardigradeObject.IsPrefix; + var lastAccess = tardigradeObject.CustomMetaData.Entries.Where(e => e.Key == TARDIGRADE_LAST_ACCESS).FirstOrDefault(); + if (lastAccess != null && !string.IsNullOrEmpty(lastAccess.Value)) + LastAccess = DateTime.Parse(lastAccess.Value); + else + LastAccess = DateTime.MinValue; + + var lastMod = tardigradeObject.CustomMetaData.Entries.Where(e => e.Key == TARDIGRADE_LAST_MODIFICATION).FirstOrDefault(); + if (lastMod != null && !string.IsNullOrEmpty(lastMod.Value)) + LastModification = DateTime.Parse(lastMod.Value); + else + LastModification = DateTime.MinValue; + + Name = tardigradeObject.Key; + Size = tardigradeObject.SystemMetaData.ContentLength; + } + } +}