-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
services/*: Set content-type as best errort (#234)
* services/azblob: Set content type Signed-off-by: Xuanwo <github@xuanwo.io> * services/cos: Set content type correctly Signed-off-by: Xuanwo <github@xuanwo.io> * pkg/mime: Add TypeByFileName support Signed-off-by: Xuanwo <github@xuanwo.io> * services/fs: Try to set content type Signed-off-by: Xuanwo <github@xuanwo.io> * services/gcs: Set content type correctly Signed-off-by: Xuanwo <github@xuanwo.io> * services/kodo: Set content type Signed-off-by: Xuanwo <github@xuanwo.io> * services/oss: Set content type Signed-off-by: Xuanwo <github@xuanwo.io> * services/uss: Set content type Signed-off-by: Xuanwo <github@xuanwo.io>
- Loading branch information
Showing
9 changed files
with
102 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package mime | ||
|
||
import ( | ||
"mime" | ||
"strings" | ||
) | ||
|
||
// TypeByFileName will get mime type via filename, and return "" if not detected. | ||
func TypeByFileName(filename string) string { | ||
x := strings.Split(filename, ".") | ||
if len(x) < 2 { | ||
return "" | ||
} | ||
|
||
return mime.TypeByExtension("." + x[len(x)-1]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package mime | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTypeByFileName(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
filename string | ||
expected string | ||
}{ | ||
{"normal case", "test.pdf", "application/pdf"}, | ||
{"no ext", "testxxx", ""}, | ||
{"not a valid type", "test.xxx", ""}, | ||
{"multiple dots", "test.xx.a.pdf", "application/pdf"}, | ||
} | ||
|
||
for _, tt := range cases { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := TypeByFileName(tt.filename) | ||
assert.Equal(t, tt.expected, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters