46 lines
1.1 KiB
Go
46 lines
1.1 KiB
Go
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package common
|
|
|
|
import (
|
|
"path"
|
|
"time"
|
|
|
|
repo_model "gitea.dev/models/repo"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/httpcache"
|
|
"gitea.dev/modules/httplib"
|
|
"gitea.dev/modules/setting"
|
|
"gitea.dev/modules/structs"
|
|
"gitea.dev/services/context"
|
|
)
|
|
|
|
// ServeBlob download a git.Blob
|
|
func ServeBlob(ctx *context.Base, repo *repo_model.Repository, filePath string, blob *git.Blob, lastModified *time.Time) error {
|
|
if httpcache.HandleGenericETagPrivateCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
|
|
return nil
|
|
}
|
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
|
return err
|
|
}
|
|
|
|
dataRc, err := blob.DataAsync()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer dataRc.Close()
|
|
|
|
if lastModified == nil {
|
|
lastModified = new(time.Time)
|
|
}
|
|
httplib.ServeUserContentByReader(ctx.Req, ctx.Resp, blob.Size(), dataRc, httplib.ServeHeaderOptions{
|
|
Filename: path.Base(filePath),
|
|
CacheIsPublic: !repo.IsPrivate && repo.Owner.Visibility == structs.VisibleTypePublic,
|
|
CacheDuration: setting.StaticCacheTime,
|
|
LastModified: *lastModified,
|
|
})
|
|
return nil
|
|
}
|