http.DetectContentType
This commit is contained in:
parent
6770bae66a
commit
43d91bd3d8
|
|
@ -63,9 +63,13 @@ func (s *Minio) Put(fileName, objectName string, onProcess func(fsize, uploaded
|
|||
|
||||
objectName = strings.TrimLeft(objectName, "/ ")
|
||||
ext := strings.TrimLeft(path.Ext(fileName), ".")
|
||||
contentType := ext2ContentType(ext)
|
||||
if contentType == "" {
|
||||
contentType = detectContentType(fileName)
|
||||
}
|
||||
|
||||
_, err := s.client.FPutObjectWithContext(ctx, s.config.Bucket, objectName, fileName,
|
||||
minio.PutObjectOptions{ContentType: ext2ContentType(ext)})
|
||||
minio.PutObjectOptions{ContentType: contentType})
|
||||
if err != nil {
|
||||
log.Errorf("upload file to minio error:%s", err.Error())
|
||||
return err
|
||||
|
|
|
|||
|
|
@ -115,6 +115,24 @@ func ext2ContentType(ext string) string {
|
|||
}
|
||||
}
|
||||
|
||||
// detectContentType 从文件内容检测 MIME 类型
|
||||
func detectContentType(filePath string) string {
|
||||
f, err := os.Open(filePath)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
// 读取前 512 字节用于检测
|
||||
buf := make([]byte, 512)
|
||||
n, err := f.Read(buf)
|
||||
if err != nil && err != io.EOF {
|
||||
return ""
|
||||
}
|
||||
|
||||
return http.DetectContentType(buf[:n])
|
||||
}
|
||||
|
||||
func Download(url, path string) error {
|
||||
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
|
||||
stat, err := f.Stat()
|
||||
|
|
|
|||
Loading…
Reference in New Issue