summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Storage
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework/Storage')
-rw-r--r--src/SMAPI.Web/Framework/Storage/StorageProvider.cs54
-rw-r--r--src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs39
-rw-r--r--src/SMAPI.Web/Framework/Storage/UploadResult.cs14
3 files changed, 61 insertions, 46 deletions
diff --git a/src/SMAPI.Web/Framework/Storage/StorageProvider.cs b/src/SMAPI.Web/Framework/Storage/StorageProvider.cs
index c6f8bac1..effbbc9f 100644
--- a/src/SMAPI.Web/Framework/Storage/StorageProvider.cs
+++ b/src/SMAPI.Web/Framework/Storage/StorageProvider.cs
@@ -63,11 +63,11 @@ namespace StardewModdingAPI.Web.Framework.Storage
BlobClient blob = this.GetAzureBlobClient(id);
await blob.UploadAsync(stream);
- return new UploadResult(true, id, null);
+ return new UploadResult(id, null);
}
catch (Exception ex)
{
- return new UploadResult(false, null, ex.Message);
+ return new UploadResult(null, ex.Message);
}
}
@@ -75,10 +75,10 @@ namespace StardewModdingAPI.Web.Framework.Storage
else
{
string path = this.GetDevFilePath(id);
- Directory.CreateDirectory(Path.GetDirectoryName(path));
+ Directory.CreateDirectory(Path.GetDirectoryName(path)!);
File.WriteAllText(path, content);
- return new UploadResult(true, id, null);
+ return new UploadResult(id, null);
}
}
@@ -103,26 +103,20 @@ namespace StardewModdingAPI.Web.Framework.Storage
// fetch file
Response<BlobDownloadInfo> response = await blob.DownloadAsync();
using BlobDownloadInfo result = response.Value;
- using StreamReader reader = new StreamReader(result.Content);
+ using StreamReader reader = new(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,
- Content = content,
- Expiry = expiry.UtcDateTime
- };
+ return new StoredFileInfo(content, expiry);
}
catch (RequestFailedException ex)
{
- return new StoredFileInfo
- {
- Error = ex.ErrorCode == "BlobNotFound"
+ return new StoredFileInfo(
+ error: ex.ErrorCode == "BlobNotFound"
? "There's no file with that ID."
: $"Could not fetch that file from storage ({ex.ErrorCode}: {ex.Message})."
- };
+ );
}
}
@@ -130,15 +124,12 @@ namespace StardewModdingAPI.Web.Framework.Storage
else
{
// get file
- FileInfo file = new FileInfo(this.GetDevFilePath(id));
+ FileInfo file = new(this.GetDevFilePath(id));
if (file.Exists && file.LastWriteTimeUtc.AddDays(this.ExpiryDays) < DateTime.UtcNow) // expired
file.Delete();
if (!file.Exists)
{
- return new StoredFileInfo
- {
- Error = "There's no file with that ID."
- };
+ return new StoredFileInfo(error: "There's no file with that ID.");
}
// renew
@@ -149,13 +140,11 @@ namespace StardewModdingAPI.Web.Framework.Storage
}
// build model
- 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."
- };
+ return new StoredFileInfo(
+ 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."
+ );
}
}
@@ -164,12 +153,7 @@ namespace StardewModdingAPI.Web.Framework.Storage
{
PasteInfo response = await this.Pastebin.GetAsync(id);
response.Content = this.GzipHelper.DecompressString(response.Content);
- return new StoredFileInfo
- {
- Success = response.Success,
- Content = response.Content,
- Error = response.Error
- };
+ return new StoredFileInfo(response.Content, null, error: response.Error);
}
}
@@ -177,8 +161,8 @@ namespace StardewModdingAPI.Web.Framework.Storage
/// <param name="id">The file ID.</param>
private BlobClient GetAzureBlobClient(string id)
{
- var azure = new BlobServiceClient(this.ClientsConfig.AzureBlobConnectionString);
- var container = azure.GetBlobContainerClient(this.ClientsConfig.AzureBlobTempContainer);
+ BlobServiceClient azure = new(this.ClientsConfig.AzureBlobConnectionString);
+ BlobContainerClient container = azure.GetBlobContainerClient(this.ClientsConfig.AzureBlobTempContainer);
return container.GetBlobClient($"uploads/{id}");
}
diff --git a/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs b/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs
index 30676c88..bbbcf2a9 100644
--- a/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs
+++ b/src/SMAPI.Web/Framework/Storage/StoredFileInfo.cs
@@ -1,23 +1,52 @@
using System;
+using System.Diagnostics.CodeAnalysis;
namespace StardewModdingAPI.Web.Framework.Storage
{
/// <summary>The response for a get-file request.</summary>
internal class StoredFileInfo
{
+ /*********
+ ** Accessors
+ *********/
/// <summary>Whether the file was successfully fetched.</summary>
- public bool Success { get; set; }
+ [MemberNotNullWhen(true, nameof(StoredFileInfo.Content))]
+ public bool Success => this.Content != null && this.Error == null;
/// <summary>The fetched file content (if <see cref="Success"/> is <c>true</c>).</summary>
- public string Content { get; set; }
+ public string? Content { get; }
/// <summary>When the file will no longer be available.</summary>
- public DateTime? Expiry { get; set; }
+ public DateTimeOffset? Expiry { get; }
/// <summary>The error message if saving succeeded, but a non-blocking issue was encountered.</summary>
- public string Warning { get; set; }
+ public string? Warning { get; }
/// <summary>The error message if saving failed.</summary>
- public string Error { get; set; }
+ public string? Error { get; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="content">The fetched file content (if <see cref="Success"/> is <c>true</c>).</param>
+ /// <param name="expiry">When the file will no longer be available.</param>
+ /// <param name="warning">The error message if saving succeeded, but a non-blocking issue was encountered.</param>
+ /// <param name="error">The error message if saving failed.</param>
+ public StoredFileInfo(string? content, DateTimeOffset? expiry, string? warning = null, string? error = null)
+ {
+ this.Content = content;
+ this.Expiry = expiry;
+ this.Warning = warning;
+ this.Error = error;
+ }
+
+ /// <summary>Construct an instance.</summary>
+ /// <param name="error">The error message if saving failed.</param>
+ public StoredFileInfo(string error)
+ {
+ this.Error = error;
+ }
}
}
diff --git a/src/SMAPI.Web/Framework/Storage/UploadResult.cs b/src/SMAPI.Web/Framework/Storage/UploadResult.cs
index 483c1769..92993d42 100644
--- a/src/SMAPI.Web/Framework/Storage/UploadResult.cs
+++ b/src/SMAPI.Web/Framework/Storage/UploadResult.cs
@@ -1,3 +1,5 @@
+using System.Diagnostics.CodeAnalysis;
+
namespace StardewModdingAPI.Web.Framework.Storage
{
/// <summary>The result of an attempt to upload a file.</summary>
@@ -7,25 +9,25 @@ namespace StardewModdingAPI.Web.Framework.Storage
** Accessors
*********/
/// <summary>Whether the file upload succeeded.</summary>
- public bool Succeeded { get; }
+ [MemberNotNullWhen(true, nameof(UploadResult.ID))]
+ [MemberNotNullWhen(false, nameof(UploadResult.UploadError))]
+ public bool Succeeded => this.ID != null && this.UploadError == null;
/// <summary>The file ID, if applicable.</summary>
- public string ID { get; }
+ public string? ID { get; }
/// <summary>The upload error, if any.</summary>
- public string UploadError { get; }
+ public string? UploadError { get; }
/*********
** Public methods
*********/
/// <summary>Construct an instance.</summary>
- /// <param name="succeeded">Whether the file upload succeeded.</param>
/// <param name="id">The file ID, if applicable.</param>
/// <param name="uploadError">The upload error, if any.</param>
- public UploadResult(bool succeeded, string id, string uploadError)
+ public UploadResult(string? id, string? uploadError)
{
- this.Succeeded = succeeded;
this.ID = id;
this.UploadError = uploadError;
}