Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (*DefaultCtx) SaveFile(fileheader *multipart.FileHeader, path string) error
func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error {
file, err := fileheader.Open()
if err != nil {
return fmt.Errorf("failed to open: %w", err)
return fmt.Errorf("failed to open file %s: %w", fileheader.Filename, err)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the fileader is nil, it is going to panic. you also need to add check for that. In addition to panic check, could you also unit test cases for error messages?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't err have the filename already?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

no it only shows what the error is https://pkg.go.dev/internal/oserror#pkg-variables

}
defer file.Close() //nolint:errcheck // not needed

Expand All @@ -529,25 +529,25 @@ func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path st
}

if fileheader.Size > 0 && fileheader.Size > int64(maxUploadSize) {
return fmt.Errorf("failed to read: %w", fasthttp.ErrBodyTooLarge)
return fmt.Errorf("failed to read file %s: %w", fileheader.Filename, fasthttp.ErrBodyTooLarge)
}

buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

limitedReader := io.LimitReader(file, int64(maxUploadSize)+1)
if _, err = buf.ReadFrom(limitedReader); err != nil {
return fmt.Errorf("failed to read: %w", err)
return fmt.Errorf("failed to read file %s: %w", fileheader.Filename, err)
}

if buf.Len() > maxUploadSize {
return fmt.Errorf("failed to read: %w", fasthttp.ErrBodyTooLarge)
return fmt.Errorf("failed to read file %s: %w", fileheader.Filename, fasthttp.ErrBodyTooLarge)
}

data := append([]byte(nil), buf.Bytes()...)

if err := storage.SetWithContext(c.Context(), path, data, 0); err != nil {
return fmt.Errorf("failed to store: %w", err)
return fmt.Errorf("failed to store file %s at %s: %w", fileheader.Filename, path, err)
}

return nil
Expand Down
Loading