增加解析文件类型的方法
This commit is contained in:
parent
b4b3e261e0
commit
c886ff6b20
23
file.go
23
file.go
@ -9,6 +9,7 @@ package util
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@ -156,3 +157,25 @@ func (f *file) IsFileExist(filePath string) (bool, bool) {
|
||||
fileStat, err := os.Stat(filePath)
|
||||
return nil == err || os.IsExist(err), (nil == err || os.IsExist(err)) && !fileStat.IsDir()
|
||||
}
|
||||
|
||||
// GetFileMIMEType 获取本地文件的MIME类型
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 17:15 2023/2/7
|
||||
func (f *file) GetFileMIMEType(filePath string) (string, error) {
|
||||
var (
|
||||
err error
|
||||
fileHandler *os.File
|
||||
)
|
||||
if fileHandler, err = os.Open(filePath); nil != err {
|
||||
return "", err
|
||||
}
|
||||
// 只需要前 512 个字节就可以了
|
||||
buffer := make([]byte, 512)
|
||||
if _, err = fileHandler.Read(buffer); nil != err {
|
||||
return "", err
|
||||
}
|
||||
contentType := http.DetectContentType(buffer)
|
||||
return contentType, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user