From b8ce634f85d0432bc4aef399e33d12ad24931ddf Mon Sep 17 00:00:00 2001 From: Jim Spillane Date: Mon, 18 May 2020 19:53:49 -0400 Subject: [PATCH] Fix file upload merge Allow upload of file names that match the token pattern ".part_", but not in the file extension. For example, a file named, a.part_Y.txt, would not be uploaded. --- Oqtane.Server/Controllers/FileController.cs | 23 ++++++++++++--------- Oqtane.Shared/Shared/Constants.cs | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Oqtane.Server/Controllers/FileController.cs b/Oqtane.Server/Controllers/FileController.cs index 0c4bfad6..5d77254b 100644 --- a/Oqtane.Server/Controllers/FileController.cs +++ b/Oqtane.Server/Controllers/FileController.cs @@ -65,7 +65,7 @@ namespace Oqtane.Controllers { foreach (string file in Directory.GetFiles(folder)) { - files.Add(new Models.File {Name = Path.GetFileName(file), Extension = Path.GetExtension(file)?.Replace(".", "")}); + files.Add(new Models.File { Name = Path.GetFileName(file), Extension = Path.GetExtension(file)?.Replace(".", "") }); } } } @@ -256,7 +256,7 @@ namespace Oqtane.Controllers HttpContext.Response.StatusCode = (int)HttpStatusCode.Conflict; return; } - + string folderPath = ""; if (int.TryParse(folder, out int folderId)) @@ -306,7 +306,8 @@ namespace Oqtane.Controllers string token = ".part_"; string parts = Path.GetExtension(filename)?.Replace(token, ""); // returns "x_y" int totalparts = int.Parse(parts?.Substring(parts.IndexOf("_") + 1)); - filename = filename?.Substring(0, filename.IndexOf(token)); // base filename + + 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 ) @@ -363,13 +364,15 @@ namespace Oqtane.Controllers } // clean up file parts which are more than 2 hours old ( which can happen if a prior file upload failed ) - fileParts = Directory.GetFiles(folder, "*" + token + "*"); - foreach (string filepart in fileParts) + var cleanupFiles = Directory.EnumerateFiles(folder, "*" + token + "*") + .Where(f => Path.GetExtension(f).StartsWith(token)); + + foreach (var file in cleanupFiles) { - DateTime createddate = System.IO.File.GetCreationTime(filepart).ToUniversalTime(); - if (createddate < DateTime.UtcNow.AddHours(-2)) + var createdDate = System.IO.File.GetCreationTime(file).ToUniversalTime(); + if (createdDate < DateTime.UtcNow.AddHours(-2)) { - System.IO.File.Delete(filepart); + System.IO.File.Delete(file); } } @@ -480,7 +483,7 @@ namespace Oqtane.Controllers string[] folders = folderpath.Split(separators, StringSplitOptions.RemoveEmptyEntries); foreach (string folder in folders) { - path = Utilities.PathCombine(path, folder,"\\"); + path = Utilities.PathCombine(path, folder, "\\"); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); @@ -497,7 +500,7 @@ namespace Oqtane.Controllers FileInfo fileinfo = new FileInfo(filepath); file.Extension = fileinfo.Extension.ToLower().Replace(".", ""); - file.Size = (int) fileinfo.Length; + file.Size = (int)fileinfo.Length; file.ImageHeight = 0; file.ImageWidth = 0; diff --git a/Oqtane.Shared/Shared/Constants.cs b/Oqtane.Shared/Shared/Constants.cs index 4a1c1ce4..11042314 100644 --- a/Oqtane.Shared/Shared/Constants.cs +++ b/Oqtane.Shared/Shared/Constants.cs @@ -45,7 +45,7 @@ namespace Oqtane.Shared public const string ImageFiles = "jpg,jpeg,jpe,gif,bmp,png"; public const string UploadableFiles = "jpg,jpeg,jpe,gif,bmp,png,mov,wmv,avi,mp4,mp3,doc,docx,xls,xlsx,ppt,pptx,pdf,txt,zip,nupkg"; - public const string ReservedDevices = "CON,NUL,PRN,,COM0,COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,LPT0,LPT1,LPT2,LPT3,LPT4,LPT5,LPT6,LPT7,LPT8,LPT9,CONIN$,CONOUT$"; + public const string ReservedDevices = "CON,NUL,PRN,COM0,COM1,COM2,COM3,COM4,COM5,COM6,COM7,COM8,COM9,LPT0,LPT1,LPT2,LPT3,LPT4,LPT5,LPT6,LPT7,LPT8,LPT9,CONIN$,CONOUT$"; public static readonly char[] InvalidFileNameChars = { '\"', '<', '>', '|', '\0', (Char) 1, (Char) 2, (Char) 3, (Char) 4, (Char) 5, (Char) 6, (Char) 7, (Char) 8,