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

[chore] Do cache-control in a less silly way to avoid writing header twice #1481

Merged
merged 3 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions internal/api/fileserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package api
import (
"github.com/gin-gonic/gin"
"github.com/superseriousbusiness/gotosocial/internal/api/fileserver"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/middleware"
"github.com/superseriousbusiness/gotosocial/internal/processing"
"github.com/superseriousbusiness/gotosocial/internal/router"
Expand All @@ -30,21 +31,31 @@ type Fileserver struct {
fileserver *fileserver.Module
}

// maxAge returns an appropriate max-age value for the
// storage method that's being used.
//
// The default max-age is very long to reflect that we
// never host different files at the same URL (since
// ULIDs are generated per piece of media), so we can
// easily prevent clients having to fetch files repeatedly.
//
// If we're using non-proxying s3, however, the max age is
// significantly shorter, to ensure that clients don't
// cache redirect responses to expired pre-signed URLs.
func maxAge() string {
if config.GetStorageBackend() == "s3" && !config.GetStorageS3Proxy() {
return "max-age=86400" // 24h
}

return "max-age=604800" // 7d
}

func (f *Fileserver) Route(r router.Router, m ...gin.HandlerFunc) {
fileserverGroup := r.AttachGroup("fileserver")

// attach middlewares appropriate for this group
fileserverGroup.Use(m...)
fileserverGroup.Use(
// Since we'll never host different files at the same
// URL (bc the ULIDs are generated per piece of media),
// it's sensible and safe to use a long cache here, so
// that clients don't keep fetching files over + over again.
//
// Nevertheless, we should use 'private' to indicate
// that there might be media in there which are gated by ACLs.
middleware.CacheControl("private", "max-age=604800"),
)
fileserverGroup.Use(middleware.CacheControl("private", maxAge()))

f.fileserver.Route(fileserverGroup.Handle)
}
Expand Down
6 changes: 0 additions & 6 deletions internal/api/fileserver/servefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,6 @@ func (m *Module) ServeFile(c *gin.Context) {
}

if content.URL != nil {
// This is a non-local S3 file we're redirecting to.
tsmethurst marked this conversation as resolved.
Show resolved Hide resolved
// Rewrite the cache control header to reflect the
// TTL of the generated signed link, instead of the
// default very long cache.
const cacheControl = "private,max-age=86400" // 24h
c.Header("Cache-Control", cacheControl)
c.Redirect(http.StatusFound, content.URL.String())
return
}
Expand Down