You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
919 B
51 lines
919 B
1 month ago
|
package utils
|
||
|
|
||
|
import (
|
||
|
"io"
|
||
|
"mime"
|
||
|
"path/filepath"
|
||
|
)
|
||
|
|
||
|
func GetContentType(filePath string, body []byte) (string, error) {
|
||
|
|
||
|
contentType := ""
|
||
|
if len(filePath) > 0 {
|
||
|
ext := filepath.Ext(filePath)
|
||
|
// 根据扩展名获取 MIME 类型
|
||
|
contentType = mime.TypeByExtension(ext)
|
||
|
}
|
||
|
|
||
|
//if contentType == "" {
|
||
|
//
|
||
|
// contentType = http.DetectContentType(body)
|
||
|
//}
|
||
|
|
||
|
return contentType, nil
|
||
|
}
|
||
|
|
||
|
func GetContentTypeForIoRead(filePath string, body io.Reader) (string, error) {
|
||
|
|
||
|
//buffer := make([]byte, 512) // 读取文件的前 512 字节用于检测内容类型
|
||
|
//_, err := body.Read(buffer)
|
||
|
//if err != nil {
|
||
|
// return "", err
|
||
|
//}
|
||
|
return GetContentType(filePath, nil)
|
||
|
}
|
||
|
|
||
|
// 获取存储类型
|
||
|
func GetStoreType(key string) (storeType string) {
|
||
|
switch key {
|
||
|
case "0":
|
||
|
storeType = "oss"
|
||
|
case "1":
|
||
|
storeType = "cos"
|
||
|
case "2":
|
||
|
storeType = "asw"
|
||
|
case "3":
|
||
|
storeType = "local"
|
||
|
}
|
||
|
|
||
|
return storeType
|
||
|
}
|