[bugfix] s3 media uploaded without content-type (#3353)

* update go-storage dependency, for S3Storage manually call PutObject() so we can set content-type

* update calls to PutFile() to include the contentType
This commit is contained in:
kim 2024-09-26 12:43:10 +00:00 committed by GitHub
commit 53ee6aef08
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
15 changed files with 433 additions and 210 deletions

View file

@ -216,10 +216,18 @@ func (p *ProcessingEmoji) store(ctx context.Context) error {
"png",
)
// Get mimetype for the file container
// type, falling back to generic data.
p.emoji.ImageContentType = getMimeType(ext)
// Set the known emoji static content type.
p.emoji.ImageStaticContentType = "image/png"
// Copy temporary file into storage at path.
filesz, err := p.mgr.state.Storage.PutFile(ctx,
p.emoji.ImagePath,
temppath,
p.emoji.ImageContentType,
)
if err != nil {
return gtserror.Newf("error writing emoji to storage: %w", err)
@ -229,6 +237,7 @@ func (p *ProcessingEmoji) store(ctx context.Context) error {
staticsz, err := p.mgr.state.Storage.PutFile(ctx,
p.emoji.ImageStaticPath,
staticpath,
p.emoji.ImageStaticContentType,
)
if err != nil {
return gtserror.Newf("error writing static to storage: %w", err)
@ -256,13 +265,6 @@ func (p *ProcessingEmoji) store(ctx context.Context) error {
"png",
)
// Get mimetype for the file container
// type, falling back to generic data.
p.emoji.ImageContentType = getMimeType(ext)
// Set the known emoji static content type.
p.emoji.ImageStaticContentType = "image/png"
// We can now consider this cached.
p.emoji.Cached = util.Ptr(true)

View file

@ -261,10 +261,15 @@ func (p *ProcessingMedia) store(ctx context.Context) error {
ext,
)
// Get mimetype for the file container
// type, falling back to generic data.
p.media.File.ContentType = getMimeType(ext)
// Copy temporary file into storage at path.
filesz, err := p.mgr.state.Storage.PutFile(ctx,
p.media.File.Path,
temppath,
p.media.File.ContentType,
)
if err != nil {
return gtserror.Newf("error writing media to storage: %w", err)
@ -286,10 +291,14 @@ func (p *ProcessingMedia) store(ctx context.Context) error {
thumbExt,
)
// Determine thumbnail content-type from thumb ext.
p.media.Thumbnail.ContentType = getMimeType(thumbExt)
// Copy thumbnail file into storage at path.
thumbsz, err := p.mgr.state.Storage.PutFile(ctx,
p.media.Thumbnail.Path,
thumbpath,
p.media.Thumbnail.ContentType,
)
if err != nil {
return gtserror.Newf("error writing thumb to storage: %w", err)
@ -298,9 +307,6 @@ func (p *ProcessingMedia) store(ctx context.Context) error {
// Set final determined thumbnail size.
p.media.Thumbnail.FileSize = int(thumbsz)
// Determine thumbnail content-type from thumb ext.
p.media.Thumbnail.ContentType = getMimeType(thumbExt)
// Generate a media attachment thumbnail URL.
p.media.Thumbnail.URL = uris.URIForAttachment(
p.media.AccountID,
@ -320,10 +326,6 @@ func (p *ProcessingMedia) store(ctx context.Context) error {
ext,
)
// Get mimetype for the file container
// type, falling back to generic data.
p.media.File.ContentType = getMimeType(ext)
// We can now consider this cached.
p.media.Cached = util.Ptr(true)

View file

@ -97,23 +97,39 @@ func (d *Driver) Put(ctx context.Context, key string, value []byte) (int, error)
return d.Storage.WriteBytes(ctx, key, value)
}
// PutStream writes the bytes from supplied reader at key in the storage
func (d *Driver) PutStream(ctx context.Context, key string, r io.Reader) (int64, error) {
return d.Storage.WriteStream(ctx, key, r)
}
// PutFile moves the contents of file at path, to storage.Driver{} under given key.
func (d *Driver) PutFile(ctx context.Context, key string, filepath string) (int64, error) {
// PutFile moves the contents of file at path, to storage.Driver{} under given key (with content-type if supported).
func (d *Driver) PutFile(ctx context.Context, key, filepath, contentType string) (int64, error) {
// Open file at path for reading.
file, err := os.Open(filepath)
if err != nil {
return 0, gtserror.Newf("error opening file %s: %w", filepath, err)
}
// Write the file data to storage under key. Note
// that for disk.DiskStorage{} this should end up
// being a highly optimized Linux sendfile syscall.
sz, err := d.Storage.WriteStream(ctx, key, file)
var sz int64
switch d := d.Storage.(type) {
case *s3.S3Storage:
var info minio.UploadInfo
// For S3 storage, write the file but specifically pass in the
// content-type as an extra option. This handles the case of media
// being served via CDN redirect (where we don't handle content-type).
info, err = d.PutObject(ctx, key, file, minio.PutObjectOptions{
ContentType: contentType,
})
// Get size from
// uploaded info.
sz = info.Size
default:
// Write the file data to storage under key. Note
// that for disk.DiskStorage{} this should end up
// being a highly optimized Linux sendfile syscall.
sz, err = d.WriteStream(ctx, key, file)
}
// Wrap write error.
if err != nil {
err = gtserror.Newf("error writing file %s: %w", key, err)
}
@ -305,11 +321,7 @@ func NewS3Storage() (*Driver, error) {
Creds: credentials.NewStaticV4(access, secret, ""),
Secure: secure,
},
GetOpts: minio.GetObjectOptions{},
PutOpts: minio.PutObjectOptions{},
PutChunkSize: 5 * 1024 * 1024, // 5MiB
StatOpts: minio.StatObjectOptions{},
RemoveOpts: minio.RemoveObjectOptions{},
ListSize: 200,
})
if err != nil {