Skip to content

Commit

Permalink
fix(server): add missing mime types (#1344)
Browse files Browse the repository at this point in the history
* chore(server): asset content type test

* fix(server): add missing mime types

* fix(server): add missing mime types
  • Loading branch information
yk-eukarya authored Dec 17, 2024
1 parent 8dbb8dc commit 07186df
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
11 changes: 11 additions & 0 deletions server/internal/usecase/gateway/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/reearth/reearth-cms/server/pkg/file"
"github.com/reearth/reearthx/i18n"
"github.com/reearth/reearthx/rerror"
"github.com/samber/lo"
)

var (
Expand Down Expand Up @@ -43,6 +44,16 @@ type IssueUploadAssetParam struct {
Cursor string
}

func init() {
// mime package depends on the OS, so adding the requited mime types to make sure about the results in different OS
lo.Must0(mime.AddExtensionType(".zip", "application/zip"))
lo.Must0(mime.AddExtensionType(".7z", "application/x-7z-compressed"))
lo.Must0(mime.AddExtensionType(".gz", "application/gzip"))
lo.Must0(mime.AddExtensionType(".bz2", "application/x-bzip2"))
lo.Must0(mime.AddExtensionType(".tar", "application/x-tar"))
lo.Must0(mime.AddExtensionType(".rar", "application/vnd.rar"))
}

func (p IssueUploadAssetParam) ContentType() string {
return mime.TypeByExtension(path.Ext(p.Filename))
}
Expand Down
45 changes: 45 additions & 0 deletions server/internal/usecase/gateway/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package gateway

import (
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)

func TestIssueUploadAssetParam_ContentType(t *testing.T) {
tests := []struct {
name string
fields IssueUploadAssetParam
want string
}{
{
name: "zip file",
fields: IssueUploadAssetParam{
UUID: uuid.New().String(),
Filename: "filename.zip",
ContentLength: 1,
ExpiresAt: time.Now(),
Cursor: "",
},
want: "application/zip",
},
{
name: "7zip file",
fields: IssueUploadAssetParam{
UUID: uuid.New().String(),
Filename: "filename.7z",
ContentLength: 1,
ExpiresAt: time.Now(),
Cursor: "",
},
want: "application/x-7z-compressed",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.want, tt.fields.ContentType())
})
}
}

0 comments on commit 07186df

Please sign in to comment.