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..f523371ff5c53 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" @@ -21,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] } @@ -40,7 +45,13 @@ func ServeData(ctx *context.Context, name string, reader io.Reader) error { ctx.Resp.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, name)) } - _, err := ctx.Resp.Write(buf) + if ctx.Data["IsRawApi"] == true { + 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 }