Skip to content

Commit

Permalink
expire files on access
Browse files Browse the repository at this point in the history
S3 can use lifecycle rules to delete files, but:
* files might not disappear instantly on expiration, which is fine as long
  as we can ensure that client-side they do expire on time
* it is rule-based, which might not be granular enough for our use case
  some files in a collection will expire at a set date that can be
  modified afterwards. So we can't set a rule to expire after x days
  without working around the limitation of the rules by (ab)using tags

might change my mind later, for now this fixes the files being
accessible way past their supposed expiration date.
  • Loading branch information
odrling committed Jul 19, 2024
1 parent 7c8aff0 commit 6bc517c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 9 additions & 1 deletion server/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,22 @@ func Download(ctx context.Context, input *DownloadInput) (*huma.StreamResponse,
if err != nil {
return nil, err
}
stat, err := obj.Stat()
if err != nil {
return nil, err
}

if stat.Expires.Unix() != 0 && stat.Expires.Before(time.Now()) {
go DeleteObject(ctx, input.Filename)
return nil, huma.Error410Gone("File expired")
}

return &huma.StreamResponse{
Body: func(ctx huma.Context) {
defer obj.Close()

writer := ctx.BodyWriter()

stat, err := obj.Stat()
filename := url.PathEscape(stat.UserMetadata["Filename"])
content_type := stat.UserMetadata["Type"]

Expand Down
10 changes: 10 additions & 0 deletions server/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,13 @@ func GetFileObject(ctx context.Context, filename string) (*minio.Object, error)
obj, err := client.GetObject(ctx, CONFIG.S3.BucketName, filename, minio.GetObjectOptions{})
return obj, err
}

func DeleteObject(ctx context.Context, filename string) error {
client, err := getS3Client()
if err != nil {
return err
}

err = client.RemoveObject(ctx, CONFIG.S3.BucketName, filename, minio.RemoveObjectOptions{})
return err
}

0 comments on commit 6bc517c

Please sign in to comment.