From 5edcae5e237877457fc10e16ddc8196d512668d6 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Mon, 9 Dec 2019 18:33:48 +0100 Subject: [PATCH 1/6] Set MIME type for text files requested via raw api --- routers/api/v1/repo/file.go | 1 + routers/repo/download.go | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/routers/api/v1/repo/file.go b/routers/api/v1/repo/file.go index 175235c5ef374..d621a74f7b639 100644 --- a/routers/api/v1/repo/file.go +++ b/routers/api/v1/repo/file.go @@ -57,6 +57,7 @@ func GetRawFile(ctx *context.APIContext) { } return } + ctx.Context.Data["IsRawApi"] = true if err = repo.ServeBlob(ctx.Context, blob); err != nil { ctx.Error(http.StatusInternalServerError, "ServeBlob", err) } diff --git a/routers/repo/download.go b/routers/repo/download.go index 6f10fe36a36b9..af439f128edb4 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -8,7 +8,9 @@ package repo import ( "fmt" "io" + "mime" "path" + "path/filepath" "strings" "code.gitea.io/gitea/modules/base" @@ -32,7 +34,11 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { // 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") { + mimeType := mime.TypeByExtension(filepath.Ext(name)) + + if base.IsTextFile(buf) && true == ctx.Data["IsRawApi"] && mimeType != "" { + ctx.Resp.Header().Set("Content-Type", mimeType) + } else 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)) From f2eaa8090827cd54e6e2edeca50a5a18eeec426a Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Tue, 10 Dec 2019 16:45:30 +0100 Subject: [PATCH 2/6] Add MIME type for all file types --- routers/repo/download.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/routers/repo/download.go b/routers/repo/download.go index af439f128edb4..380a8682cf30f 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -34,11 +34,7 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { // Google Chrome dislike commas in filenames, so let's change it to a space name = strings.Replace(name, ",", " ", -1) - mimeType := mime.TypeByExtension(filepath.Ext(name)) - - if base.IsTextFile(buf) && true == ctx.Data["IsRawApi"] && mimeType != "" { - ctx.Resp.Header().Set("Content-Type", mimeType) - } else if base.IsTextFile(buf) || ctx.QueryBool("render") { + 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)) @@ -46,6 +42,12 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name)) } + if true == ctx.Data["IsRawApi"] { + if mimeType := mime.TypeByExtension(filepath.Ext(name)); mimeType != "" { + ctx.Resp.Header().Set("Content-Type", mimeType) + } + } + _, err := ctx.Resp.Write(buf) if err != nil { return err From a162b06450259849faec9f6501fa7f22a24f7ef4 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Sat, 14 Dec 2019 14:23:46 +0100 Subject: [PATCH 3/6] Use more readable conditional --- routers/repo/download.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/download.go b/routers/repo/download.go index 380a8682cf30f..462f192b61cdf 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -42,7 +42,7 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name)) } - if true == ctx.Data["IsRawApi"] { + if ctx.Data["IsRawApi"] == true { if mimeType := mime.TypeByExtension(filepath.Ext(name)); mimeType != "" { ctx.Resp.Header().Set("Content-Type", mimeType) } From 8fe25d1bdc7627ba3c9bad519ba3f55e09abb931 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Wed, 27 Jan 2021 14:19:16 +0100 Subject: [PATCH 4/6] Resolve merge conflicts --- routers/repo/download.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/routers/repo/download.go b/routers/repo/download.go index 462f192b61cdf..83a867d7925f7 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -23,7 +23,7 @@ import ( // ServeData download file from io.Reader func ServeData(ctx *context.Context, name string, reader io.Reader) error { buf := make([]byte, 1024) - n, _ := reader.Read(buf) + n, err := reader.Read(buf) if n >= 0 { buf = buf[:n] } @@ -48,7 +48,7 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { } } - _, err := ctx.Resp.Write(buf) + _, err = ctx.Resp.Write(buf) if err != nil { return err } From 2610841d72a2a1d01f9bb8efafea0bcf84622f40 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Wed, 27 Jan 2021 14:21:36 +0100 Subject: [PATCH 5/6] clean --- routers/repo/download.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/routers/repo/download.go b/routers/repo/download.go index 83a867d7925f7..b87a658e2cb53 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -23,7 +23,7 @@ import ( // ServeData download file from io.Reader func ServeData(ctx *context.Context, name string, reader io.Reader) error { buf := make([]byte, 1024) - n, err := reader.Read(buf) + n, _ := reader.Read(buf) if n >= 0 { buf = buf[:n] } From 35c03f0babc753ebf13fca6f7b1e3d1cf6a8e442 Mon Sep 17 00:00:00 2001 From: Daniel Appelt Date: Wed, 27 Jan 2021 14:25:49 +0100 Subject: [PATCH 6/6] Resolve merge conflicts 2 This should leave just the intended change as a conflict. --- routers/repo/download.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/routers/repo/download.go b/routers/repo/download.go index b87a658e2cb53..f523371ff5c53 100644 --- a/routers/repo/download.go +++ b/routers/repo/download.go @@ -23,7 +23,10 @@ import ( // ServeData download file from io.Reader func ServeData(ctx *context.Context, name string, reader io.Reader) error { buf := make([]byte, 1024) - n, _ := reader.Read(buf) + n, err := reader.Read(buf) + if err != nil && err != io.EOF { + return err + } if n >= 0 { buf = buf[:n] }