videoplayer/service/fileService.go

48 lines
1.2 KiB
Go
Raw Permalink Normal View History

2024-08-30 11:28:27 +08:00
package service
import (
2024-08-30 21:46:28 +08:00
"github.com/gin-gonic/gin"
2024-08-30 11:28:27 +08:00
"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
}
2024-08-30 21:46:28 +08:00
func SaveFile(c *gin.Context, file *multipart.FileHeader, uploadType string) (string, string, error) {
2024-08-30 11:28:27 +08:00
//获取文件后缀
fileSuffix := path.Ext(file.Filename)
//生成文件名
fileStoreName := uuid.NewString() + fileSuffix
//生成文件路径
path_ := getFilePath(proto.FILE_BASE_DIR)
filePath := path_ + "/" + fileStoreName
2024-08-30 21:46:28 +08:00
//保存文件
if err := c.SaveUploadedFile(file, filePath); err != nil {
2024-08-30 11:28:27 +08:00
return "", "", err
}
if uploadType == "2" {
worker.PushRedisList("video_need_handle", filePath)
}
return path_, fileStoreName, nil
}