feat: Working backup!

This commit is contained in:
TopperDEL
2020-05-07 22:22:34 +02:00
parent e6842e7adc
commit 0b4d75dabc
3 changed files with 91 additions and 4 deletions
@@ -47,6 +47,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="TardigradeFile.cs" />
<Compile Include="Strings.cs" />
<Compile Include="TardigradeBackend.cs" />
<Compile Include="Properties\AssemblyInfo.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<IFileEntry> List()
{
throw new NotImplementedException();
var listTask = ListAsync();
listTask.Wait();
return listTask.Result;
}
public Task PutAsync(string remotename, string filename, CancellationToken cancelToken)
private async Task<IEnumerable<IFileEntry>> ListAsync()
{
throw new NotImplementedException();
List<TardigradeFile> files = new List<TardigradeFile>();
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()
@@ -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;
}
}
}