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.

serve.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package common
  4. import (
  5. "io"
  6. "time"
  7. "code.gitea.io/gitea/modules/context"
  8. "code.gitea.io/gitea/modules/git"
  9. "code.gitea.io/gitea/modules/httpcache"
  10. "code.gitea.io/gitea/modules/httplib"
  11. "code.gitea.io/gitea/modules/log"
  12. )
  13. // ServeBlob download a git.Blob
  14. func ServeBlob(ctx *context.Base, filePath string, blob *git.Blob, lastModified *time.Time) error {
  15. if httpcache.HandleGenericETagTimeCache(ctx.Req, ctx.Resp, `"`+blob.ID.String()+`"`, lastModified) {
  16. return nil
  17. }
  18. dataRc, err := blob.DataAsync()
  19. if err != nil {
  20. return err
  21. }
  22. defer func() {
  23. if err = dataRc.Close(); err != nil {
  24. log.Error("ServeBlob: Close: %v", err)
  25. }
  26. }()
  27. httplib.ServeContentByReader(ctx.Req, ctx.Resp, filePath, blob.Size(), dataRc)
  28. return nil
  29. }
  30. func ServeContentByReader(ctx *context.Base, filePath string, size int64, reader io.Reader) {
  31. httplib.ServeContentByReader(ctx.Req, ctx.Resp, filePath, size, reader)
  32. }
  33. func ServeContentByReadSeeker(ctx *context.Base, filePath string, modTime *time.Time, reader io.ReadSeeker) {
  34. httplib.ServeContentByReadSeeker(ctx.Req, ctx.Resp, filePath, modTime, reader)
  35. }