From f3dbeae28efbe0d4d396bd28cda14eae53fe801d Mon Sep 17 00:00:00 2001 From: sbwalker Date: Mon, 3 Feb 2025 11:00:22 -0500 Subject: [PATCH] fix #5044 - improve file part removal logic --- Oqtane.Server/Controllers/FileController.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Oqtane.Server/Controllers/FileController.cs b/Oqtane.Server/Controllers/FileController.cs index 70d17d1d..847cdfa8 100644 --- a/Oqtane.Server/Controllers/FileController.cs +++ b/Oqtane.Server/Controllers/FileController.cs @@ -510,10 +510,10 @@ namespace Oqtane.Controllers filename = Path.GetFileNameWithoutExtension(filename); // base filename string[] fileparts = Directory.GetFiles(folder, filename + token + "*"); // list of all file parts - // if all of the file parts exist ( note that file parts can arrive out of order ) + // if all of the file parts exist (note that file parts can arrive out of order) if (fileparts.Length == totalparts && CanAccessFiles(fileparts)) { - // merge file parts into temp file ( in case another user is trying to get the file ) + // merge file parts into temp file (in case another user is trying to get the file) bool success = true; using (var stream = new FileStream(Path.Combine(folder, filename + ".tmp"), FileMode.Create)) { @@ -536,17 +536,23 @@ namespace Oqtane.Controllers // clean up file parts foreach (var file in Directory.GetFiles(folder, "*" + token + "*")) { - // file name matches part or is more than 2 hours old (ie. a prior file upload failed) - if (fileparts.Contains(file) || System.IO.File.GetCreationTime(file).ToUniversalTime() < DateTime.UtcNow.AddHours(-2)) + if (fileparts.Contains(file)) { - System.IO.File.Delete(file); + try + { + System.IO.File.Delete(file); + } + catch + { + // unable to delete part - ignore + } } } // rename temp file if (success) { - // remove file if it already exists (as well as any thumbnails) + // remove file if it already exists (as well as any thumbnails which may exist) foreach (var file in Directory.GetFiles(folder, Path.GetFileNameWithoutExtension(filename) + ".*")) { if (Path.GetExtension(file) != ".tmp")