Skip to content

Commit

Permalink
send content disposition with copied file
Browse files Browse the repository at this point in the history
  • Loading branch information
leafo committed Nov 24, 2023
1 parent 125f5ee commit 5af17a1
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
18 changes: 13 additions & 5 deletions zipserver/copy_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func notifyCallback(callbackURL string, resValues url.Values) error {
outBody := bytes.NewBufferString(resValues.Encode())
req, err := http.NewRequestWithContext(notifyCtx, http.MethodPost, callbackURL, outBody)
if err != nil {
log.Printf("Failed to create callback request: %v", err)
log.Print("Failed to create callback request: ", err)
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
Expand Down Expand Up @@ -134,14 +134,22 @@ func copyHandler(w http.ResponseWriter, r *http.Request) error {

mReader := newMeasuredReader(reader)

// transfer the reader to s3
// TODO: get the actual mime type from the GetFile request
log.Print("Starting transfer: ", key)
uploadHeaders := http.Header{}

contentType := headers.Get("Content-Type")
if contentType == "" {
contentType = "application/octet-stream"
}
checksumMd5, err := targetStorage.PutFile(jobCtx, targetBucket, key, mReader, contentType)

uploadHeaders.Set("Content-Type", contentType)

contentDisposition := headers.Get("Content-Disposition")
if contentDisposition != "" {
uploadHeaders.Set("Content-Disposition", contentDisposition)
}

log.Print("Starting transfer: ", key, " ", uploadHeaders)
checksumMd5, err := targetStorage.PutFile(jobCtx, targetBucket, key, mReader, uploadHeaders)

if err != nil {
log.Print("Failed to copy file: ", err)
Expand Down
28 changes: 18 additions & 10 deletions zipserver/s3_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/md5"
"fmt"
"io"
"net/http"
"net/url"
"strconv"

Expand Down Expand Up @@ -46,22 +47,29 @@ func NewS3Storage(config *StorageConfig) (*S3Storage, error) {
}

// upload file and return md5 checksum of transferred bytes
func (c *S3Storage) PutFile(ctx context.Context, bucket, key string, contents io.Reader, mimeType string) (string, error) {
func (c *S3Storage) PutFile(ctx context.Context, bucket, key string, contents io.Reader, uploadHeaders http.Header) (string, error) {
uploader := s3manager.NewUploaderWithClient(s3.New(c.Session))

// Initialize a new MD5 hash.
hash := md5.New()

// Create a multi-reader that will read from the original reader and also
// perform the hash.
// duplicate reads into the md5 hasher
multi := io.TeeReader(contents, hash)

_, err := uploader.UploadWithContext(ctx, &s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: multi,
ContentType: aws.String(mimeType),
})
uploadInput := &s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: multi,
}

if contentType := uploadHeaders.Get("Content-Type"); contentType != "" {
uploadInput.ContentType = aws.String(contentType)
}

if contentDisposition := uploadHeaders.Get("Content-Disposition"); contentDisposition != "" {
uploadInput.ContentDisposition = aws.String(contentDisposition)
}

_, err := uploader.UploadWithContext(ctx, uploadInput)

if err != nil {
return "", err
Expand Down

0 comments on commit 5af17a1

Please sign in to comment.