1
0
Fork 0
mirror of https://github.com/Luzifer/share.git synced 2024-12-29 23:11:18 +00:00

Force bootstrap to apply gzip encoding

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2019-05-13 00:05:06 +02:00
parent 81060aba97
commit f679cd3785
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
3 changed files with 30 additions and 9 deletions

View file

@ -19,7 +19,7 @@ func simpleFilePost(res http.ResponseWriter, r *http.Request) {
return return
} }
url, err := executeUpload(fh.Filename, f, true, "") url, err := executeUpload(fh.Filename, f, true, "", false)
if err != nil { if err != nil {
log.WithError(err).Error("Uploading file from HTTP request failed") log.WithError(err).Error("Uploading file from HTTP request failed")
http.Error(res, "Failed to upload file. For details see the log.", http.StatusInternalServerError) http.Error(res, "Failed to upload file. For details see the log.", http.StatusInternalServerError)

View file

@ -9,8 +9,9 @@ import (
"io" "io"
"os" "os"
"github.com/Luzifer/rconfig"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/Luzifer/rconfig"
) )
var ( var (
@ -98,7 +99,7 @@ func doCLIUpload() error {
inFile = inFileHandle inFile = inFileHandle
} }
url, err := executeUpload(inFileName, inFile, true, cfg.ContentType) url, err := executeUpload(inFileName, inFile, true, cfg.ContentType, false)
if err != nil { if err != nil {
return fmt.Errorf("Unable to upload file: %s", err) return fmt.Errorf("Unable to upload file: %s", err)
} }
@ -109,7 +110,7 @@ func doCLIUpload() error {
func doBootstrap() error { func doBootstrap() error {
for _, asset := range []string{"index.html", "app.js"} { for _, asset := range []string{"index.html", "app.js"} {
if _, err := executeUpload(asset, bytes.NewReader(MustAsset("frontend/"+asset)), false, ""); err != nil { if _, err := executeUpload(asset, bytes.NewReader(MustAsset("frontend/"+asset)), false, "", true); err != nil {
return fmt.Errorf("Unable to upload bootstrap asset %q: %s", asset, err) return fmt.Errorf("Unable to upload bootstrap asset %q: %s", asset, err)
} }
} }

View file

@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"compress/gzip"
"crypto/sha1" "crypto/sha1"
"fmt" "fmt"
"html/template" "html/template"
@ -16,10 +17,11 @@ import (
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3" "github.com/aws/aws-sdk-go/service/s3"
"github.com/cheggaaa/pb" "github.com/cheggaaa/pb"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
) )
func executeUpload(inFileName string, inFileHandle io.ReadSeeker, useCalculatedFilename bool, overrideMimeType string) (string, error) { func executeUpload(inFileName string, inFileHandle io.ReadSeeker, useCalculatedFilename bool, overrideMimeType string, forceGzip bool) (string, error) {
var ( var (
upFile = inFileName upFile = inFileName
err error err error
@ -42,6 +44,23 @@ func executeUpload(inFileName string, inFileHandle io.ReadSeeker, useCalculatedF
log.Debugf("Uploading file to %q with type %q", upFile, mimeType) log.Debugf("Uploading file to %q with type %q", upFile, mimeType)
var contentEncoding *string
if forceGzip {
buf := new(bytes.Buffer)
gw := gzip.NewWriter(buf)
if _, err := io.Copy(gw, inFileHandle); err != nil {
return "", errors.Wrap(err, "Unable to compress file")
}
if err := gw.Close(); err != nil {
return "", errors.Wrap(err, "Unable to close gzip writer")
}
inFileHandle = bytes.NewReader(buf.Bytes())
contentEncoding = aws.String("gzip")
}
sess := session.Must(session.NewSession()) sess := session.Must(session.NewSession())
svc := s3.New(sess) svc := s3.New(sess)
@ -71,6 +90,7 @@ func executeUpload(inFileName string, inFileHandle io.ReadSeeker, useCalculatedF
if _, err := svc.PutObject(&s3.PutObjectInput{ if _, err := svc.PutObject(&s3.PutObjectInput{
Body: ps, Body: ps,
Bucket: aws.String(cfg.Bucket), Bucket: aws.String(cfg.Bucket),
ContentEncoding: contentEncoding,
ContentType: aws.String(mimeType), ContentType: aws.String(mimeType),
Key: aws.String(upFile), Key: aws.String(upFile),
}); err != nil { }); err != nil {