Skip to content

Commit

Permalink
Merge pull request #270 from andreynering/gitea/http-headers-download
Browse files Browse the repository at this point in the history
Fix HTTP headers for issue attachment download
  • Loading branch information
andreynering authored Nov 27, 2016
2 parents 94da472 + d647d02 commit c664ffd
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
6 changes: 1 addition & 5 deletions cmd/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,7 @@ func runWeb(ctx *cli.Context) error {
}
defer fr.Close()

ctx.Header().Set("Cache-Control", "public,max-age=86400")
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 {
if err = repo.ServeData(ctx, attach.Name, fr); err != nil {
ctx.Handle(500, "ServeData", err)
return
}
Expand Down
20 changes: 13 additions & 7 deletions routers/repo/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
package repo

import (
"fmt"
"io"
"path"
"strings"

"code.gitea.io/git"

Expand All @@ -22,14 +23,19 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error {
buf = buf[:n]
}

if !base.IsTextFile(buf) {
if !base.IsImageFile(buf) {
ctx.Resp.Header().Set("Content-Disposition", "attachment; filename=\""+path.Base(ctx.Repo.TreePath)+"\"")
ctx.Resp.Header().Set("Content-Transfer-Encoding", "binary")
}
} else if !ctx.QueryBool("render") {
ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400")

// Google Chrome dislike commas in filenames, so let's change it to a space
name = strings.Replace(name, ",", " ", -1)

if base.IsTextFile(buf) || ctx.QueryBool("render") {
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)
_, err := io.Copy(ctx.Resp, reader)
return err
Expand Down

0 comments on commit c664ffd

Please sign in to comment.