Skip to content

Logo upload: never replace a file already in the media folder - #1103

Merged
KrzysztofPajak merged 2 commits into
developfrom
fix/logo-upload-overwrite
Nov 23, 2026
Merged

KrzysztofPajak merged 2 commits into
developfrom
fix/logo-upload-overwrite

Conversation

@KrzysztofPajak

@KrzysztofPajak KrzysztofPajak commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

BasePictureController.AsyncLogoUpload (admin and store panels) wrote the logo into the media root, which every store shares, with FileMode.OpenOrCreate. An upload with an existing name, such as another store's logo.png, overwrote that file. The mode does not truncate, so a shorter upload left the end of the old file behind and corrupted the image.

Changes

  • An upload whose name is taken is saved under the next free name (logo-1.png, logo-2.png, ...) with FileMode.CreateNew; the returned imageUrl carries that name, which the settings form stores.
  • The action had two identical branches (file exists / does not exist); they are one now.

Notes

  • Uploading a logo with the same name no longer replaces the file in place for the main admin either; the store keeps pointing at the new name it was given.
  • Files already overwritten or corrupted stay as they are.

Tests

  • BasePictureControllerTests.AsyncLogoUpload_NameTaken_KeepsTheExistingFile_AndSavesUnderAFreeName (new, on a temporary folder).
  • Grand.Web.Admin.Tests (2110) pass; Grand.Web.Store builds.

Review follow-up

  • The free name is now found by creating the file: CreateNew is tried on each name in turn and an IOException on a name that exists moves to the next one, so two uploads of the same name at once both succeed.
  • The catch that returned the exception message (with the server's physical path) to the browser is gone; any other failure is left to the application's error handling. Grand.Web.Admin.Tests (2110) pass.

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.
Copilot AI balanced review requested due to automatic review settings November 23, 2026 05:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

…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.
@KrzysztofPajak
KrzysztofPajak merged commit f915d6b into develop Nov 23, 2026
9 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the fix/logo-upload-overwrite branch November 25, 2026 09:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants