Merge pull request #908 from hi019/master

🐛 Make SendFile return 404 if file not found
pull/911/head
Fenny 2020-10-10 16:26:21 +02:00 committed by GitHub
commit c0efbcd28c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 4 additions and 1 deletions

5
ctx.go
View File

@ -904,6 +904,9 @@ var sendFileHandler fasthttp.RequestHandler
// The file is not compressed by default, enable this by passing a 'true' argument
// Sets the Content-Type response HTTP header field based on the filenames extension.
func (c *Ctx) SendFile(file string, compress ...bool) error {
// Save the filename, we will need it in the error message if the file isn't found
filename := file
// https://github.com/valyala/fasthttp/blob/master/fs.go#L81
sendFileOnce.Do(func() {
sendFileFS = &fasthttp.FS{
@ -953,7 +956,7 @@ func (c *Ctx) SendFile(file string, compress ...bool) error {
}
// Check for error
if status != StatusNotFound && fsStatus == StatusNotFound {
return fmt.Errorf("sendfile: file %s not found", file)
return NewError(StatusNotFound, fmt.Sprintf("sendfile: file %s not found", filename))
}
return nil
}