mirror of https://github.com/gogits/gogs.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.4 KiB
61 lines
1.4 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package repo
|
||
|
|
||
|
import (
|
||
11 years ago
|
"io"
|
||
|
"path"
|
||
11 years ago
|
|
||
9 years ago
|
"github.com/gogits/git-module"
|
||
9 years ago
|
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/tool"
|
||
8 years ago
|
"github.com/gogits/gogs/pkg/context"
|
||
|
"github.com/gogits/gogs/pkg/setting"
|
||
11 years ago
|
)
|
||
|
|
||
8 years ago
|
func ServeData(c *context.Context, name string, reader io.Reader) error {
|
||
11 years ago
|
buf := make([]byte, 1024)
|
||
10 years ago
|
n, _ := reader.Read(buf)
|
||
8 years ago
|
if n >= 0 {
|
||
11 years ago
|
buf = buf[:n]
|
||
|
}
|
||
|
|
||
8 years ago
|
if !tool.IsTextFile(buf) {
|
||
|
if !tool.IsImageFile(buf) {
|
||
8 years ago
|
c.Resp.Header().Set("Content-Disposition", "attachment; filename=\""+name+"\"")
|
||
|
c.Resp.Header().Set("Content-Transfer-Encoding", "binary")
|
||
10 years ago
|
}
|
||
8 years ago
|
} else if !setting.Repository.EnableRawFileRenderMode || !c.QueryBool("render") {
|
||
|
c.Resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
|
||
11 years ago
|
}
|
||
8 years ago
|
c.Resp.Write(buf)
|
||
|
_, err := io.Copy(c.Resp, reader)
|
||
10 years ago
|
return err
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func ServeBlob(c *context.Context, blob *git.Blob) error {
|
||
10 years ago
|
dataRc, err := blob.Data()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
8 years ago
|
return ServeData(c, path.Base(c.Repo.TreePath), dataRc)
|
||
10 years ago
|
}
|
||
|
|
||
8 years ago
|
func SingleDownload(c *context.Context) {
|
||
|
blob, err := c.Repo.Commit.GetBlobByPath(c.Repo.TreePath)
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
if git.IsErrNotExist(err) {
|
||
8 years ago
|
c.Handle(404, "GetBlobByPath", nil)
|
||
10 years ago
|
} else {
|
||
8 years ago
|
c.Handle(500, "GetBlobByPath", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
8 years ago
|
if err = ServeBlob(c, blob); err != nil {
|
||
|
c.Handle(500, "ServeBlob", err)
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|