mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-09-06 14:37:49 +08:00
add etag modified headers
This commit is contained in:
parent
463ab6b6f3
commit
8ba3d0f18a
2 changed files with 15 additions and 1 deletions
3
main.go
3
main.go
|
@ -45,7 +45,8 @@ func main() {
|
|||
|
||||
router.Use(cors.New(cors.Config{
|
||||
AllowMethods: []string{"GET", "POST", "PUT", "PATCH", "DELETE", "HEAD", "OPTIONS"},
|
||||
AllowHeaders: []string{"Origin", "Content-Length", "Content-Type"},
|
||||
AllowHeaders: []string{"Content-Length", "Content-Type", "If-Modified-Since", "Range"},
|
||||
ExposeHeaders: []string{"Content-Length", "Content-Range"},
|
||||
AllowCredentials: true,
|
||||
AllowOriginFunc: func(origin string) bool {
|
||||
return true
|
||||
|
|
|
@ -10,11 +10,13 @@ import (
|
|||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/divyam234/teldrive/cache"
|
||||
"github.com/divyam234/teldrive/models"
|
||||
"github.com/divyam234/teldrive/schemas"
|
||||
"github.com/divyam234/teldrive/utils"
|
||||
"github.com/divyam234/teldrive/utils/md5"
|
||||
|
||||
"github.com/divyam234/teldrive/types"
|
||||
|
||||
|
@ -318,6 +320,15 @@ func (fs *FileService) GetFileStream(c *gin.Context) {
|
|||
|
||||
file := res.(*schemas.FileOutFull)
|
||||
|
||||
ifModifiedSinceHeader := r.Header.Get("If-Modified-Since")
|
||||
if ifModifiedSinceHeader != "" {
|
||||
ifModifiedSinceTime, err := time.Parse(http.TimeFormat, ifModifiedSinceHeader)
|
||||
if err == nil && file.UpdatedAt.Before(ifModifiedSinceTime.Add(1*time.Second)) {
|
||||
w.WriteHeader(http.StatusNotModified)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
|
||||
var start, end int64
|
||||
|
@ -345,6 +356,8 @@ func (fs *FileService) GetFileStream(c *gin.Context) {
|
|||
w.Header().Set("Content-Type", file.MimeType)
|
||||
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(contentLength, 10))
|
||||
w.Header().Set("E-Tag", md5.FromString(file.ID+strconv.FormatInt(file.Size, 10)))
|
||||
w.Header().Set("Last-Modified", file.UpdatedAt.UTC().Format(http.TimeFormat))
|
||||
|
||||
disposition := "inline"
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue