mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-08-28 02:19:04 +08:00
v2.0
This commit is contained in:
113
server/utility/file/file.go
Normal file
113
server/utility/file/file.go
Normal file
@@ -0,0 +1,113 @@
|
||||
// Package file
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package file
|
||||
|
||||
import (
|
||||
"hotgo/utility/format"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
const ( //文件大小单位
|
||||
_ = iota
|
||||
KB = 1 << (10 * iota)
|
||||
MB
|
||||
)
|
||||
|
||||
type fileInfo struct { //文件信息
|
||||
name string
|
||||
size int64
|
||||
}
|
||||
|
||||
func PathExists(path string) (bool, error) {
|
||||
info, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return info.IsDir(), nil
|
||||
}
|
||||
|
||||
return false, err
|
||||
}
|
||||
|
||||
func FileExists(path string) (bool, error) {
|
||||
_, err := os.Stat(path)
|
||||
if err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, err
|
||||
}
|
||||
|
||||
// HasDir 判断文件夹是否存在
|
||||
func HasDir(path string) (bool, error) {
|
||||
_, _err := os.Stat(path)
|
||||
if _err == nil {
|
||||
return true, nil
|
||||
}
|
||||
if os.IsNotExist(_err) {
|
||||
return false, nil
|
||||
}
|
||||
return false, _err
|
||||
}
|
||||
|
||||
// CreateDir 创建文件夹
|
||||
func CreateDir(path string) (err error) {
|
||||
_exist, err := HasDir(path)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if !_exist {
|
||||
err = os.Mkdir(path, os.ModePerm)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// WalkDir 递归获取目录下文件的名称和大小
|
||||
func WalkDir(dirname string) (error, []fileInfo) {
|
||||
op, err := filepath.Abs(dirname) //获取目录的绝对路径
|
||||
if nil != err {
|
||||
return err, nil
|
||||
}
|
||||
files, err := ioutil.ReadDir(op) //获取目录下所有文件的信息,包括文件和文件夹
|
||||
if nil != err {
|
||||
return err, nil
|
||||
}
|
||||
|
||||
var fileInfos []fileInfo //返回值,存储读取的文件信息
|
||||
for _, f := range files {
|
||||
if f.IsDir() { // 如果是目录,那么就递归调用
|
||||
err, fs := WalkDir(op + `/` + f.Name()) //路径分隔符,linux 和 windows 不同
|
||||
if nil != err {
|
||||
return err, nil
|
||||
}
|
||||
fileInfos = append(fileInfos, fs...) //将 slice 添加到 slice
|
||||
} else {
|
||||
fi := fileInfo{op + `/` + f.Name(), f.Size()}
|
||||
fileInfos = append(fileInfos, fi) //slice 中添加成员
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fileInfos
|
||||
}
|
||||
|
||||
// DirSize 获取目录下所有文件大小
|
||||
func DirSize(dirname string) string {
|
||||
var (
|
||||
ss int64
|
||||
_, files = WalkDir(dirname)
|
||||
)
|
||||
for _, n := range files {
|
||||
ss += n.size
|
||||
}
|
||||
|
||||
return format.FileSize(ss)
|
||||
}
|
147
server/utility/file/mime.go
Normal file
147
server/utility/file/mime.go
Normal file
@@ -0,0 +1,147 @@
|
||||
// Package file
|
||||
// @Link https://github.com/bufanyun/hotgo
|
||||
// @Copyright Copyright (c) 2022 HotGo CLI
|
||||
// @Author Ms <133814250@qq.com>
|
||||
// @License https://github.com/bufanyun/hotgo/blob/master/LICENSE
|
||||
//
|
||||
package file
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"io/ioutil"
|
||||
"path"
|
||||
)
|
||||
|
||||
// 文件分类
|
||||
const (
|
||||
KindImg = "images" // 图片
|
||||
KindDoc = "document" // 文档
|
||||
KindAudio = "audio" // 音频
|
||||
KindVideo = "video" // 视频
|
||||
KindOther = "other" // 其他
|
||||
)
|
||||
|
||||
var (
|
||||
// 图片类型
|
||||
imgType = g.MapStrStr{
|
||||
"jpg": "image/jpeg",
|
||||
"png": "image/png",
|
||||
"gif": "image/gif",
|
||||
"webp": "image/webp",
|
||||
"cr2": "image/x-canon-cr2",
|
||||
"tif": "image/tiff",
|
||||
"bmp": "image/bmp",
|
||||
"heif": "image/heif",
|
||||
"jxr": "image/vnd.ms-photo",
|
||||
"psd": "image/vnd.adobe.photoshop",
|
||||
"ico": "image/vnd.microsoft.icon",
|
||||
"dwg": "image/vnd.dwg",
|
||||
}
|
||||
|
||||
// 文档类型
|
||||
docType = g.MapStrStr{
|
||||
"doc": "application/msword",
|
||||
"docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"xls": "application/vnd.ms-excel",
|
||||
"xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"ppt": "application/vnd.ms-powerpoint",
|
||||
"pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
}
|
||||
|
||||
// 音频类型
|
||||
audioType = g.MapStrStr{
|
||||
"mid": "audio/midi",
|
||||
"mp3": "audio/mpeg",
|
||||
"m4a": "audio/mp4",
|
||||
"ogg": "audio/ogg",
|
||||
"flac": "audio/x-flac",
|
||||
"wav": "audio/x-wav",
|
||||
"amr": "audio/amr",
|
||||
"aac": "audio/aac",
|
||||
"aiff": "audio/x-aiff",
|
||||
}
|
||||
|
||||
// 视频类型
|
||||
videoType = g.MapStrStr{
|
||||
"mp4": "video/mp4",
|
||||
"m4v": "video/x-m4v",
|
||||
"mkv": "video/x-matroska",
|
||||
"webm": "video/webm",
|
||||
"mov": "video/quicktime",
|
||||
"avi": "video/x-msvideo",
|
||||
"wmv": "video/x-ms-wmv",
|
||||
"mpg": "video/mpeg",
|
||||
"flv": "video/x-flv",
|
||||
"3gp": "video/3gpp",
|
||||
}
|
||||
)
|
||||
|
||||
// IsImgType 判断是否为图片
|
||||
func IsImgType(ext string) bool {
|
||||
_, ok := imgType[ext]
|
||||
return ok
|
||||
}
|
||||
|
||||
// GetImgType 获取图片类型
|
||||
func GetImgType(ext string) (string, error) {
|
||||
if mime, ok := imgType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
return "", gerror.New("Invalid image type")
|
||||
}
|
||||
|
||||
// GetFileType 获取文件类型
|
||||
func GetFileType(ext string) (string, error) {
|
||||
if mime, ok := imgType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
if mime, ok := docType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
if mime, ok := audioType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
if mime, ok := videoType[ext]; ok {
|
||||
return mime, nil
|
||||
}
|
||||
return "", gerror.New("Invalid file type")
|
||||
}
|
||||
|
||||
// GetFileKind 获取文件所属分类
|
||||
func GetFileKind(ext string) string {
|
||||
if _, ok := imgType[ext]; ok {
|
||||
return KindImg
|
||||
}
|
||||
if _, ok := docType[ext]; ok {
|
||||
return KindDoc
|
||||
}
|
||||
if _, ok := audioType[ext]; ok {
|
||||
return KindAudio
|
||||
}
|
||||
if _, ok := videoType[ext]; ok {
|
||||
return KindVideo
|
||||
}
|
||||
return KindOther
|
||||
}
|
||||
|
||||
// Ext 获取文件后缀
|
||||
func Ext(baseName string) string {
|
||||
return gstr.StrEx(path.Ext(baseName), ".")
|
||||
}
|
||||
|
||||
// UploadFileByte 获取上传文件的byte
|
||||
func UploadFileByte(file *ghttp.UploadFile) (b []byte, err error) {
|
||||
open, err := file.Open()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
all, err := ioutil.ReadAll(open)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return all, nil
|
||||
}
|
Reference in New Issue
Block a user