-
-
Notifications
You must be signed in to change notification settings - Fork 2k
🔥 feat: Add support for RootDir and RootFs #4035
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e5d4bf3
06e9506
27bd252
2ddecf0
6722830
030c269
ca7e850
9e92226
09cb0c8
0d76119
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,10 +7,14 @@ package fiber | |||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||
| "crypto/tls" | ||||||||||||||||||||||||||||||
| "errors" | ||||||||||||||||||||||||||||||
| "fmt" | ||||||||||||||||||||||||||||||
| "io" | ||||||||||||||||||||||||||||||
| "io/fs" | ||||||||||||||||||||||||||||||
| "maps" | ||||||||||||||||||||||||||||||
| "mime/multipart" | ||||||||||||||||||||||||||||||
| "os" | ||||||||||||||||||||||||||||||
| "path/filepath" | ||||||||||||||||||||||||||||||
| "strconv" | ||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||
| "sync/atomic" | ||||||||||||||||||||||||||||||
|
|
@@ -511,12 +515,57 @@ func (c *DefaultCtx) IsPreflight() bool { | |||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // SaveFile saves any multipart file to disk. | ||||||||||||||||||||||||||||||
| func (*DefaultCtx) SaveFile(fileheader *multipart.FileHeader, path string) error { | ||||||||||||||||||||||||||||||
| return fasthttp.SaveMultipartFile(fileheader, path) | ||||||||||||||||||||||||||||||
| func (c *DefaultCtx) SaveFile(fileheader *multipart.FileHeader, path string) error { | ||||||||||||||||||||||||||||||
| normalized, err := validateUploadPath(path) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if c.app.config.RootFs != nil { | ||||||||||||||||||||||||||||||
| fsPath := storageUploadPath(c.app.config.uploadRootFSPrefix, normalized.slashPath) | ||||||||||||||||||||||||||||||
| err = ensureNoSymlinkFS(c.app.config.RootFs, fsPath) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
gaby marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return saveMultipartFileToFS(fileheader, fsPath, c.app.config.uploadRootFSWriter) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| fullPath := normalized.osPath | ||||||||||||||||||||||||||||||
| if root := c.app.config.uploadRootDir; root != "" { | ||||||||||||||||||||||||||||||
| fullPath = filepath.Join(root, normalized.osPath) | ||||||||||||||||||||||||||||||
| err = ensureUploadPathWithinRoot(c.app.config.uploadRootEval, fullPath) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return fasthttp.SaveMultipartFile(fileheader, fullPath) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // SaveFileToStorage saves any multipart file to an external storage system. | ||||||||||||||||||||||||||||||
| func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error { | ||||||||||||||||||||||||||||||
| normalized, err := validateUploadPath(path) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if c.app.config.RootFs != nil { | ||||||||||||||||||||||||||||||
| fsPath := storageUploadPath(c.app.config.uploadRootFSPrefix, normalized.slashPath) | ||||||||||||||||||||||||||||||
| err = ensureNoSymlinkFS(c.app.config.RootFs, fsPath) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if root := c.app.config.uploadRootDir; root != "" { | ||||||||||||||||||||||||||||||
| fullPath := filepath.Join(root, filepath.FromSlash(normalized.slashPath)) | ||||||||||||||||||||||||||||||
| err = ensureUploadPathWithinRoot(c.app.config.uploadRootEval, fullPath) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return err | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| storagePath := storageUploadPath(c.app.config.uploadRootPath, normalized.slashPath) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| file, err := fileheader.Open() | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to open: %w", err) | ||||||||||||||||||||||||||||||
|
|
@@ -546,13 +595,52 @@ func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path st | |||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| data := append([]byte(nil), buf.Bytes()...) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| if err := storage.SetWithContext(c.Context(), path, data, 0); err != nil { | ||||||||||||||||||||||||||||||
| if err := storage.SetWithContext(c.Context(), storagePath, data, 0); err != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to store: %w", err) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| func saveMultipartFileToFS( | ||||||||||||||||||||||||||||||
| fileheader *multipart.FileHeader, | ||||||||||||||||||||||||||||||
| path string, | ||||||||||||||||||||||||||||||
| fsys interface { | ||||||||||||||||||||||||||||||
| fs.FS | ||||||||||||||||||||||||||||||
| OpenFile(name string, flag int, perm fs.FileMode) (fs.File, error) | ||||||||||||||||||||||||||||||
| }, | ||||||||||||||||||||||||||||||
| ) error { | ||||||||||||||||||||||||||||||
| file, err := fileheader.Open() | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to open multipart file: %w", err) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| defer file.Close() //nolint:errcheck // not needed | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| dst, err := fsys.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644) | ||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to open upload destination: %w", err) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| writer, ok := dst.(io.Writer) | ||||||||||||||||||||||||||||||
| if !ok { | ||||||||||||||||||||||||||||||
| closeErr := dst.Close() | ||||||||||||||||||||||||||||||
| if closeErr != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to close upload destination: %w", closeErr) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return errors.New("failed to open upload destination for write") | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| if _, err = io.Copy(writer, file); err != nil { | ||||||||||||||||||||||||||||||
| closeErr := dst.Close() | ||||||||||||||||||||||||||||||
| if closeErr != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to close upload destination: %w", closeErr) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to copy upload data: %w", err) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
Comment on lines
+631
to
+637
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Original copy error is lost when close also fails. When Proposed fix to preserve both errors if _, err = io.Copy(writer, file); err != nil {
closeErr := dst.Close()
if closeErr != nil {
- return fmt.Errorf("failed to close upload destination: %w", closeErr)
+ return fmt.Errorf("failed to copy upload data: %w (also failed to close: %v)", err, closeErr)
}
return fmt.Errorf("failed to copy upload data: %w", err)
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||
| if err := dst.Close(); err != nil { | ||||||||||||||||||||||||||||||
| return fmt.Errorf("failed to close upload destination: %w", err) | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
| return nil | ||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| // Secure returns whether a secure connection was established. | ||||||||||||||||||||||||||||||
| func (c *DefaultCtx) Secure() bool { | ||||||||||||||||||||||||||||||
| return c.Protocol() == schemeHTTPS | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.