From a96a9ce319781020b571e2c352c5c4b9fd01b5ea Mon Sep 17 00:00:00 2001 From: ThatBen Date: Thu, 27 Feb 2025 10:53:35 +0100 Subject: [PATCH] Considers local quota before uploading. --- .../AutoClient/Modes/FolderStore/FileSaver.cs | 24 ++++++++++- .../Modes/FolderStore/QuotaCheck.cs | 41 +++++++++++++++++++ 2 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 Tools/AutoClient/Modes/FolderStore/QuotaCheck.cs diff --git a/Tools/AutoClient/Modes/FolderStore/FileSaver.cs b/Tools/AutoClient/Modes/FolderStore/FileSaver.cs index 280f980b..cacac867 100644 --- a/Tools/AutoClient/Modes/FolderStore/FileSaver.cs +++ b/Tools/AutoClient/Modes/FolderStore/FileSaver.cs @@ -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; diff --git a/Tools/AutoClient/Modes/FolderStore/QuotaCheck.cs b/Tools/AutoClient/Modes/FolderStore/QuotaCheck.cs new file mode 100644 index 00000000..b7c31fb2 --- /dev/null +++ b/Tools/AutoClient/Modes/FolderStore/QuotaCheck.cs @@ -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; + } + } +}