2025-03-18 13:22:20 +08:00
|
|
|
|
package service
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"StuAcaWorksAI/dao"
|
|
|
|
|
|
"StuAcaWorksAI/proto"
|
|
|
|
|
|
"StuAcaWorksAI/worker"
|
|
|
|
|
|
"crypto/md5"
|
2025-03-28 19:09:34 +08:00
|
|
|
|
"errors"
|
2025-03-18 13:22:20 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
"io"
|
2025-03-29 13:58:40 +08:00
|
|
|
|
"log"
|
2025-03-18 13:22:20 +08:00
|
|
|
|
"mime/multipart"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path"
|
|
|
|
|
|
"regexp"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// 检查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.AddConfigFileReq, 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
|
|
|
|
|
|
}
|
|
|
|
|
|
if req.FileName == "" || req.FilePath == "" {
|
|
|
|
|
|
err = fmt.Errorf("file name or file path is empty")
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
_, fileErr := os.Stat(file)
|
|
|
|
|
|
if fileErr != 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, FilePath: config_file.FilePath, 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
|
|
|
|
|
|
}
|
2025-03-28 19:09:34 +08:00
|
|
|
|
|
|
|
|
|
|
func UpdateUserFile(userID int, fileAuthID int, fileAuthName string) error {
|
|
|
|
|
|
fileAuth := dao.FindFileAuthByID(fileAuthID)
|
|
|
|
|
|
if fileAuth.ID == 0 {
|
|
|
|
|
|
return errors.New("file auth not found")
|
|
|
|
|
|
}
|
|
|
|
|
|
if fileAuth.AuthID != userID {
|
|
|
|
|
|
return errors.New("no permission")
|
|
|
|
|
|
}
|
|
|
|
|
|
fileAuth.UserFileName = fileAuthName
|
|
|
|
|
|
err := dao.UpdateFileAuthByID(userID, fileAuth)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
|
|
|
|
|
func DeleteUserFile(userID, fileAuthID int) error {
|
|
|
|
|
|
fileAuth := dao.FindFileAuthByID(fileAuthID)
|
|
|
|
|
|
if fileAuth.ID == 0 {
|
|
|
|
|
|
return errors.New("file auth not found")
|
|
|
|
|
|
}
|
|
|
|
|
|
if fileAuth.AuthID != userID {
|
|
|
|
|
|
return errors.New("no permission")
|
|
|
|
|
|
}
|
|
|
|
|
|
err := dao.DeleteFileAuthByID(fileAuthID)
|
|
|
|
|
|
return err
|
|
|
|
|
|
}
|
2025-03-28 19:53:25 +08:00
|
|
|
|
|
|
|
|
|
|
func CreateUserFile(userID int, fileAuthName string, fileID int, UploadType string) dao.FileAuth {
|
|
|
|
|
|
fileAuth := dao.CreateFileAuth(userID, fileID, fileAuthName, UploadType, 1, "")
|
|
|
|
|
|
return fileAuth
|
|
|
|
|
|
}
|
2025-03-29 13:38:30 +08:00
|
|
|
|
|
2025-03-29 14:02:21 +08:00
|
|
|
|
func FindUserFileList(userID int, uploadType string) []dao.UserFileListResp {
|
2025-03-29 13:38:30 +08:00
|
|
|
|
fileList, files := dao.FileUserFileList(userID, uploadType)
|
|
|
|
|
|
filesM := make(map[uint]dao.File)
|
|
|
|
|
|
for _, file := range files {
|
|
|
|
|
|
filesM[file.ID] = file
|
|
|
|
|
|
}
|
2025-03-29 14:02:21 +08:00
|
|
|
|
var res []dao.UserFileListResp
|
2025-03-29 13:38:30 +08:00
|
|
|
|
for _, file := range fileList {
|
|
|
|
|
|
fileStoreName := filesM[uint(file.FileID)].FileStoreName
|
2025-03-29 14:02:21 +08:00
|
|
|
|
r := dao.UserFileListResp{FileAuth: file, FileStoreName: fileStoreName}
|
2025-03-29 13:38:30 +08:00
|
|
|
|
res = append(res, r)
|
|
|
|
|
|
}
|
2025-03-29 13:58:40 +08:00
|
|
|
|
log.Println("FindUserFileList res:", len(res))
|
2025-03-29 13:38:30 +08:00
|
|
|
|
return res
|
|
|
|
|
|
}
|