WIP, abandoned: One-way backup tool with versioning and client-side encryption. Your precious data will never get lost!
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
2.0 KiB

namespace EternalArrowBackup.TargetMetadataStorage.InMemoryMetadataStorage
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
using EternalArrowBackup.TargetMetadataStorage.Contracts;
internal class TargetDirectory : ITargetDirectory
{
public TargetDirectory(MetadataStorage metadataStorage, string directoryName, Dictionary<string, List<ITargetFileVersion>> files)
{
this.MetadataStorage = metadataStorage;
this.DirectoryName = directoryName;
this.Files = files;
}
public string DirectoryName { get; }
private MetadataStorage MetadataStorage { get; }
private Dictionary<string, List<ITargetFileVersion>> Files { get; }
public Task GetAllLatestFileVersions(ITargetBlock<ITargetFileVersion> actionBlock, CancellationToken ct)
{
return Task.Run(() =>
{
foreach (var file in this.Files.Values)
{
if (ct.IsCancellationRequested)
{
break;
}
if (file.Any())
{
actionBlock.Post(file.Last());
}
}
actionBlock.Complete();
});
}
public Task UploadFile(string filename, string originalHash, long originalSize)
{
return Task.Run(() =>
{
if (!this.Files.ContainsKey(filename))
{
this.Files[filename] = new List<ITargetFileVersion>();
}
var fileVersion = new TargetFileVersion(filename, originalHash, DateTime.UtcNow, originalSize);
this.Files[filename].Add(fileVersion);
MetadataStorage.AddBlob(fileVersion);
});
}
}
}