Skip to content

Commit

Permalink
set Content-Type correctly for javascript and css files (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
tleguijt authored Jan 22, 2025
1 parent a36c6cf commit d2e7de2
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20250121-150950.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: 'GCP: Detection of JS and CSS filetype and set ContentType'
time: 2025-01-21T15:09:50.953801+01:00
2 changes: 1 addition & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ var syncCmd = &cobra.Command{
fmt.Println("Directory synchronized successfully; creating lock file")

// Create the lockfile (empty file)
err = client.UploadFile(ctx, strings.NewReader(""), filepath.Join(remoteDir, lockFile))
err = client.UploadFile(ctx, strings.NewReader(""), filepath.Join(remoteDir, lockFile), "")
if err != nil {
log.Fatalf("failed to create lock file: %v", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/provider_azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func NewAzureBlobClient(ctx context.Context, bucket string) (*AzureBlobClient, e
return &AzureBlobClient{client: client, container: container}, nil
}

func (c *AzureBlobClient) UploadFile(ctx context.Context, file io.Reader, remotePath string) error {
func (c *AzureBlobClient) UploadFile(ctx context.Context, file io.Reader, remotePath string, contentType string) error {
var tempFile *os.File
var err error

Expand Down
7 changes: 6 additions & 1 deletion internal/provider_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@ func NewGCSClient(ctx context.Context, bucket string) (*GCSClient, error) {
}

// UploadFile to GCS
func (c *GCSClient) UploadFile(ctx context.Context, file io.Reader, remotePath string) error {
func (c *GCSClient) UploadFile(ctx context.Context, file io.Reader, remotePath string, contentType string) error {
bucket := c.client.Bucket(c.bucket)
obj := bucket.Object(remotePath)

writer := obj.NewWriter(ctx)

if contentType != "" {
writer.ContentType = contentType
}

defer writer.Close()

_, err := io.Copy(writer, file)
Expand Down
2 changes: 1 addition & 1 deletion internal/provider_s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewS3Client(ctx context.Context, bucket string) (*S3Client, error) {
}

// UploadFile to S3
func (c *S3Client) UploadFile(ctx context.Context, file io.Reader, remotePath string) error {
func (c *S3Client) UploadFile(ctx context.Context, file io.Reader, remotePath string, contentType string) error {
_, err := c.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(c.bucket),
Key: aws.String(remotePath),
Expand Down
23 changes: 21 additions & 2 deletions internal/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

type StorageClient interface {
UploadFile(ctx context.Context, file io.Reader, remotePath string) error
UploadFile(ctx context.Context, file io.Reader, remotePath string, contentType string) error
FileExists(ctx context.Context, remotePath string) (bool, error)
}

Expand Down Expand Up @@ -45,7 +45,12 @@ func SyncDirectory(ctx context.Context, client StorageClient, localDir, remoteDi
return
}
defer file.Close()
if err := client.UploadFile(ctx, file, remotePath); err != nil {

// Get the content type based on file extension.
// We can't rely on auto-detection of mimetypes because
// of security implications. See https://stackoverflow.com/questions/70695214/net-http-does-detectcontenttype-support-javascript
contentType := GetContentType(path)
if err := client.UploadFile(ctx, file, remotePath, contentType); err != nil {
errCh <- err
} else {
fmt.Printf("Uploaded %s to %s\n", path, remotePath)
Expand All @@ -69,3 +74,17 @@ func SyncDirectory(ctx context.Context, client StorageClient, localDir, remoteDi
}
return nil
}

func GetContentType(file string) string {
ext := filepath.Ext(file)
switch ext {
case ".htm", ".html":
return "text/html; charset=UTF-8"
case ".css":
return "text/css; charset=UTF-8"
case ".js":
return "application/javascript; charset=UTF-8"
}

return ""
}

0 comments on commit d2e7de2

Please sign in to comment.