2023-05-09 15:34:36 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
package common
|
|
|
|
|
|
|
|
|
|
import (
|
2025-03-13 07:04:50 +08:00
|
|
|
"path"
|
2023-05-09 15:34:36 +08:00
|
|
|
"time"
|
|
|
|
|
|
2025-03-13 07:04:50 +08:00
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2023-05-09 15:34:36 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
|
|
|
|
"code.gitea.io/gitea/modules/httpcache"
|
|
|
|
|
"code.gitea.io/gitea/modules/httplib"
|
2025-03-13 07:04:50 +08:00
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
|
"code.gitea.io/gitea/modules/structs"
|
2024-02-27 15:12:22 +08:00
|
|
|
"code.gitea.io/gitea/services/context"
|
2023-05-09 15:34:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ServeBlob download a git.Blob
|
2025-03-13 07:04:50 +08:00
|
|
|
func ServeBlob(ctx *context.Base, repo *repo_model.Repository, filePath string, blob *git.Blob, lastModified *time.Time) error {
|
2026-03-01 21:32:35 +08:00
|
|
|
if httpcache.HandleGenericETagPrivateCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
|
2023-05-09 15:34:36 +08:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-25 17:37:48 +01:00
|
|
|
if err := repo.LoadOwner(ctx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-09 15:34:36 +08:00
|
|
|
dataRc, err := blob.DataAsync()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
2026-03-25 17:37:48 +01:00
|
|
|
defer dataRc.Close()
|
2023-05-09 15:34:36 +08:00
|
|
|
|
2026-03-25 17:37:48 +01:00
|
|
|
if lastModified == nil {
|
|
|
|
|
lastModified = new(time.Time)
|
|
|
|
|
}
|
|
|
|
|
httplib.ServeUserContentByReader(ctx.Req, ctx.Resp, blob.Size(), dataRc, httplib.ServeHeaderOptions{
|
2025-03-13 07:04:50 +08:00
|
|
|
Filename: path.Base(filePath),
|
2026-03-25 17:37:48 +01:00
|
|
|
CacheIsPublic: !repo.IsPrivate && repo.Owner.Visibility == structs.VisibleTypePublic,
|
2025-03-13 07:04:50 +08:00
|
|
|
CacheDuration: setting.StaticCacheTime,
|
2026-03-25 17:37:48 +01:00
|
|
|
LastModified: *lastModified,
|
2025-03-13 07:04:50 +08:00
|
|
|
})
|
2023-05-09 15:34:36 +08:00
|
|
|
return nil
|
|
|
|
|
}
|