0
0
mirror of https://github.com/thegeeklab/wp-s3-action.git synced 2024-11-10 04:40:38 +00:00

Adding support for uploading files with Content Encoding

This commit is contained in:
Craig Warren 2016-05-05 08:16:34 +01:00
parent 81ee08a5a5
commit 748c4417a3
2 changed files with 49 additions and 7 deletions

55
aws.go
View File

@ -82,6 +82,19 @@ func (a *AWS) Upload(local, remote string) error {
}
}
var contentEncoding string
if a.vargs.ContentEncoding.IsString() {
contentEncoding = a.vargs.ContentEncoding.String()
} else if !a.vargs.ContentEncoding.IsEmpty() {
encodingMap := a.vargs.ContentEncoding.Map()
for patternExt := range encodingMap {
if patternExt == fileExt {
contentEncoding = encodingMap[patternExt]
break
}
}
}
metadata := map[string]*string{}
vmap := a.vargs.Metadata.Map()
if len(vmap) > 0 {
@ -108,15 +121,21 @@ func (a *AWS) Upload(local, remote string) error {
return err
}
debug("Uploading \"%s\" with Content-Type \"%s\" and permissions \"%s\"", local, contentType, access)
_, err = a.client.PutObject(&s3.PutObjectInput{
debug("\"%s\" not found in bucket, uploading with Content-Type \"%s\" and permissions \"%s\"", local, contentType, access)
var putObject = &s3.PutObjectInput{
Bucket: aws.String(a.vargs.Bucket),
Key: aws.String(remote),
Body: file,
ContentType: aws.String(contentType),
ACL: aws.String(access),
Metadata: metadata,
})
}
if(len(contentEncoding) > 0) {
putObject.ContentEncoding = aws.String(contentEncoding)
}
_, err = a.client.PutObject(putObject)
return err
}
@ -137,6 +156,16 @@ func (a *AWS) Upload(local, remote string) error {
shouldCopy = true
}
if !shouldCopy && head.ContentEncoding == nil && contentEncoding != "" {
debug("Content-Encoding has changed from unset to %s", contentEncoding)
shouldCopy = true
}
if !shouldCopy && head.ContentEncoding != nil && contentEncoding != *head.ContentEncoding {
debug("Content-Encoding has changed from %s to %s", *head.ContentEncoding, contentEncoding)
shouldCopy = true
}
if !shouldCopy && len(head.Metadata) != len(metadata) {
debug("Count of metadata values has changed for %s", local)
shouldCopy = true
@ -193,7 +222,7 @@ func (a *AWS) Upload(local, remote string) error {
}
debug("Updating metadata for \"%s\" Content-Type: \"%s\", ACL: \"%s\"", local, contentType, access)
_, err = a.client.CopyObject(&s3.CopyObjectInput{
var copyObject = &s3.CopyObjectInput{
Bucket: aws.String(a.vargs.Bucket),
Key: aws.String(remote),
CopySource: aws.String(fmt.Sprintf("%s/%s", a.vargs.Bucket, remote)),
@ -201,7 +230,13 @@ func (a *AWS) Upload(local, remote string) error {
ContentType: aws.String(contentType),
Metadata: metadata,
MetadataDirective: aws.String("REPLACE"),
})
}
if(len(contentEncoding) > 0) {
copyObject.ContentEncoding = aws.String(contentEncoding)
}
_, err = a.client.CopyObject(copyObject)
return err
} else {
_, err = file.Seek(0, 0)
@ -210,14 +245,20 @@ func (a *AWS) Upload(local, remote string) error {
}
debug("Uploading \"%s\" with Content-Type \"%s\" and permissions \"%s\"", local, contentType, access)
_, err = a.client.PutObject(&s3.PutObjectInput{
var putObject = &s3.PutObjectInput{
Bucket: aws.String(a.vargs.Bucket),
Key: aws.String(remote),
Body: file,
ContentType: aws.String(contentType),
ACL: aws.String(access),
Metadata: metadata,
})
}
if(len(contentEncoding) > 0){
putObject.ContentEncoding = aws.String(contentEncoding)
}
_, err = a.client.PutObject(putObject)
return err
}
}

View File

@ -12,6 +12,7 @@ type PluginArgs struct {
Delete bool `json:"delete"`
Access StringMap `json:"acl"`
ContentType StringMap `json:"content_type"`
ContentEncoding StringMap `json:"content_encoding"`
Metadata DeepStringMap `json:"metadata"`
Redirects map[string]string `json:"redirects"`
CloudFrontDistribution string `json:"cloudfront_distribution_id"`