From 202222c3c4df9fe9267a3978feedd0416e30db56 Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Fri, 15 Mar 2024 15:46:24 +0330 Subject: [PATCH 1/6] force to not use cache in ui for thumbnail fix #857 --- bun.lockb | Bin 18693 -> 18693 bytes internal/view/assets/js/component/bookmark.js | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/bun.lockb b/bun.lockb index be93a5ff0ca087880603bcbaf3607f47d86b1667..f1294b59ecfb172f1e78b7483c381f6c82e45395 100755 GIT binary patch delta 322 zcmW;AD-42Q07c>b!x1Sc_{0ZjG#Z`BNQ6ct5{Wj92H1>7B4cwS(j*d%Mx(PxWO8r$ zc6OpzM6t+!st`$|x}A0(rnIHb04JlehW2Pd6&=T?CDn#F0j;Pp!U<_jH)EU=ebVg& z=S*L8H^sToH{H*0uJl8X2F{H()Sly{w584hC!_Kb?a_iND;%GeR5Nh`T2aHo32AM) PNiONhSL)pl>xcOVpSx9l delta 322 zcmW;AD-42Q07YRLjz~d&eBuK%8ja3mBtjz+i9{Pl18hbkk?GusG>Jr`(daA^nciEz zot-F_qFB0oHK~=EdKm5onL*RMl~OT2Rr$31~@;K2At0x*Xu_>4UC@ zI0yQq>k-b8zUXF*bE0p$GjPtdruGCUr44naI2l!DXpiPpo#Xhlpu)rnXh{tVC#03- PCRbTcK63lEUEIwde~nd5 diff --git a/internal/view/assets/js/component/bookmark.js b/internal/view/assets/js/component/bookmark.js index 9b4c6a36c..d931c4db7 100644 --- a/internal/view/assets/js/component/bookmark.js +++ b/internal/view/assets/js/component/bookmark.js @@ -94,8 +94,9 @@ export default { return this.excerpt !== "" && !this.thumbnailVisible && !this.HideExcerpt; }, thumbnailStyleURL() { + var cacheBuster = new Date().getTime(); return { - backgroundImage: `url("${this.imageURL}")`, + backgroundImage: `url("${this.imageURL}?cache=${cacheBuster}")`, }; }, eventItem() { From c91ce2c8853f1a58d31e3e0584774af753c9410f Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Wed, 26 Jun 2024 22:24:50 +0330 Subject: [PATCH 2/6] use modifiedAt instead of current time --- internal/view/assets/js/component/bookmark.js | 4 ++-- internal/view/assets/js/page/home.js | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/internal/view/assets/js/component/bookmark.js b/internal/view/assets/js/component/bookmark.js index d931c4db7..0e21b6f5c 100644 --- a/internal/view/assets/js/component/bookmark.js +++ b/internal/view/assets/js/component/bookmark.js @@ -51,6 +51,7 @@ export default { hasContent: Boolean, hasArchive: Boolean, hasEbook: Boolean, + modifiedAt: String, index: Number, ShowId: Boolean, editMode: Boolean, @@ -94,9 +95,8 @@ export default { return this.excerpt !== "" && !this.thumbnailVisible && !this.HideExcerpt; }, thumbnailStyleURL() { - var cacheBuster = new Date().getTime(); return { - backgroundImage: `url("${this.imageURL}?cache=${cacheBuster}")`, + backgroundImage: `url("${this.imageURL}?cache=${this.modifiedAt}")`, }; }, eventItem() { diff --git a/internal/view/assets/js/page/home.js b/internal/view/assets/js/page/home.js index da26d6afc..cd4623eea 100644 --- a/internal/view/assets/js/page/home.js +++ b/internal/view/assets/js/page/home.js @@ -48,6 +48,7 @@ var template = ` :excerpt="book.excerpt" :public="book.public" :imageURL="book.imageURL" + :modifiedAt="book.modifiedAt" :hasContent="book.hasContent" :hasArchive="book.hasArchive" :hasEbook="book.hasEbook" From 56a79a4684fe5034e9b5b091d8eb72f0fcf009eb Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:18:32 +0330 Subject: [PATCH 3/6] now can define custom Header for SendFile --- internal/http/response/file.go | 17 ++++++++++++++--- internal/http/routes/bookmark.go | 11 +++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/internal/http/response/file.go b/internal/http/response/file.go index d0704a8de..d5329ea4a 100644 --- a/internal/http/response/file.go +++ b/internal/http/response/file.go @@ -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 @@ -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) diff --git a/internal/http/routes/bookmark.go b/internal/http/routes/bookmark.go index ae2cc0905..41a96a26c 100644 --- a/internal/http/routes/bookmark.go +++ b/internal/http/routes/bookmark.go @@ -165,7 +165,14 @@ func (r *BookmarkRoutes) bookmarkThumbnailHandler(c *gin.Context) { return } - response.SendFile(c, r.deps.Domains.Storage, model.GetThumbnailPath(bookmark)) + options := &response.SendFileOptions{ + Headers: []http.Header{ + {"Last-Modified": {bookmark.ModifiedAt}}, + {"ETag": {"w/" + model.GetThumbnailPath(bookmark) + "-" + bookmark.ModifiedAt}}, + }, + } + + response.SendFile(c, r.deps.Domains.Storage, model.GetThumbnailPath(bookmark), options) } func (r *BookmarkRoutes) bookmarkEbookHandler(c *gin.Context) { @@ -185,5 +192,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) } From a0b01f944ab538f4cec079f458047d6eec9f166b Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Tue, 2 Jul 2024 20:48:45 +0330 Subject: [PATCH 4/6] remvoe use of cacheBuster --- internal/view/assets/js/component/bookmark.js | 3 +-- internal/view/assets/js/page/home.js | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/internal/view/assets/js/component/bookmark.js b/internal/view/assets/js/component/bookmark.js index 0e21b6f5c..9b4c6a36c 100644 --- a/internal/view/assets/js/component/bookmark.js +++ b/internal/view/assets/js/component/bookmark.js @@ -51,7 +51,6 @@ export default { hasContent: Boolean, hasArchive: Boolean, hasEbook: Boolean, - modifiedAt: String, index: Number, ShowId: Boolean, editMode: Boolean, @@ -96,7 +95,7 @@ export default { }, thumbnailStyleURL() { return { - backgroundImage: `url("${this.imageURL}?cache=${this.modifiedAt}")`, + backgroundImage: `url("${this.imageURL}")`, }; }, eventItem() { diff --git a/internal/view/assets/js/page/home.js b/internal/view/assets/js/page/home.js index cd4623eea..da26d6afc 100644 --- a/internal/view/assets/js/page/home.js +++ b/internal/view/assets/js/page/home.js @@ -48,7 +48,6 @@ var template = ` :excerpt="book.excerpt" :public="book.public" :imageURL="book.imageURL" - :modifiedAt="book.modifiedAt" :hasContent="book.hasContent" :hasArchive="book.hasArchive" :hasEbook="book.hasEbook" From 7fb272183d81670c2e1757258d5060e004381703 Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Wed, 3 Jul 2024 02:49:34 +0330 Subject: [PATCH 5/6] send 304 to the user if file not change --- internal/http/routes/bookmark.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/internal/http/routes/bookmark.go b/internal/http/routes/bookmark.go index 41a96a26c..e0073dcdd 100644 --- a/internal/http/routes/bookmark.go +++ b/internal/http/routes/bookmark.go @@ -165,10 +165,19 @@ func (r *BookmarkRoutes) bookmarkThumbnailHandler(c *gin.Context) { return } + 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": {"w/" + model.GetThumbnailPath(bookmark) + "-" + bookmark.ModifiedAt}}, + {"ETag": {etag}}, }, } From 3e5f39ca32a2c21d3e8390bc1cab26ed7dac9b5c Mon Sep 17 00:00:00 2001 From: monirzadeh <25131576+Monirzadeh@users.noreply.github.com> Date: Fri, 6 Sep 2024 19:17:50 +0330 Subject: [PATCH 6/6] back to queryparameter --- internal/view/assets/js/component/bookmark.js | 3 ++- internal/view/assets/js/page/home.js | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/view/assets/js/component/bookmark.js b/internal/view/assets/js/component/bookmark.js index 9b4c6a36c..952ab129f 100644 --- a/internal/view/assets/js/component/bookmark.js +++ b/internal/view/assets/js/component/bookmark.js @@ -51,6 +51,7 @@ export default { hasContent: Boolean, hasArchive: Boolean, hasEbook: Boolean, + modifiedAt: String, index: Number, ShowId: Boolean, editMode: Boolean, @@ -95,7 +96,7 @@ export default { }, thumbnailStyleURL() { return { - backgroundImage: `url("${this.imageURL}")`, + backgroundImage: `url("${this.imageURL}?modifiedAt=${this.modifiedAt}")`, }; }, eventItem() { diff --git a/internal/view/assets/js/page/home.js b/internal/view/assets/js/page/home.js index 05721268b..9d703df81 100644 --- a/internal/view/assets/js/page/home.js +++ b/internal/view/assets/js/page/home.js @@ -48,6 +48,7 @@ var template = ` :excerpt="book.excerpt" :public="book.public" :imageURL="book.imageURL" + :modifiedAt="book.modifiedAt" :hasContent="book.hasContent" :hasArchive="book.hasArchive" :hasEbook="book.hasEbook"