61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
|
|
package service
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"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(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
|
|||
|
|
|
|||
|
|
//保存文件file
|
|||
|
|
dst, err := os.Create(filePath)
|
|||
|
|
if err != nil {
|
|||
|
|
return "", "", err
|
|||
|
|
}
|
|||
|
|
defer dst.Close()
|
|||
|
|
|
|||
|
|
// 复制文件内容
|
|||
|
|
file_, err := file.Open()
|
|||
|
|
if err != nil {
|
|||
|
|
return "", "", err
|
|||
|
|
}
|
|||
|
|
_, err = io.Copy(dst, file_)
|
|||
|
|
if err != nil {
|
|||
|
|
return "", "", err
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if uploadType == "2" {
|
|||
|
|
worker.PushRedisList("video_need_handle", filePath)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return path_, fileStoreName, nil
|
|||
|
|
}
|