From 775ffc88d970e4991281ff6f14ffa89ac56a4f8a Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 06:16:32 +0100 Subject: [PATCH 1/2] Logo upload: never replace a file already in the media folder The logo upload wrote into the media root, which every store shares, with FileMode.OpenOrCreate. A file with the same name, such as another store's logo, was overwritten, and since the mode does not truncate, a shorter upload left the end of the old file behind and corrupted the image. The store panel reaches the same action. An upload whose name is taken is now saved under the next free name (logo-1.png, logo-2.png, ...) with FileMode.CreateNew, and that name is returned to the settings form. The two identical branches of the action are merged. --- .../Controllers/BasePictureControllerTests.cs | 31 ++++++++ .../Controllers/BasePictureController.cs | 74 ++++++++----------- 2 files changed, 61 insertions(+), 44 deletions(-) diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs index e56224c18..0e1b433a5 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs @@ -166,4 +166,35 @@ public async Task AsyncLogoUpload_DisallowedFileType_ReturnsFailureJson() Assert.AreEqual("File no allowed", GetValue(result, "message")); _mediaFileStoreMock.Verify(m => m.GetFileInfo(It.IsAny()), Times.Never); } + + [TestMethod] + public async Task AsyncLogoUpload_NameTaken_KeepsTheExistingFile_AndSavesUnderAFreeName() + { + _permissionServiceMock.Setup(p => p.Authorize(StandardPermission.ManageSettings)).ReturnsAsync(true); + var folder = Directory.CreateDirectory(Path.Join(Path.GetTempPath(), "grand-logo-" + Guid.NewGuid())).FullName; + try + { + await File.WriteAllTextAsync(Path.Join(folder, "logo.png"), "another store's logo"); + var root = new Mock(); + root.Setup(r => r.PhysicalPath).Returns(folder); + _mediaFileStoreMock.Setup(m => m.GetDirectoryInfo("")).Returns(root.Object); + _mediaFileStoreMock.Setup(m => m.GetFileInfo(It.IsAny())) + .ReturnsAsync((string name) => File.Exists(Path.Join(folder, name)) ? Mock.Of() : null); + var file = CreateFormFile("logo.png", "image/png"); + file.Setup(f => f.CopyToAsync(It.IsAny(), It.IsAny())) + .Returns((Stream target, CancellationToken _) => target.WriteAsync("new"u8.ToArray()).AsTask()); + + var result = await CreateController().AsyncLogoUpload(file.Object); + + Assert.IsTrue(GetValue(result, "success")); + Assert.AreEqual("logo-1.png", GetValue(result, "imageUrl")); + Assert.AreEqual("another store's logo", await File.ReadAllTextAsync(Path.Join(folder, "logo.png"))); + Assert.AreEqual("new", await File.ReadAllTextAsync(Path.Join(folder, "logo-1.png"))); + } + finally + { + Directory.Delete(folder, true); + } + } } + diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs index 543d68df4..5c7886714 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs @@ -89,58 +89,44 @@ public virtual async Task AsyncLogoUpload(IFormFile file) message = "Unknown content type" }); fileName = fileName.Replace(" ", ""); - var fileInfo = await mediaFileStore.GetFileInfo(fileName); - if (fileInfo == null) - try - { - var filepath = mediaFileStore.GetDirectoryInfo(""); - if (filepath != null) - { - await using (var stream = new FileStream(mediaFileStore.Combine(filepath.PhysicalPath, fileName), - FileMode.OpenOrCreate)) - { - await file.CopyToAsync(stream); - } - - return Json(new { - success = true, - imageUrl = fileName - }); - } - - return Json(new { - success = false, - message = "Physical path not exist" - }); - } - catch (Exception ex) - { - return Json(new { - success = false, - message = ex.Message - }); - } + var filepath = mediaFileStore.GetDirectoryInfo(""); + if (filepath == null) + return Json(new { + success = false, + message = "Physical path not exist" + }); + //the folder is shared by every store, so an existing file (another store's logo) is never replaced + fileName = await FreeFileName(fileName); + try { - var filepath = mediaFileStore.GetDirectoryInfo(""); - if (filepath != null) + await using (var stream = new FileStream(mediaFileStore.Combine(filepath.PhysicalPath, fileName), + FileMode.CreateNew)) { - await using (var stream = new FileStream(mediaFileStore.Combine(filepath.PhysicalPath, fileName), - FileMode.OpenOrCreate)) - { - await file.CopyToAsync(stream); - } - - return Json(new { - success = true, - imageUrl = fileName - }); + await file.CopyToAsync(stream); } + return Json(new { + success = true, + imageUrl = fileName + }); + } + catch (Exception ex) + { return Json(new { success = false, - message = "Physical path not exist" + message = ex.Message }); } } + + private async Task FreeFileName(string fileName) + { + var name = Path.GetFileNameWithoutExtension(fileName); + var extension = Path.GetExtension(fileName); + var candidate = fileName; + for (var i = 1; await mediaFileStore.GetFileInfo(candidate) != null; i++) + candidate = $"{name}-{i}{extension}"; + return candidate; + } } From 5162726b0fbbf467176a028713e48e99d44ef2f1 Mon Sep 17 00:00:00 2001 From: support Date: Mon, 23 Nov 2026 07:20:09 +0100 Subject: [PATCH 2/2] Logo upload: pick the free name by creating the file, not by looking first Looking for a free name and creating the file were two steps, so two uploads of the same name at once could both pick it; the second then failed, and its error message, which held the server's physical path, went back to the browser. The upload now tries CreateNew on each name in turn and takes the first that succeeds. Any other failure is left to the application's error handling, so no exception text reaches the response. --- .../Controllers/BasePictureControllerTests.cs | 2 - .../Controllers/BasePictureController.cs | 46 +++++++++---------- 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs b/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs index 0e1b433a5..5926b2325 100644 --- a/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs +++ b/src/Tests/Grand.Web.Admin.Tests/Controllers/BasePictureControllerTests.cs @@ -178,8 +178,6 @@ public async Task AsyncLogoUpload_NameTaken_KeepsTheExistingFile_AndSavesUnderAF var root = new Mock(); root.Setup(r => r.PhysicalPath).Returns(folder); _mediaFileStoreMock.Setup(m => m.GetDirectoryInfo("")).Returns(root.Object); - _mediaFileStoreMock.Setup(m => m.GetFileInfo(It.IsAny())) - .ReturnsAsync((string name) => File.Exists(Path.Join(folder, name)) ? Mock.Of() : null); var file = CreateFormFile("logo.png", "image/png"); file.Setup(f => f.CopyToAsync(It.IsAny(), It.IsAny())) .Returns((Stream target, CancellationToken _) => target.WriteAsync("new"u8.ToArray()).AsTask()); diff --git a/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs b/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs index 5c7886714..8b342796a 100644 --- a/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs +++ b/src/Web/Grand.Web.AdminShared/Controllers/BasePictureController.cs @@ -97,36 +97,34 @@ public virtual async Task AsyncLogoUpload(IFormFile file) }); //the folder is shared by every store, so an existing file (another store's logo) is never replaced - fileName = await FreeFileName(fileName); - try + var (stream, savedName) = CreateUnderFreeName(filepath.PhysicalPath, fileName); + await using (stream) { - await using (var stream = new FileStream(mediaFileStore.Combine(filepath.PhysicalPath, fileName), - FileMode.CreateNew)) - { - await file.CopyToAsync(stream); - } - - return Json(new { - success = true, - imageUrl = fileName - }); - } - catch (Exception ex) - { - return Json(new { - success = false, - message = ex.Message - }); + await file.CopyToAsync(stream); } + + return Json(new { + success = true, + imageUrl = savedName + }); } - private async Task FreeFileName(string fileName) + //CreateNew refuses a taken name, also one taken by an upload running at the same time, so the next is tried + private (FileStream stream, string fileName) CreateUnderFreeName(string folder, string fileName) { var name = Path.GetFileNameWithoutExtension(fileName); var extension = Path.GetExtension(fileName); - var candidate = fileName; - for (var i = 1; await mediaFileStore.GetFileInfo(candidate) != null; i++) - candidate = $"{name}-{i}{extension}"; - return candidate; + for (var i = 0; ; i++) + { + var candidate = i == 0 ? fileName : $"{name}-{i}{extension}"; + var path = mediaFileStore.Combine(folder, candidate); + try + { + return (new FileStream(path, FileMode.CreateNew), candidate); + } + catch (IOException) when (System.IO.File.Exists(path)) + { + } + } } }