diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-09-16 21:33:53 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-09-16 21:33:53 -0400 |
commit | de84adb47751c96fc60be66f6fdb186a09160820 (patch) | |
tree | 5c09422db13fba3c9a231308def6913188989946 /src/SMAPI.Web/Framework | |
parent | 44e91ca5e1527333fdbd2993b694cc17601293e3 (diff) | |
parent | 59974c18a6fb6f8f2e7e3a8c5df109e060fc5ae6 (diff) | |
download | SMAPI-de84adb47751c96fc60be66f6fdb186a09160820.tar.gz SMAPI-de84adb47751c96fc60be66f6fdb186a09160820.tar.bz2 SMAPI-de84adb47751c96fc60be66f6fdb186a09160820.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI.Web/Framework')
-rw-r--r-- | src/SMAPI.Web/Framework/Storage/IStorageProvider.cs | 3 | ||||
-rw-r--r-- | src/SMAPI.Web/Framework/Storage/StorageProvider.cs | 53 |
2 files changed, 34 insertions, 22 deletions
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 /// <summary>Fetch raw text from storage.</summary> /// <param name="id">The storage ID returned by <see cref="SaveAsync"/>.</param> - Task<StoredFileInfo> GetAsync(string id); + /// <param name="renew">Whether to reset the file expiry.</param> + Task<StoredFileInfo> 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; } - /// <summary>Save a text file to storage.</summary> - /// <param name="content">The content to upload.</param> - /// <param name="compress">Whether to gzip the text.</param> - /// <returns>Returns metadata about the save attempt.</returns> + /// <inheritdoc /> public async Task<UploadResult> SaveAsync(string content, bool compress = true) { string id = Guid.NewGuid().ToString("N"); @@ -84,9 +82,8 @@ namespace StardewModdingAPI.Web.Framework.Storage } } - /// <summary>Fetch raw text from storage.</summary> - /// <param name="id">The storage ID returned by <see cref="SaveAsync"/>.</param> - public async Task<StoredFileInfo> GetAsync(string id) + /// <inheritdoc /> + public async Task<StoredFileInfo> 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<string, string> { ["expiryRenewed"] = DateTime.UtcNow.ToString("O") }); // change the blob's last-modified date (the specific property set doesn't matter) + + // fetch file Response<BlobDownloadInfo> 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." }; } } |