Fix HTTP headers for issue attachment download

- Download filename was wrong for files other than images. Example: It was `download` instead of `file.pdf`
- PDF was downloading instead of showing on browser
This commit is contained in:
Andrey Nering 2016-11-26 10:08:31 -02:00
parent 0a76d260fa
commit 638dd24cec
2 changed files with 10 additions and 12 deletions

View file

@ -336,11 +336,7 @@ func runWeb(ctx *cli.Context) error {
} }
defer fr.Close() defer fr.Close()
ctx.Header().Set("Cache-Control", "public,max-age=86400") if err = repo.ServeData(ctx, attach.Name, fr); err != nil {
ctx.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, attach.Name))
// Fix #312. Attachments with , in their name are not handled correctly by Google Chrome.
// We must put the name in " manually.
if err = repo.ServeData(ctx, "\""+attach.Name+"\"", fr); err != nil {
ctx.Handle(500, "ServeData", err) ctx.Handle(500, "ServeData", err)
return return
} }

View file

@ -5,8 +5,8 @@
package repo package repo
import ( import (
"fmt"
"io" "io"
"path"
"code.gitea.io/git" "code.gitea.io/git"
@ -22,14 +22,16 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error {
buf = buf[:n] buf = buf[:n]
} }
if !base.IsTextFile(buf) { ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400")
if !base.IsImageFile(buf) {
ctx.Resp.Header().Set("Content-Disposition", "attachment; filename=\""+path.Base(ctx.Repo.TreePath)+"\"") if base.IsTextFile(buf) || ctx.QueryBool("render") {
ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary")
}
} else if !ctx.QueryBool("render") {
ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8") ctx.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
} else if base.IsImageFile(buf) || base.IsPDFFile(buf) {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`inline; filename="%s"`, name))
} else {
ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name))
} }
ctx.Resp.Write(buf) ctx.Resp.Write(buf)
_, err := io.Copy(ctx.Resp, reader) _, err := io.Copy(ctx.Resp, reader)
return err return err