From 638dd24cec6f2951ecd4550165b858138c4c3f40 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 26 Nov 2016 10:08:31 -0200 Subject: [PATCH 1/2] 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 --- cmd/web.go | 6 +----- routers/repo/download.go | 16 +++++++++------- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/cmd/web.go b/cmd/web.go index 3d0c97779..f5553cdf0 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -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 } diff --git a/routers/repo/download.go b/routers/repo/download.go index 3adab315d..aba0971a4 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -5,8 +5,8 @@ package repo import ( + "fmt" "io" - "path" "code.gitea.io/git" @@ -22,14 +22,16 @@ 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") + + 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 From d647d02c2f9816effd50b2eea45aa65fb1b4047c Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Sat, 26 Nov 2016 11:26:03 -0200 Subject: [PATCH 2/2] Fix Chrome not liking commas --- routers/repo/download.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/routers/repo/download.go b/routers/repo/download.go index aba0971a4..85e9fc64c 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -7,6 +7,7 @@ package repo import ( "fmt" "io" + "strings" "code.gitea.io/git" @@ -24,6 +25,9 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { 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) {