2023-05-18 11:01:34 +08:00
|
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/md5"
|
2023-06-17 09:38:26 +08:00
|
|
|
|
"encoding/base64"
|
2023-05-18 11:01:34 +08:00
|
|
|
|
"encoding/hex"
|
|
|
|
|
"fmt"
|
|
|
|
|
"git.hpds.cc/Component/logging"
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
"hpds-iot-web/pkg/minio"
|
|
|
|
|
"io"
|
|
|
|
|
"os"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func CopyFile(src, dst string) error {
|
|
|
|
|
sourceFileStat, err := os.Stat(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !sourceFileStat.Mode().IsRegular() {
|
|
|
|
|
return fmt.Errorf("%s is not a regular file", src)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
source, err := os.Open(src)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer func(source *os.File) {
|
|
|
|
|
_ = source.Close()
|
|
|
|
|
}(source)
|
|
|
|
|
|
|
|
|
|
destination, err := os.Create(dst)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
defer func(destination *os.File) {
|
|
|
|
|
_ = destination.Close()
|
|
|
|
|
}(destination)
|
|
|
|
|
_, err = io.Copy(destination, source)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func PathExists(path string) bool {
|
|
|
|
|
_, err := os.Stat(path)
|
|
|
|
|
if err == nil {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ReadFile 读取到file中,再利用ioutil将file直接读取到[]byte中, 这是最优
|
|
|
|
|
func ReadFile(fn string) []byte {
|
|
|
|
|
f, err := os.Open(fn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logging.L().Error("Read File", zap.String("File Name", fn), zap.Error(err))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
defer func(f *os.File) {
|
|
|
|
|
_ = f.Close()
|
|
|
|
|
}(f)
|
|
|
|
|
|
|
|
|
|
fd, err := io.ReadAll(f)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logging.L().Error("Read File To buff", zap.String("File Name", fn), zap.Error(err))
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fd
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func GetFileName(fn string) string {
|
|
|
|
|
fileType := path.Ext(fn)
|
|
|
|
|
return strings.TrimSuffix(fn, fileType)
|
|
|
|
|
}
|
|
|
|
|
func GetFileNameAndExt(fn string) string {
|
|
|
|
|
_, fileName := filepath.Split(fn)
|
|
|
|
|
return fileName
|
|
|
|
|
}
|
|
|
|
|
func GetFileMd5(data []byte) string {
|
|
|
|
|
hash := md5.New()
|
|
|
|
|
hash.Write(data)
|
|
|
|
|
return hex.EncodeToString(hash.Sum(nil))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DownloadMinioFileToLocalPath(accessUrl, dstPath, fileName, protocol, endpoint, bucket, accessKeyId, secretAccessKey string,
|
|
|
|
|
logger *logging.Logger) {
|
|
|
|
|
|
|
|
|
|
if !PathExists(path.Join(dstPath, fileName)) {
|
|
|
|
|
dPath := strings.Replace(accessUrl, fmt.Sprintf("%s://%s/", protocol, endpoint), "", 1)
|
|
|
|
|
|
|
|
|
|
dPath = strings.Replace(dPath, bucket, "", 1)
|
|
|
|
|
minioCli := minio.NewClient(accessKeyId, secretAccessKey, endpoint, false, logger)
|
|
|
|
|
|
|
|
|
|
imgByte, err := minioCli.GetObject(dPath, bucket)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.With(zap.String("源文件名", accessUrl)).
|
|
|
|
|
With(zap.String("文件名", path.Join(dstPath, fileName))).
|
|
|
|
|
Error("文件下载", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
err = os.MkdirAll(dstPath, os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.With(zap.String("源文件名", accessUrl)).
|
|
|
|
|
With(zap.String("文件名", path.Join(dstPath, fileName))).
|
|
|
|
|
Error("创建文件下载目录", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
err = os.WriteFile(path.Join(dstPath, fileName), imgByte, os.ModePerm)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logger.With(zap.String("源文件名", accessUrl)).
|
|
|
|
|
With(zap.String("文件名", path.Join(dstPath, fileName))).
|
|
|
|
|
Error("文件写入", zap.Error(err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-17 09:38:26 +08:00
|
|
|
|
|
|
|
|
|
func FileToBase64(fn string) string {
|
|
|
|
|
buff := ReadFile(fn)
|
|
|
|
|
return base64.StdEncoding.EncodeToString(buff) // 加密成base64字符串
|
|
|
|
|
}
|