48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package service
|
||
|
||
import (
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/google/uuid"
|
||
"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
|
||
}
|