diff --git a/modules/base/markdown.go b/modules/base/markdown.go index c722f04b2..a98ca8cf0 100644 --- a/modules/base/markdown.go +++ b/modules/base/markdown.go @@ -51,6 +51,14 @@ func IsTextFile(data []byte) (string, bool) { return contentType, false } +func IsImageFile(data []byte) (string, bool) { + contentType := http.DetectContentType(data) + if strings.Index(contentType, "img/") != -1 { + return contentType, true + } + return contentType, false +} + func IsReadmeFile(name string) bool { name = strings.ToLower(name) if len(name) < 6 { diff --git a/routers/repo/repo.go b/routers/repo/repo.go index 3c8d24a6a..4573a3e48 100644 --- a/routers/repo/repo.go +++ b/routers/repo/repo.go @@ -120,15 +120,20 @@ func Single(ctx *middleware.Context, params martini.Params) { data := blob.Contents() _, isTextFile := base.IsTextFile(data) + _, isImageFile := base.IsImageFile(data) ctx.Data["FileIsText"] = isTextFile - readmeExist := base.IsMarkdownFile(repoFile.Name) || base.IsReadmeFile(repoFile.Name) - ctx.Data["ReadmeExist"] = readmeExist - if readmeExist { - ctx.Data["FileContent"] = string(base.RenderMarkdown(data, "")) + if isImageFile { + ctx.Data["IsImageFile"] = true } else { - if isTextFile { - ctx.Data["FileContent"] = string(data) + readmeExist := base.IsMarkdownFile(repoFile.Name) || base.IsReadmeFile(repoFile.Name) + ctx.Data["ReadmeExist"] = readmeExist + if readmeExist { + ctx.Data["FileContent"] = string(base.RenderMarkdown(data, "")) + } else { + if isTextFile { + ctx.Data["FileContent"] = string(data) + } } } } @@ -236,9 +241,9 @@ func SingleDownload(ctx *middleware.Context, params martini.Params) { data := blob.Contents() contentType, isTextFile := base.IsTextFile(data) + _, isImageFile := base.IsImageFile(data) ctx.Res.Header().Set("Content-Type", contentType) if !isTextFile { - ctx.Res.Header().Set("Content-Type", contentType) ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename)) ctx.Res.Header().Set("Content-Transfer-Encoding", "binary") } diff --git a/templates/repo/single_file.tmpl b/templates/repo/single_file.tmpl index cf398595e..9199ca91f 100644 --- a/templates/repo/single_file.tmpl +++ b/templates/repo/single_file.tmpl @@ -23,7 +23,11 @@ {{if not .FileIsText}}
{{else}} {{if .ReadmeExist}} @@ -43,4 +47,4 @@ {{end}} {{end}} - \ No newline at end of file +