fix(context): only chmod newly created dirs in SaveUploadedFile

Fixes #4622 - SaveUploadedFile no longer attempts to modify
permissions on existing directories, which breaks saving to /tmp
and other system directories.

- Check if directory exists before calling os.MkdirAll
- Only call os.Chmod if directory was newly created
- Preserves backward compatibility for newly created dirs
- Allows saving files to existing system directories

This regression was introduced in v1.12.0 when chmod support
was added, but was incorrectly applied to all directories.

BREAKING CHANGE REVERTED: Restores v1.10.1 behavior for existing dirs

Co-authored-by: Nghia Nguyen <nghiack7@users.noreply.github.com>
This commit is contained in:
Nghĩa Nguyễn Ngọc 2026-05-09 10:02:46 +07:00
parent 5f4f964325
commit c92f4bf410

View File

@ -728,11 +728,17 @@ func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string, perm
mode = perm[0]
}
dir := filepath.Dir(dst)
// Check if directory exists to only chmod newly created directories
_, statErr := os.Stat(dir)
dirExists := !os.IsNotExist(statErr)
if err = os.MkdirAll(dir, mode); err != nil {
return err
}
if err = os.Chmod(dir, mode); err != nil {
return err
// Only chmod if directory was newly created (fixes #4622)
if !dirExists {
if err = os.Chmod(dir, mode); err != nil {
return err
}
}
out, err := os.Create(dst)