You've already forked golang-saas-starter-kit
mirror of
https://github.com/raseels-repos/golang-saas-starter-kit.git
synced 2025-07-03 00:58:13 +02:00
Set static file sync content type by file extension
This commit is contained in:
@ -2,14 +2,13 @@ package cicd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
"mime"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/aws/aws-sdk-go/aws"
|
"github.com/aws/aws-sdk-go/aws"
|
||||||
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
"github.com/aws/aws-sdk-go/service/s3/s3manager"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// DirectoryIterator represents an iterator of a specified directory
|
// DirectoryIterator represents an iterator of a specified directory
|
||||||
@ -76,20 +75,8 @@ func (di *DirectoryIterator) UploadObject() s3manager.BatchUploadObject {
|
|||||||
acl = aws.String(di.acl)
|
acl = aws.String(di.acl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get file size and read the file content into a buffer
|
|
||||||
fileInfo, err := f.Stat()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
}
|
|
||||||
var size int64 = fileInfo.Size()
|
|
||||||
buffer := make([]byte, size)
|
|
||||||
f.Read(buffer)
|
|
||||||
|
|
||||||
f.Seek(0, io.SeekStart)
|
buffer, contentType, rerr := readFile(f)
|
||||||
|
|
||||||
ctBuf := make([]byte, 512)
|
|
||||||
f.Read(ctBuf)
|
|
||||||
contentType := http.DetectContentType(ctBuf)
|
|
||||||
|
|
||||||
nextPath, _ := filepath.Rel(di.dir, di.next.path)
|
nextPath, _ := filepath.Rel(di.dir, di.next.path)
|
||||||
|
|
||||||
@ -102,7 +89,32 @@ func (di *DirectoryIterator) UploadObject() s3manager.BatchUploadObject {
|
|||||||
ACL: acl,
|
ACL: acl,
|
||||||
},
|
},
|
||||||
After: func() error {
|
After: func() error {
|
||||||
|
if rerr != nil {
|
||||||
|
return rerr
|
||||||
|
}
|
||||||
return f.Close()
|
return f.Close()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func readFile(f *os.File) ([]byte, string, error) {
|
||||||
|
// Get file size and read the file content into a buffer
|
||||||
|
fileInfo, err := f.Stat()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
return nil, "", err
|
||||||
|
}
|
||||||
|
var size int64 = fileInfo.Size()
|
||||||
|
buffer := make([]byte, size)
|
||||||
|
f.Read(buffer)
|
||||||
|
|
||||||
|
ext := filepath.Ext(f.Name())
|
||||||
|
contentType := mime.TypeByExtension(ext)
|
||||||
|
|
||||||
|
//f.Seek(0, io.SeekStart)
|
||||||
|
//ctBuf := make([]byte, 512)
|
||||||
|
//f.Read(ctBuf)
|
||||||
|
//contentType = http.DetectContentType(ctBuf)
|
||||||
|
|
||||||
|
return buffer, contentType, nil
|
||||||
|
}
|
||||||
|
@ -3329,6 +3329,10 @@ func ServiceDeploy(log *log.Logger, req *serviceDeployRequest) error {
|
|||||||
|
|
||||||
staticDir := filepath.Join(req.ServiceDir, "static")
|
staticDir := filepath.Join(req.ServiceDir, "static")
|
||||||
|
|
||||||
|
if _, err := os.Stat(staticDir); err != nil {
|
||||||
|
return errors.Wrapf(err, "Static directory '%s' does not exist.", staticDir)
|
||||||
|
}
|
||||||
|
|
||||||
err := SyncPublicS3Files(req.awsSession(), req.S3BucketPublicName, req.StaticFilesS3Prefix, staticDir)
|
err := SyncPublicS3Files(req.awsSession(), req.S3BucketPublicName, req.StaticFilesS3Prefix, staticDir)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "Failed to sync static files from %s to s3://%s/%s", staticDir, req.S3BucketPublicName, req.StaticFilesS3Prefix)
|
return errors.Wrapf(err, "Failed to sync static files from %s to s3://%s/%s", staticDir, req.S3BucketPublicName, req.StaticFilesS3Prefix)
|
||||||
|
Reference in New Issue
Block a user