From 99dc272f23b892f94f78b1367598247b70b4f26e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20D=C3=B6ll?= Date: Wed, 8 Jan 2025 15:06:48 +0000 Subject: [PATCH] chore: have a default type --- examples/demo/index.html | 13 +++++++++++++ examples/index.html | 13 +++++++++++++ go.mod | 1 + go.sum | 2 ++ main.go | 23 ++++++++++++++--------- 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 examples/demo/index.html create mode 100644 examples/index.html diff --git a/examples/demo/index.html b/examples/demo/index.html new file mode 100644 index 0000000..fa58d0b --- /dev/null +++ b/examples/demo/index.html @@ -0,0 +1,13 @@ + + + + + This is the title of the webpage! + + + +

This is an example paragraph. Anything in the body tag will appear on the page, just like this + p tag and its contents.

+ + + \ No newline at end of file diff --git a/examples/index.html b/examples/index.html new file mode 100644 index 0000000..fa58d0b --- /dev/null +++ b/examples/index.html @@ -0,0 +1,13 @@ + + + + + This is the title of the webpage! + + + +

This is an example paragraph. Anything in the body tag will appear on the page, just like this + p tag and its contents.

+ + + \ No newline at end of file diff --git a/go.mod b/go.mod index 5ed485f..0066133 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,7 @@ toolchain go1.23.4 require ( github.com/Azure/azure-sdk-for-go/sdk/azidentity v1.8.0 github.com/Azure/azure-sdk-for-go/sdk/storage/azblob v1.5.0 + github.com/h2non/filetype v1.1.3 github.com/sethvargo/go-githubactions v1.3.0 github.com/stretchr/testify v1.10.0 github.com/zeiss/pkg v0.1.19 diff --git a/go.sum b/go.sum index 89d11e4..0110eea 100644 --- a/go.sum +++ b/go.sum @@ -24,6 +24,8 @@ github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17w github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/h2non/filetype v1.1.3 h1:FKkx9QbD7HR/zjK1Ia5XiBsq9zdLi5Kf3zGyFTAFkGg= +github.com/h2non/filetype v1.1.3/go.mod h1:319b3zT68BvV+WRj7cwy856M2ehB3HqNOt6sy1HndBY= github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6 h1:IsMZxCuZqKuao2vNdfD82fjjgPLfyHLpR41Z88viRWs= github.com/keybase/go-keychain v0.0.0-20231219164618-57a3676c3af6/go.mod h1:3VeWNIJaW+O5xpRQbPp0Ybqu1vJd/pm7s2F473HRrkw= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= diff --git a/main.go b/main.go index a90ac6f..60fa562 100644 --- a/main.go +++ b/main.go @@ -5,16 +5,15 @@ import ( "errors" "io" "io/fs" - "net/http" "os" "path/filepath" - "strings" "template/internal/cfg" "github.com/Azure/azure-sdk-for-go/sdk/azidentity" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob" "github.com/Azure/azure-sdk-for-go/sdk/storage/azblob/blob" + "github.com/h2non/filetype" "github.com/sethvargo/go-githubactions" "github.com/zeiss/pkg/cast" "github.com/zeiss/pkg/utilx" @@ -39,7 +38,16 @@ func GetContentType(seeker io.ReadSeeker) (string, error) { // Slice to remove fill-up zero values which cause a wrong content type detection in the next step buff = buff[:bytesRead] - return http.DetectContentType(buff), nil + kind, err := filetype.Match(buff) + if err != nil && !errors.Is(err, filetype.ErrEmptyBuffer) { + return "", err + } + + if errors.Is(err, filetype.ErrEmptyBuffer) { + return "application/octet-stream", nil + } + + return kind.MIME.Type, nil } // nolint:gocyclo @@ -82,21 +90,18 @@ func main() { return err } - ct, err := GetContentType(file) + kind, err := GetContentType(file) if err != nil { return err } - cts := strings.SplitN(ct, ";", 2) - c := strings.TrimSpace(cts[0]) - opts := &azblob.UploadFileOptions{ HTTPHeaders: &blob.HTTPHeaders{ - BlobContentType: cast.Ptr(c), + BlobContentType: cast.Ptr(kind), }, } - githubactions.Infof("uploading %s (%s)", p, c) + githubactions.Infof("uploading %s (%s)", p, kind) _, err = client.UploadFile(ctx, cfg.ContainerName, p, file, opts) if err != nil {