From 4189e2f3faa9197e83aebd32fc0af93d46ee1a61 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 13 Sep 2020 11:59:17 -0400 Subject: add support for renewing uploaded JSON/log files --- .../Framework/Storage/IStorageProvider.cs | 3 +- src/SMAPI.Web/Framework/Storage/StorageProvider.cs | 53 +++++++++++++--------- 2 files changed, 34 insertions(+), 22 deletions(-) (limited to 'src/SMAPI.Web/Framework') diff --git a/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs b/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs index 96a34fbb..dfc1fb47 100644 --- a/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs +++ b/src/SMAPI.Web/Framework/Storage/IStorageProvider.cs @@ -13,6 +13,7 @@ namespace StardewModdingAPI.Web.Framework.Storage /// Fetch raw text from storage. /// The storage ID returned by . - Task GetAsync(string id); + /// Whether to reset the file expiry. + Task GetAsync(string id, bool renew); } } diff --git a/src/SMAPI.Web/Framework/Storage/StorageProvider.cs b/src/SMAPI.Web/Framework/Storage/StorageProvider.cs index 35538443..c6f8bac1 100644 --- a/src/SMAPI.Web/Framework/Storage/StorageProvider.cs +++ b/src/SMAPI.Web/Framework/Storage/StorageProvider.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.IO; using System.Text; using System.Threading.Tasks; @@ -48,10 +49,7 @@ namespace StardewModdingAPI.Web.Framework.Storage this.GzipHelper = gzipHelper; } - /// Save a text file to storage. - /// The content to upload. - /// Whether to gzip the text. - /// Returns metadata about the save attempt. + /// public async Task SaveAsync(string content, bool compress = true) { string id = Guid.NewGuid().ToString("N"); @@ -84,9 +82,8 @@ namespace StardewModdingAPI.Web.Framework.Storage } } - /// Fetch raw text from storage. - /// The storage ID returned by . - public async Task GetAsync(string id) + /// + public async Task GetAsync(string id, bool renew) { // fetch from blob storage if (Guid.TryParseExact(id, "N", out Guid _)) @@ -96,14 +93,21 @@ namespace StardewModdingAPI.Web.Framework.Storage { try { + // get client BlobClient blob = this.GetAzureBlobClient(id); + + // extend expiry + if (renew) + await blob.SetMetadataAsync(new Dictionary { ["expiryRenewed"] = DateTime.UtcNow.ToString("O") }); // change the blob's last-modified date (the specific property set doesn't matter) + + // fetch file Response response = await blob.DownloadAsync(); using BlobDownloadInfo result = response.Value; - using StreamReader reader = new StreamReader(result.Content); DateTimeOffset expiry = result.Details.LastModified + TimeSpan.FromDays(this.ExpiryDays); string content = this.GzipHelper.DecompressString(reader.ReadToEnd()); + // build model return new StoredFileInfo { Success = true, @@ -125,25 +129,32 @@ namespace StardewModdingAPI.Web.Framework.Storage // local filesystem for testing else { + // get file FileInfo file = new FileInfo(this.GetDevFilePath(id)); - if (file.Exists) + if (file.Exists && file.LastWriteTimeUtc.AddDays(this.ExpiryDays) < DateTime.UtcNow) // expired + file.Delete(); + if (!file.Exists) { - if (file.LastWriteTimeUtc.AddDays(this.ExpiryDays) < DateTime.UtcNow) - file.Delete(); - else + return new StoredFileInfo { - return new StoredFileInfo - { - Success = true, - Content = File.ReadAllText(file.FullName), - Expiry = DateTime.UtcNow.AddDays(this.ExpiryDays), - Warning = "This file was saved temporarily to the local computer. This should only happen in a local development environment." - }; - } + Error = "There's no file with that ID." + }; } + + // renew + if (renew) + { + File.SetLastWriteTimeUtc(file.FullName, DateTime.UtcNow); + file.Refresh(); + } + + // build model return new StoredFileInfo { - Error = "There's no file with that ID." + Success = true, + Content = File.ReadAllText(file.FullName), + Expiry = DateTime.UtcNow.AddDays(this.ExpiryDays), + Warning = "This file was saved temporarily to the local computer. This should only happen in a local development environment." }; } } -- cgit