Files
kenaito-media-picker/util/file.go
T
odboy b276e8c56f feat(core): 添加PSD/PSB图片格式支持并重构媒体文件处理
- 新增ReadPsdImage函数支持PSD/PSB格式图片信息读取
- 修改文件扫描逻辑,将视频处理改为媒体候选处理,使用ffprobe统一判定
- 添加对PSB格式的支持,与PSD共享相同8BPS文件头处理
- 重构handle_image.go中的图片处理逻辑,优化错误处理和记录保存
- 更新文件扫描过滤规则,支持更多图片格式并跳过数据库文件
- 添加文件MD5计算功能用于文件唯一性标识
- 修改媒体处理流程,支持音频文件识别和分类处理
- 移除废弃的视频时长转换函数和相关依赖
- 更新依赖版本并添加SQLite支持用于媒体信息存储
2025-03-18 21:04:15 +08:00

130 lines
4.0 KiB
Go

package util
import (
"crypto/md5"
"encoding/hex"
"fmt"
"github.com/gabriel-vasile/mimetype"
"io"
"io/ioutil"
"os"
"path/filepath"
)
// CreateDir 创建目录(含递归创建父目录)
func CreateDir(dirPath string) bool {
err := os.MkdirAll(dirPath, 0755)
return err == nil
}
// GetFileDirectory 获取文件所在文件夹
func GetFileDirectory(filePath string) string {
directoryPath, _ := filepath.Split(filePath)
return directoryPath
}
// ReadFileMimeInfo 获取文件mime信息,失败时返回nil
func ReadFileMimeInfo(filepath string) *mimetype.MIME {
mt, err := mimetype.DetectFile(filepath)
if err != nil {
fmt.Printf("=== 读取Mime信息失败: %s, 错误: %v\n", filepath, err)
return nil
}
return mt
}
// CheckFileIsExist 判断文件是否存在,存在返回 true,不存在返回false
func CheckFileIsExist(filename string) bool {
var exist = true
if _, err := os.Stat(filename); os.IsNotExist(err) {
exist = false
}
return exist
}
// WriteByteArraysToFile 写字节数组到文件
func WriteByteArraysToFile(content []byte, filename string) error {
return ioutil.WriteFile(filename, content, 0777)
}
// CalcFileMd5 计算文件MD5(分块读取,适合大文件)
func CalcFileMd5(filePath string) (string, error) {
file, err := os.Open(filePath)
if err != nil {
return "", err
}
defer file.Close()
h := md5.New()
if _, err := io.Copy(h, file); err != nil {
return "", err
}
return hex.EncodeToString(h.Sum(nil)), nil
}
//// 移动文件到根目录
//func renameFile(rootDir string, modelType string, videoList []string, pathSeparator string) {
// total := len(videoList)
// var count = 0
// bar := goPrint.NewBar(100)
// bar.SetNotice("=== 重命名文件:")
// bar.SetGraph(">")
// for _, videoFilePath := range videoList {
// wh := videoPath2WidthHeightTagMap[videoFilePath]
// fileName := vars.GlobalFilePath2FileNameMap[videoFilePath]
// if strings.Contains(fileName, videoTag) { // 处理过了
// fileNames := strings.Split(fileName, videoTag)
// if len(fileNames) == 2 {
// fileName = fileNames[1]
// targetFilePath := rootDir + pathSeparator + "[" + videoPath2DurationMap[videoFilePath] + "]" + modelType + wh + videoTag + fileName
// err := os.Rename(videoFilePath, targetFilePath)
// if err != nil {
// fmt.Printf("=== 重命名异常: %s \n", videoFilePath)
// }
// }
// } else {
// targetFilePath := rootDir + pathSeparator + "[" + videoPath2DurationMap[videoFilePath] + "]" + modelType + wh + videoTag + " - " + fileName
// err := os.Rename(videoFilePath, targetFilePath)
// if err != nil {
// fmt.Printf("=== 重命名异常: %s \n", videoFilePath)
// }
// }
// count = count + 1
// bar.PrintBar(util.CalcPercentage(count, total))
// }
// bar.PrintEnd("=== Finish")
//}
//
//// 移动文件到原目录
//func renameFileV2(modelType string, videoList []string) {
// total := len(videoList)
// var count = 0
// bar := goPrint.NewBar(100)
// bar.SetNotice("=== 重命名文件:")
// bar.SetGraph(">")
// for _, videoFilePath := range videoList {
// wh := videoPath2WidthHeightTagMap[videoFilePath]
// fileName := vars.GlobalFilePath2FileNameMap[videoFilePath]
// filePath := util.GetFileDirectory(videoFilePath)
// if strings.Contains(fileName, videoTag) { // 处理过了
// fileNames := strings.Split(fileName, videoTag)
// if len(fileNames) == 2 {
// fileName = fileNames[1]
// targetFilePath := filePath + "[" + videoPath2DurationMap[videoFilePath] + "]" + modelType + wh + videoTag + fileName
// err := os.Rename(videoFilePath, targetFilePath)
// if err != nil {
// fmt.Printf("=== 重命名异常: %s \n", videoFilePath)
// }
// }
// } else {
// targetFilePath := filePath + "[" + videoPath2DurationMap[videoFilePath] + "]" + modelType + wh + videoTag + " - " + fileName
// err := os.Rename(videoFilePath, targetFilePath)
// if err != nil {
// fmt.Printf("=== 重命名异常: %s \n", videoFilePath)
// }
// }
// count = count + 1
// bar.PrintBar(util.CalcPercentage(count, total))
// }
// bar.PrintEnd("=== Finish")
//}