216 lines
5.7 KiB
Go
216 lines
5.7 KiB
Go
package service
|
||
|
||
import (
|
||
"crypto/md5"
|
||
"fmt"
|
||
"github.com/gin-gonic/gin"
|
||
"github.com/google/uuid"
|
||
"io"
|
||
"mime/multipart"
|
||
"os"
|
||
"path"
|
||
"regexp"
|
||
"time"
|
||
"videoplayer/dao"
|
||
"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))
|
||
}
|
||
|
||
func CheckUploadRequestParameters(req *proto.FileUploadReq) error {
|
||
var err error
|
||
if req.AuthType == "" {
|
||
err = fmt.Errorf("auth_type is empty")
|
||
}
|
||
if req.UploadType == "" {
|
||
req.UploadType = "1"
|
||
}
|
||
if req.UploadType != "1" {
|
||
req.UploadType = "2"
|
||
}
|
||
if proto.File_Type[req.Type] == 0 {
|
||
err = fmt.Errorf("file type is invalid")
|
||
}
|
||
return err
|
||
}
|
||
|
||
func CreateConfigFile(req *proto.ConfigFileReq, userId int) error {
|
||
var err error
|
||
user := GetUserByIDWithCache(userId)
|
||
if user.ID == 0 || user.Role != "admin" {
|
||
err = fmt.Errorf("user not found or no permission")
|
||
return err
|
||
}
|
||
//查看系统中是否存在文件,不存在则创建
|
||
file := req.FilePath + "/" + req.FileName
|
||
//正则判断文件名是否合法
|
||
pattern := `^/([^/:\*?]+/)*([^/:\*?]+)?$`
|
||
reg := regexp.MustCompile(pattern)
|
||
if reg.MatchString(file) == false {
|
||
err = fmt.Errorf("file path is invalid")
|
||
return err
|
||
}
|
||
_, err = os.Stat(file)
|
||
if err != nil {
|
||
//创建文件
|
||
f, err2 := os.Create(file)
|
||
if err2 != nil {
|
||
err = err2
|
||
return err
|
||
}
|
||
defer func(f *os.File) {
|
||
err := f.Close()
|
||
if err != nil {
|
||
fmt.Println("Error closing file")
|
||
}
|
||
}(f)
|
||
}
|
||
//创建
|
||
configFile := dao.ConfigFile{FilePath: req.FilePath, FileName: req.FileName, AuthID: userId}
|
||
_, err3 := dao.CreateConfigFile(configFile)
|
||
if err3 != nil {
|
||
err = err3
|
||
return err
|
||
}
|
||
return err
|
||
}
|
||
|
||
func DeleteConfigFile(req *proto.ConfigFileReq, userId int) error {
|
||
var err error
|
||
user := GetUserByIDWithCache(userId)
|
||
if user.ID == 0 || user.Role != "admin" {
|
||
err = fmt.Errorf("user not found or no permission")
|
||
return err
|
||
}
|
||
//删除文件
|
||
config_file := dao.FindConfigFileByID(req.ID, userId)
|
||
if config_file.ID == 0 {
|
||
err = fmt.Errorf("config file not found")
|
||
return err
|
||
}
|
||
err = dao.DeleteConfigFileByID(req.ID)
|
||
if req.DelFile {
|
||
file := config_file.FilePath + "/" + config_file.FileName
|
||
err = os.Remove(file)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
//删除数据库记录
|
||
return err
|
||
}
|
||
|
||
type ConfigFileService struct {
|
||
}
|
||
|
||
func (c *ConfigFileService) UpdateConfigFile(req *proto.ConfigFileReq, userId int) error {
|
||
var err error
|
||
user := GetUserByIDWithCache(userId)
|
||
if user.ID == 0 || user.Role != "admin" {
|
||
err = fmt.Errorf("user not found or no permission")
|
||
return err
|
||
}
|
||
config_file := dao.FindConfigFileByID(req.ID, userId)
|
||
if config_file.ID == 0 {
|
||
err = fmt.Errorf("config file not found")
|
||
return err
|
||
}
|
||
//修改文件名
|
||
if req.FileName != "" {
|
||
file := config_file.FilePath + "/" + config_file.FileName
|
||
new_file := config_file.FilePath + "/" + req.FileName
|
||
err = os.Rename(file, new_file)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
err = dao.UpdateConfigFileByID(req.ID, dao.ConfigFile{FileName: req.FileName})
|
||
}
|
||
if req.Content != "" {
|
||
file := config_file.FilePath + "/" + config_file.FileName
|
||
f, err2 := os.OpenFile(file, os.O_WRONLY|os.O_TRUNC, 0644) //打开文件,清空文件内容,写入新内容,不存在则创建
|
||
if err2 != nil {
|
||
err = err2
|
||
return err
|
||
}
|
||
defer func(f *os.File) {
|
||
err3 := f.Close()
|
||
if err3 != nil {
|
||
fmt.Println("Error closing file")
|
||
}
|
||
}(f)
|
||
_, err = f.WriteString(req.Content)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
}
|
||
return err
|
||
}
|
||
|
||
func (c *ConfigFileService) SearchOneConfigFile(req *proto.ConfigFileReq, userId int) ([]proto.SearchOneConfigFileResp, error) {
|
||
user := GetUserByIDWithCache(userId)
|
||
if user.ID == 0 || user.Role != "admin" {
|
||
return []proto.SearchOneConfigFileResp{}, fmt.Errorf("user not found or no permission")
|
||
}
|
||
config_file := dao.FindConfigFileByID(req.ID, userId)
|
||
if config_file.ID == 0 {
|
||
return []proto.SearchOneConfigFileResp{}, fmt.Errorf("config file not found")
|
||
}
|
||
file := config_file.FilePath + "/" + config_file.FileName
|
||
content, err2 := os.ReadFile(file)
|
||
if err2 != nil {
|
||
return []proto.SearchOneConfigFileResp{}, err2
|
||
}
|
||
resp := []proto.SearchOneConfigFileResp{{ID: config_file.ID, FileName: config_file.FileName, Content: string(content), CreatedAt: config_file.CreatedAt, UpdatedAt: config_file.UpdatedAt}}
|
||
return resp, nil
|
||
}
|
||
|
||
func (c *ConfigFileService) SearchAllConfigFile(userId int) ([]dao.ConfigFile, error) {
|
||
user := GetUserByIDWithCache(userId)
|
||
if user.ID == 0 || user.Role != "admin" {
|
||
return []dao.ConfigFile{}, fmt.Errorf("user not found or no permission")
|
||
}
|
||
config_files := dao.FindConfigFileByAuthID(userId)
|
||
return config_files, nil
|
||
}
|