Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: thumbnail should show last thumbnail in server in any condition #858

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
17 changes: 14 additions & 3 deletions internal/http/response/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"github.com/go-shiori/shiori/internal/model"
)

// SendFile sends file to client with caching header
func SendFile(c *gin.Context, storageDomain model.StorageDomain, path string) {
c.Header("Cache-Control", "public, max-age=86400")
type SendFileOptions struct {
Headers []http.Header
}

// SendFile sends file to client with caching header
func SendFile(c *gin.Context, storageDomain model.StorageDomain, path string, options *SendFileOptions) {
if !storageDomain.FileExists(path) {
c.AbortWithStatus(http.StatusNotFound)
return
Expand All @@ -24,8 +26,17 @@ func SendFile(c *gin.Context, storageDomain model.StorageDomain, path string) {
return
}

c.Header("Cache-Control", "public, max-age=86400")
c.Header("ETag", fmt.Sprintf("W/%x-%x", info.ModTime().Unix(), info.Size()))

if options != nil {
for _, header := range options.Headers {
for key, value := range header {
c.Header(key, value[0])
}
}
}

// TODO: Find a better way to send the file to the client from the FS, probably making a
// conversion between afero.Fs and http.FileSystem to use c.FileFromFS.
fileContent, err := storageDomain.FS().Open(path)
Expand Down
20 changes: 18 additions & 2 deletions internal/http/routes/bookmark.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,23 @@ func (r *BookmarkRoutes) bookmarkThumbnailHandler(c *gin.Context) {
return
}

response.SendFile(c, r.deps.Domains.Storage, model.GetThumbnailPath(bookmark))
etag := "w/" + model.GetThumbnailPath(bookmark) + "-" + bookmark.ModifiedAt

// Check if the client's ETag matches the current ETag
if c.GetHeader("If-None-Match") == etag {
c.Status(http.StatusNotModified)
return
}

options := &response.SendFileOptions{
Headers: []http.Header{
{"Cache-Control": {"no-cache , must-revalidate"}},
{"Last-Modified": {bookmark.ModifiedAt}},
{"ETag": {etag}},
},
}

response.SendFile(c, r.deps.Domains.Storage, model.GetThumbnailPath(bookmark), options)
}

func (r *BookmarkRoutes) bookmarkEbookHandler(c *gin.Context) {
Expand All @@ -185,5 +201,5 @@ func (r *BookmarkRoutes) bookmarkEbookHandler(c *gin.Context) {

// TODO: Potentially improve this
c.Header("Content-Disposition", `attachment; filename="`+bookmark.Title+`.epub"`)
response.SendFile(c, r.deps.Domains.Storage, model.GetEbookPath(bookmark))
response.SendFile(c, r.deps.Domains.Storage, model.GetEbookPath(bookmark), nil)
}
Loading