Skip to content

Commit 698c03e

Browse files
committed
Define UploadPathHelper.TemporaryFilePrefix and UploadPathHelper.IsTemporaryFile and use them instead of hardcoding temporary/
1 parent 242316c commit 698c03e

File tree

4 files changed

+23
-5
lines changed

4 files changed

+23
-5
lines changed

src/services/Upload/DefaultUploadProcessor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public virtual ProcessedUploadInfo Process(System.IO.Stream stream, string filen
5757

5858
UploadPathHelper.CheckFileNameSecurity(filename);
5959

60-
bool isExistingTemporary = filename.StartsWith("temporary/", StringComparison.Ordinal) &&
60+
bool isExistingTemporary = UploadPathHelper.IsTemporaryFile(filename) &&
6161
uploadStorage.FileExists(filename);
6262

6363
if (!isExistingTemporary)
@@ -86,7 +86,7 @@ public virtual ProcessedUploadInfo Process(System.IO.Stream stream, string filen
8686

8787
if (!isExistingTemporary)
8888
{
89-
var basePath = "temporary/" + Guid.NewGuid().ToString("N");
89+
var basePath = UploadPathHelper.TemporaryFilePrefix + Guid.NewGuid().ToString("N");
9090
stream.Seek(0, System.IO.SeekOrigin.Begin);
9191
result.TemporaryFile = uploadStorage.WriteFile(basePath + System.IO.Path.GetExtension(filename),
9292
stream, OverwriteOption.Disallowed);

src/services/Upload/UploadPathHelper.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,22 @@ public static string FindAvailableName(string path, Func<string, bool> exists)
113113

114114
return path;
115115
}
116+
117+
/// <summary>
118+
/// Represents the prefix used to identify temporary upload files.
119+
/// </summary>
120+
/// <remarks>Use this constant to distinguish files that are stored temporarily during the upload process.
121+
/// Files with this prefix are typically subject to cleanup or special handling.</remarks>
122+
public const string TemporaryFilePrefix = "temporary/";
123+
124+
/// <summary>
125+
/// Determines whether the specified file name represents a temporary upload file.
126+
/// </summary>
127+
/// <param name="fileName">The name of the file to evaluate. Can be null or empty.</param>
128+
/// <returns>true if the file name is not null or empty and indicates a temporary upload file; otherwise, false.</returns>
129+
public static bool IsTemporaryFile(string fileName)
130+
{
131+
return !string.IsNullOrEmpty(fileName) &&
132+
fileName.StartsWith(TemporaryFilePrefix, StringComparison.Ordinal);
133+
}
116134
}

src/web/Upload/DefaultUploadStorage.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ public DefaultUploadStorage(IOptions<UploadSettings> options, IWebHostEnvironmen
4545
new TempUploadStorage(new DiskUploadStorageOptions
4646
{
4747
RootPath = Path.Combine(path, "temporary"),
48-
RootUrl = UriHelper.Combine(opt.Url, "temporary/")
48+
RootUrl = UriHelper.Combine(opt.Url, UploadPathHelper.TemporaryFilePrefix)
4949
}, fileSystem),
50-
"temporary/");
50+
UploadPathHelper.TemporaryFilePrefix);
5151
}
5252

5353
/// <inheritdoc/>

src/web/Upload/FileUploadBehavior.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ private CopyTemporaryFileResult CopyTemporaryFile(ISaveRequestHandler handler, I
360360
if (string.IsNullOrEmpty(temporaryFile))
361361
throw new ArgumentNullException(nameof(temporaryFile));
362362

363-
if (!temporaryFile.StartsWith("temporary/", StringComparison.OrdinalIgnoreCase))
363+
if (!UploadPathHelper.IsTemporaryFile(temporaryFile))
364364
throw new InvalidOperationException("For security reasons, only temporary files can be used in uploads!");
365365

366366
UploadPathHelper.CheckFileNameSecurity(temporaryFile);

0 commit comments

Comments
 (0)