Skip to content

Commit

Permalink
fix: detect mime type
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Jan 8, 2025
1 parent d581e14 commit 418921a
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package main

import (
"context"
"crypto/md5"

Check failure on line 5 in main.go

View workflow job for this annotation

GitHub Actions / lint

G501: Blocklisted import crypto/md5: weak cryptographic primitive (gosec)
"encoding/hex"
"io/fs"
"net/http"
"os"
"path/filepath"

Expand All @@ -11,6 +14,8 @@ import (
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
"github.com/Azure/azure-sdk-for-go/sdk/storage/azblob"
"github.com/sethvargo/go-githubactions"
"github.com/zeiss/pkg/cast"
"github.com/zeiss/pkg/conv"
"github.com/zeiss/pkg/utilx"
)

Expand Down Expand Up @@ -48,14 +53,38 @@ func main() {
}
defer file.Close()

fileinfo, err := file.Stat()
if err != nil {
return err
}

p, err := filepath.Rel(root, path)
if err != nil {
return err
}

githubactions.Infof("uploading %s", p)

_, err = client.UploadFile(ctx, cfg.ContainerName, p, file, nil)
filesize := fileinfo.Size()
buffer := make([]byte, filesize)

_, err = file.Read(buffer)
if err != nil {
return err
}

hash := md5.Sum(buffer)

Check failure on line 76 in main.go

View workflow job for this annotation

GitHub Actions / lint

G401: Use of weak cryptographic primitive (gosec)

ct := http.DetectContentType(buffer)
opts := &azblob.UploadFileOptions{
Metadata: map[string]*string{
"Content-Type": cast.Ptr(ct),
"Content-MD5": cast.Ptr(hex.EncodeToString(hash[:])),
"Content-Length": cast.Ptr(conv.String(filesize)),
},
}

_, err = client.UploadBuffer(ctx, cfg.ContainerName, p, buffer, opts)
if err != nil {
return err
}
Expand Down

0 comments on commit 418921a

Please sign in to comment.