Considers local quota before uploading.

This commit is contained in:
ThatBen 2025-02-27 10:53:35 +01:00
parent 3ab1e2ea47
commit a96a9ce319
No known key found for this signature in database
GPG Key ID: 62C543548433D43E
2 changed files with 64 additions and 1 deletions

View File

@ -11,6 +11,7 @@ namespace AutoClient.Modes.FolderStore
private readonly Stats stats;
private readonly string folderFile;
private readonly FileStatus entry;
private readonly QuotaCheck quotaCheck;
public FileSaver(ILog log, CodexWrapper instance, Stats stats, string folderFile, FileStatus entry)
{
@ -19,6 +20,8 @@ namespace AutoClient.Modes.FolderStore
this.stats = stats;
this.folderFile = folderFile;
this.entry = entry;
quotaCheck = new QuotaCheck(log, folderFile, instance);
}
public bool HasFailed { get; private set; }
@ -45,7 +48,10 @@ namespace AutoClient.Modes.FolderStore
Log("BasicCid is available.");
return;
}
UploadFile();
if (QuotaAvailable())
{
UploadFile();
}
}
private bool IsBasicCidAvailable()
@ -54,6 +60,22 @@ namespace AutoClient.Modes.FolderStore
return NodeContainsBasicCid();
}
private bool QuotaAvailable()
{
if (quotaCheck.IsLocalQuotaAvailable()) return true;
Log("Waiting for local storage quota to become available...");
var timeLimit = DateTime.UtcNow + TimeSpan.FromHours(1.0);
while (DateTime.UtcNow < timeLimit)
{
if (quotaCheck.IsLocalQuotaAvailable()) return true;
Thread.Sleep(TimeSpan.FromMinutes(1.0));
}
Log("Could not upload: Insufficient local storage quota.");
HasFailed = true;
return false;
}
private bool HasRecentPurchase(FileStatus entry)
{
if (string.IsNullOrEmpty(entry.PurchaseId)) return false;

View File

@ -0,0 +1,41 @@
using Logging;
namespace AutoClient.Modes.FolderStore
{
public class QuotaCheck
{
private readonly ILog log;
private readonly string filepath;
private readonly CodexWrapper instance;
public QuotaCheck(ILog log, string filepath, CodexWrapper instance)
{
this.log = log;
this.filepath = filepath;
this.instance = instance;
}
public bool IsLocalQuotaAvailable()
{
try
{
return CheckQuota();
}
catch (Exception exc)
{
log.Error("Failed to check quota: " + exc);
throw;
}
}
private bool CheckQuota()
{
var info = new FileInfo(filepath);
var fileSize = info.Length;
var padded = fileSize * 1.1;
var space = instance.Node.Space();
return space.FreeBytes > padded;
}
}
}