fix: Improve error handling in file download API (#11127)

This commit is contained in:
KOMATA 2025-12-01 09:40:48 +08:00 committed by GitHub
parent 997b820a42
commit 508ac2ae84
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -525,9 +525,14 @@ func (b *BaseApi) Download(c *gin.Context) {
file, err := os.Open(filePath) file, err := os.Open(filePath)
if err != nil { if err != nil {
helper.InternalServer(c, err) helper.InternalServer(c, err)
return
} }
defer file.Close() defer file.Close()
info, _ := file.Stat() info, err := file.Stat()
if err != nil {
helper.InternalServer(c, err)
return
}
c.Header("Content-Length", strconv.FormatInt(info.Size(), 10)) c.Header("Content-Length", strconv.FormatInt(info.Size(), 10))
c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(info.Name())) c.Header("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(info.Name()))
http.ServeContent(c.Writer, c.Request, info.Name(), info.ModTime(), file) http.ServeContent(c.Writer, c.Request, info.Name(), info.ModTime(), file)