Skip to content

Commit

Permalink
Merge branch 'master' into sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Monirzadeh authored Sep 24, 2024
2 parents 92307dc + 0128107 commit d4ea048
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func initShiori(ctx context.Context, cmd *cobra.Command) (*config.Config, *depen
cfg := config.ParseServerConfiguration(ctx, logger)
cfg.LogLevel = logger.Level.String()

if storageDirectory != "" && cfg.Storage.DataDir != "" {
if storageDirectory != "" {
logger.Warn("--storage-directory is set, overriding SHIORI_DIR.")
cfg.Storage.DataDir = storageDirectory
}
Expand Down
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)
}
2 changes: 1 addition & 1 deletion internal/view/assets/css/style.css

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion internal/view/assets/js/component/bookmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export default {
hasContent: Boolean,
hasArchive: Boolean,
hasEbook: Boolean,
modifiedAt: String,
index: Number,
ShowId: Boolean,
editMode: Boolean,
Expand Down Expand Up @@ -95,7 +96,7 @@ export default {
},
thumbnailStyleURL() {
return {
backgroundImage: `url("${this.imageURL}")`,
backgroundImage: `url("${this.imageURL}?modifiedAt=${this.modifiedAt}")`,
};
},
eventItem() {
Expand Down
1 change: 1 addition & 0 deletions internal/view/assets/js/page/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/view/assets/less/common.less
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ a {
&.list {
grid-gap: 0;
padding-bottom: 0;
grid-template-columns: minmax(0, 1000px);
grid-template-columns: auto;

.pagination-box {
padding: 16px 0;
Expand Down

0 comments on commit d4ea048

Please sign in to comment.