summaryrefslogtreecommitdiffstats
path: root/routers/repo
diff options
context:
space:
mode:
authorslene <vslene@gmail.com>2014-05-25 20:16:11 +0800
committerslene <vslene@gmail.com>2014-05-28 14:01:41 +0800
commit4ee6bc4fcac47edeea1f1e9fd5fbfd3ef595bbca (patch)
tree8867be03be123ea8c84bc92d7d388f6ac85de058 /routers/repo
parentab13a29cb530457328cf9d3a2511c2db1d5ccaca (diff)
downloadgitea-4ee6bc4fcac47edeea1f1e9fd5fbfd3ef595bbca.tar.gz
gitea-4ee6bc4fcac47edeea1f1e9fd5fbfd3ef595bbca.zip
fix for new git api
Diffstat (limited to 'routers/repo')
-rw-r--r--routers/repo/commit.go10
-rw-r--r--routers/repo/download.go20
-rw-r--r--routers/repo/repo.go41
3 files changed, 57 insertions, 14 deletions
diff --git a/routers/repo/commit.go b/routers/repo/commit.go
index 44427b8a42..09dcaf5ead 100644
--- a/routers/repo/commit.go
+++ b/routers/repo/commit.go
@@ -85,11 +85,17 @@ func Diff(ctx *middleware.Context, params martini.Params) {
return false
}
- data, err := blob.Data()
+ dataRc, err := blob.Data()
if err != nil {
return false
}
- _, isImage := base.IsImageFile(data)
+ buf := make([]byte, 1024)
+ n, _ := dataRc.Read(buf)
+ if n > 0 {
+ buf = buf[:n]
+ }
+ dataRc.Close()
+ _, isImage := base.IsImageFile(buf)
return isImage
}
diff --git a/routers/repo/download.go b/routers/repo/download.go
index 5df78dc7d8..ac9c390b1f 100644
--- a/routers/repo/download.go
+++ b/routers/repo/download.go
@@ -5,6 +5,7 @@
package repo
import (
+ "io"
"os"
"path/filepath"
@@ -26,20 +27,31 @@ func SingleDownload(ctx *middleware.Context, params martini.Params) {
return
}
- data, err := blob.Data()
+ dataRc, err := blob.Data()
if err != nil {
ctx.Handle(500, "repo.SingleDownload(Data)", err)
return
}
- contentType, isTextFile := base.IsTextFile(data)
- _, isImageFile := base.IsImageFile(data)
+ buf := make([]byte, 1024)
+ n, _ := dataRc.Read(buf)
+ if n > 0 {
+ buf = buf[:n]
+ }
+
+ defer func() {
+ dataRc.Close()
+ }()
+
+ contentType, isTextFile := base.IsTextFile(buf)
+ _, isImageFile := base.IsImageFile(buf)
ctx.Res.Header().Set("Content-Type", contentType)
if !isTextFile && !isImageFile {
ctx.Res.Header().Set("Content-Disposition", "attachment; filename="+filepath.Base(treename))
ctx.Res.Header().Set("Content-Transfer-Encoding", "binary")
}
- ctx.Res.Write(data)
+ ctx.Res.Write(buf)
+ io.Copy(ctx.Res, dataRc)
}
func ZipDownload(ctx *middleware.Context, params martini.Params) {
diff --git a/routers/repo/repo.go b/routers/repo/repo.go
index 7769d22774..3f46332c1c 100644
--- a/routers/repo/repo.go
+++ b/routers/repo/repo.go
@@ -8,6 +8,7 @@ import (
"encoding/base64"
"errors"
"fmt"
+ "io/ioutil"
"path"
"path/filepath"
"strings"
@@ -148,7 +149,7 @@ func Single(ctx *middleware.Context, params martini.Params) {
if entry != nil && !entry.IsDir() {
blob := entry.Blob()
- if data, err := blob.Data(); err != nil {
+ if dataRc, err := blob.Data(); err != nil {
ctx.Handle(404, "repo.Single(blob.Data)", err)
} else {
ctx.Data["FileSize"] = blob.Size()
@@ -161,20 +162,32 @@ func Single(ctx *middleware.Context, params martini.Params) {
ctx.Data["FileExt"] = ext
ctx.Data["FileLink"] = rawLink + "/" + treename
- _, isTextFile := base.IsTextFile(data)
- _, isImageFile := base.IsImageFile(data)
+ buf := make([]byte, 1024)
+ n, _ := dataRc.Read(buf)
+ if n > 0 {
+ buf = buf[:n]
+ }
+
+ defer func() {
+ dataRc.Close()
+ }()
+
+ _, isTextFile := base.IsTextFile(buf)
+ _, isImageFile := base.IsImageFile(buf)
ctx.Data["FileIsText"] = isTextFile
if isImageFile {
ctx.Data["IsImageFile"] = true
} else {
+ d, _ := ioutil.ReadAll(dataRc)
+ buf = append(buf, d...)
readmeExist := base.IsMarkdownFile(blob.Name()) || base.IsReadmeFile(blob.Name())
ctx.Data["ReadmeExist"] = readmeExist
if readmeExist {
- ctx.Data["FileContent"] = string(base.RenderMarkdown(data, ""))
+ ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, ""))
} else {
if isTextFile {
- ctx.Data["FileContent"] = string(data)
+ ctx.Data["FileContent"] = string(buf)
}
}
}
@@ -218,17 +231,29 @@ func Single(ctx *middleware.Context, params martini.Params) {
if readmeFile != nil {
ctx.Data["ReadmeInSingle"] = true
ctx.Data["ReadmeExist"] = true
- if data, err := readmeFile.Data(); err != nil {
+ if dataRc, err := readmeFile.Data(); err != nil {
ctx.Handle(404, "repo.Single(readmeFile.LookupBlob)", err)
return
} else {
+
+ buf := make([]byte, 1024)
+ n, _ := dataRc.Read(buf)
+ if n > 0 {
+ buf = buf[:n]
+ }
+ defer func() {
+ dataRc.Close()
+ }()
+
ctx.Data["FileSize"] = readmeFile.Size
ctx.Data["FileLink"] = rawLink + "/" + treename
- _, isTextFile := base.IsTextFile(data)
+ _, isTextFile := base.IsTextFile(buf)
ctx.Data["FileIsText"] = isTextFile
ctx.Data["FileName"] = readmeFile.Name()
if isTextFile {
- ctx.Data["FileContent"] = string(base.RenderMarkdown(data, branchLink))
+ d, _ := ioutil.ReadAll(dataRc)
+ buf = append(buf, d...)
+ ctx.Data["FileContent"] = string(base.RenderMarkdown(buf, branchLink))
}
}
}