summaryrefslogtreecommitdiffstats
path: root/routers
diff options
context:
space:
mode:
Diffstat (limited to 'routers')
-rw-r--r--routers/repo/attachment.go20
-rw-r--r--routers/repo/download.go13
2 files changed, 23 insertions, 10 deletions
diff --git a/routers/repo/attachment.go b/routers/repo/attachment.go
index a896e4a501..f53e7450ae 100644
--- a/routers/repo/attachment.go
+++ b/routers/repo/attachment.go
@@ -10,6 +10,7 @@ import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/context"
+ "code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/storage"
@@ -124,21 +125,25 @@ func GetAttachment(ctx *context.Context) {
}
}
+ if err := attach.IncreaseDownloadCount(); err != nil {
+ ctx.ServerError("IncreaseDownloadCount", err)
+ return
+ }
+
if setting.Attachment.ServeDirect {
//If we have a signed url (S3, object storage), redirect to this directly.
u, err := storage.Attachments.URL(attach.RelativePath(), attach.Name)
if u != nil && err == nil {
- if err := attach.IncreaseDownloadCount(); err != nil {
- ctx.ServerError("Update", err)
- return
- }
-
ctx.Redirect(u.String())
return
}
}
+ if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+attach.UUID+`"`) {
+ return
+ }
+
//If we have matched and access to release or issue
fr, err := storage.Attachments.Open(attach.RelativePath())
if err != nil {
@@ -147,11 +152,6 @@ func GetAttachment(ctx *context.Context) {
}
defer fr.Close()
- if err := attach.IncreaseDownloadCount(); err != nil {
- ctx.ServerError("Update", err)
- return
- }
-
if err = ServeData(ctx, attach.Name, attach.Size, fr); err != nil {
ctx.ServerError("ServeData", err)
return
diff --git a/routers/repo/download.go b/routers/repo/download.go
index 63a9ca47d7..1eedec8cb1 100644
--- a/routers/repo/download.go
+++ b/routers/repo/download.go
@@ -15,6 +15,7 @@ import (
"code.gitea.io/gitea/modules/charset"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
+ "code.gitea.io/gitea/modules/httpcache"
"code.gitea.io/gitea/modules/lfs"
"code.gitea.io/gitea/modules/log"
)
@@ -31,6 +32,7 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
}
ctx.Resp.Header().Set("Cache-Control", "public,max-age=86400")
+
if size >= 0 {
ctx.Resp.Header().Set("Content-Length", fmt.Sprintf("%d", size))
} else {
@@ -71,6 +73,10 @@ func ServeData(ctx *context.Context, name string, size int64, reader io.Reader)
// ServeBlob download a git.Blob
func ServeBlob(ctx *context.Context, blob *git.Blob) error {
+ if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`) {
+ return nil
+ }
+
dataRc, err := blob.DataAsync()
if err != nil {
return err
@@ -86,6 +92,10 @@ func ServeBlob(ctx *context.Context, blob *git.Blob) error {
// ServeBlobOrLFS download a git.Blob redirecting to LFS if necessary
func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
+ if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`) {
+ return nil
+ }
+
dataRc, err := blob.DataAsync()
if err != nil {
return err
@@ -102,6 +112,9 @@ func ServeBlobOrLFS(ctx *context.Context, blob *git.Blob) error {
if meta == nil {
return ServeBlob(ctx, blob)
}
+ if httpcache.HandleGenericETagCache(ctx.Req, ctx.Resp, `"`+pointer.Oid+`"`) {
+ return nil
+ }
lfsDataRc, err := lfs.ReadMetaObject(meta.Pointer)
if err != nil {
return err