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.
132 lines
3.3 KiB
132 lines
3.3 KiB
11 years ago
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
||
6 years ago
|
// Copyright 2018 The Gitea Authors. All rights reserved.
|
||
11 years ago
|
// Use of this source code is governed by a MIT-style
|
||
|
// license that can be found in the LICENSE file.
|
||
|
|
||
|
package repo
|
||
|
|
||
|
import (
|
||
8 years ago
|
"code.gitea.io/gitea/modules/context"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/git"
|
||
4 years ago
|
"code.gitea.io/gitea/modules/httpcache"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/lfs"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
3 years ago
|
"code.gitea.io/gitea/routers/common"
|
||
11 years ago
|
)
|
||
|
|
||
6 years ago
|
// ServeBlobOrLFS download a git.Blob redirecting to LFS if necessary
|
||
|
func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
|
||
4 years ago
|
if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`) {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
6 years ago
|
dataRc, err := blob.DataAsync()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
4 years ago
|
closed := false
|
||
5 years ago
|
defer func() {
|
||
4 years ago
|
if closed {
|
||
|
return
|
||
|
}
|
||
5 years ago
|
if err = dataRc.Close(); err != nil {
|
||
|
log.Error("ServeBlobOrLFS: Close: %v", err)
|
||
|
}
|
||
|
}()
|
||
6 years ago
|
|
||
4 years ago
|
pointer, _ := lfs.ReadPointer(dataRc)
|
||
|
if pointer.IsValid() {
|
||
|
meta, _ := ctx.Repo.Repository.GetLFSMetaObjectByOid(pointer.Oid)
|
||
6 years ago
|
if meta == nil {
|
||
4 years ago
|
if err = dataRc.Close(); err != nil {
|
||
|
log.Error("ServeBlobOrLFS: Close: %v", err)
|
||
|
}
|
||
|
closed = true
|
||
3 years ago
|
return common.ServeBlob(ctx, blob)
|
||
6 years ago
|
}
|
||
4 years ago
|
if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+pointer.Oid+`"`) {
|
||
|
return nil
|
||
|
}
|
||
4 years ago
|
lfsDataRc, err := lfs.ReadMetaObject(meta.Pointer)
|
||
6 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
5 years ago
|
defer func() {
|
||
|
if err = lfsDataRc.Close(); err != nil {
|
||
|
log.Error("ServeBlobOrLFS: Close: %v", err)
|
||
|
}
|
||
|
}()
|
||
3 years ago
|
return common.ServeData(ctx, ctx.Repo.TreePath, meta.Size, lfsDataRc)
|
||
6 years ago
|
}
|
||
4 years ago
|
if err = dataRc.Close(); err != nil {
|
||
|
log.Error("ServeBlobOrLFS: Close: %v", err)
|
||
|
}
|
||
|
closed = true
|
||
6 years ago
|
|
||
3 years ago
|
return common.ServeBlob(ctx, blob)
|
||
6 years ago
|
}
|
||
|
|
||
8 years ago
|
// SingleDownload download a file by repos path
|
||
9 years ago
|
func SingleDownload(ctx *context.Context) {
|
||
8 years ago
|
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
|
||
10 years ago
|
if err != nil {
|
||
9 years ago
|
if git.IsErrNotExist(err) {
|
||
7 years ago
|
ctx.NotFound("GetBlobByPath", nil)
|
||
10 years ago
|
} else {
|
||
7 years ago
|
ctx.ServerError("GetBlobByPath", err)
|
||
10 years ago
|
}
|
||
|
return
|
||
|
}
|
||
3 years ago
|
if err = common.ServeBlob(ctx, blob); err != nil {
|
||
7 years ago
|
ctx.ServerError("ServeBlob", err)
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
6 years ago
|
|
||
6 years ago
|
// SingleDownloadOrLFS download a file by repos path redirecting to LFS if necessary
|
||
|
func SingleDownloadOrLFS(ctx *context.Context) {
|
||
|
blob, err := ctx.Repo.Commit.GetBlobByPath(ctx.Repo.TreePath)
|
||
|
if err != nil {
|
||
|
if git.IsErrNotExist(err) {
|
||
|
ctx.NotFound("GetBlobByPath", nil)
|
||
|
} else {
|
||
|
ctx.ServerError("GetBlobByPath", err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
if err = ServeBlobOrLFS(ctx, blob); err != nil {
|
||
|
ctx.ServerError("ServeBlobOrLFS", err)
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
// DownloadByID download a file by sha1 ID
|
||
|
func DownloadByID(ctx *context.Context) {
|
||
|
blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params("sha"))
|
||
|
if err != nil {
|
||
|
if git.IsErrNotExist(err) {
|
||
|
ctx.NotFound("GetBlob", nil)
|
||
|
} else {
|
||
|
ctx.ServerError("GetBlob", err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
3 years ago
|
if err = common.ServeBlob(ctx, blob); err != nil {
|
||
6 years ago
|
ctx.ServerError("ServeBlob", err)
|
||
|
}
|
||
|
}
|
||
6 years ago
|
|
||
|
// DownloadByIDOrLFS download a file by sha1 ID taking account of LFS
|
||
|
func DownloadByIDOrLFS(ctx *context.Context) {
|
||
|
blob, err := ctx.Repo.GitRepo.GetBlob(ctx.Params("sha"))
|
||
|
if err != nil {
|
||
|
if git.IsErrNotExist(err) {
|
||
|
ctx.NotFound("GetBlob", nil)
|
||
|
} else {
|
||
|
ctx.ServerError("GetBlob", err)
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
if err = ServeBlobOrLFS(ctx, blob); err != nil {
|
||
|
ctx.ServerError("ServeBlob", err)
|
||
|
}
|
||
|
}
|