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.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
package files
import (
"encoding/base64"
"goskeleton/app/global/my_errors"
"goskeleton/app/global/variable"
"mime/multipart"
"net/http"
"os"
)
// 返回值说明:
// 7z、exe、doc 类型会返回 application/octet-stream 未知的文件类型
// jpg => image/jpeg
// png => image/png
// ico => image/x-icon
// bmp => image/bmp
// xlsx、docx 、zip => application/zip
// tar.gz => application/x-gzip
// txt、json、log等文本文件 => text/plain; charset=utf-8 备注: 就算txt是gbk、ansi编码, 也会识别为utf-8
// 通过文件名获取文件mime信息
func GetFilesMimeByFileName ( filepath string ) string {
f , err := os . Open ( filepath )
if err != nil {
variable . ZapLog . Error ( my_errors . ErrorsFilesUploadOpenFail + err . Error ( ) )
}
defer f . Close ( )
// 只需要前 32 个字节就可以了
buffer := make ( [ ] byte , 32 )
if _ , err := f . Read ( buffer ) ; err != nil {
variable . ZapLog . Error ( my_errors . ErrorsFilesUploadReadFail + err . Error ( ) )
return ""
}
return http . DetectContentType ( buffer )
}
// 通过文件指针获取文件mime信息
func GetFilesMimeByFp ( fp multipart . File ) string {
buffer := make ( [ ] byte , 32 )
if _ , err := fp . Read ( buffer ) ; err != nil {
variable . ZapLog . Error ( my_errors . ErrorsFilesUploadReadFail + err . Error ( ) )
return ""
}
return http . DetectContentType ( buffer )
}
// 读取本地文件, 进行Base64编码
func GetFileBase64 ( filePath string ) ( string , error ) {
data , err := os . ReadFile ( filePath )
if err != nil {
return "" , err
}
return base64 . StdEncoding . EncodeToString ( data ) , nil
}