videoplayer/service/fileService.go

58 lines
1.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package service
import (
"crypto/md5"
"fmt"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"io"
"mime/multipart"
"os"
"path"
"time"
"videoplayer/proto"
"videoplayer/worker"
)
// 检查path是否存在当前日期文件夹如2024-08-09不存在则path下当前日期文件夹创建存在则返回
func getFilePath(path string) string {
//当前日期格式为2024-08-09
date := time.Now().Format("2006-01-02")
//拼接文件路径
filePath := path + "/" + date
//判断文件夹是否存在
_, err := os.Stat(filePath)
if err != nil {
//不存在则创建
os.MkdirAll(filePath, os.ModePerm)
}
return filePath
}
func SaveFile(c *gin.Context, file *multipart.FileHeader, uploadType string) (string, string, error) {
//获取文件后缀
fileSuffix := path.Ext(file.Filename)
//生成文件名
fileStoreName := uuid.NewString() + fileSuffix
//生成文件路径
path_ := getFilePath(proto.FILE_BASE_DIR)
filePath := path_ + "/" + fileStoreName
//保存文件
if err := c.SaveUploadedFile(file, filePath); err != nil {
return "", "", err
}
if uploadType == "2" {
worker.PushRedisList("video_need_handle", filePath)
}
return path_, fileStoreName, nil
}
func CalculateFileMd5(file io.Reader) string {
hash := md5.New()
if _, err := io.Copy(hash, file); err != nil {
return ""
}
return fmt.Sprintf("%x", hash.Sum(nil))
}