code.gitea.io/gitea@v1.21.7/routers/common/serve.go (about)

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