| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package system
- import (
- "path/filepath"
- "strconv"
- "strings"
- "github.com/gin-gonic/gin"
- "go_server/base/config"
- "go_server/global"
- "go_server/model/biz_modules/daytask"
- "go_server/model/common/response"
- "go_server/service/base"
- "go_server/utils"
- "gorm.io/gorm"
- )
- type FileService struct {
- base.SysCommonService
- }
- func (s *FileService) UploadFile(c *gin.Context) {
- _, header, err := c.Request.FormFile("file")
- if err != nil {
- response.Resp(c, "接收文件失败")
- return
- }
- path, file, err := config.EnvConf().File.UploadFile(header) // 文件上传后拿到文件路径
- if err != nil {
- response.Resp(c, err.Error())
- return
- }
- // 自动保存到素材库
- groupIdStr := c.PostForm("groupId")
- groupCode := c.PostForm("groupCode") // 分组代码,如 banner, task, notice 等
- autoSave := c.PostForm("autoSave") // 是否自动保存到素材库,默认true
- // 默认自动保存,除非明确设置为 "false" 或 "0"
- shouldSave := autoSave != "false" && autoSave != "0"
- if shouldSave {
- // 获取daytask数据库连接
- db, dbErr := global.BizDBByAlias("daytask")
- if dbErr == nil && db != nil {
- // 解析文件信息
- filename := header.Filename
- fileSize := header.Size
- mimeType := header.Header.Get("Content-Type")
- // 判断文件类型
- fileType := getFileType(mimeType, filename)
- // 获取groupId
- var groupId int64 = 0
- if groupIdStr != "" {
- groupId, _ = strconv.ParseInt(groupIdStr, 10, 64)
- } else if groupCode != "" {
- // 通过groupCode查找或创建分组
- groupId = getOrCreateGroupByCode(db, groupCode)
- }
- // 获取管理员ID
- adminId := c.GetInt64("userId")
- // 创建素材记录(不包含cover字段)
- material := daytask.DtMaterial{
- Name: filename,
- Path: file,
- Url: path,
- Type: fileType,
- MimeType: mimeType,
- Size: fileSize,
- Storage: "local",
- GroupId: groupId,
- AdminId: adminId,
- Status: 1,
- }
- // 保存到数据库,排除cover字段
- db.Omit("cover").Create(&material)
- }
- }
- response.Resp(c, map[string]interface{}{
- "path": path,
- "file": file,
- })
- }
- // getOrCreateGroupByCode 通过分组代码获取或创建分组
- func getOrCreateGroupByCode(db *gorm.DB, code string) int64 {
- // 分组代码与名称的映射
- codeNameMap := map[string]string{
- "banner": "Banner图片",
- "task": "任务素材",
- "task_icon": "任务图标",
- "category_icon": "分类图标",
- "notice": "公告素材",
- "help": "帮助中心",
- "avatar": "用户头像",
- "default": "默认分组",
- }
- // 查找已存在的分组
- var group daytask.DtMaterialGroup
- result := db.Where("code = ?", code).First(&group)
- if result.Error == nil {
- return group.Id
- }
- // 分组不存在,创建新分组
- groupName := codeNameMap[code]
- if groupName == "" {
- groupName = code // 如果没有映射,直接使用code作为名称
- }
- newGroup := daytask.DtMaterialGroup{
- Name: groupName,
- Code: code,
- Status: 1,
- }
- db.Create(&newGroup)
- return newGroup.Id
- }
- // getFileType 根据MIME类型和文件名判断文件类型
- func getFileType(mimeType, filename string) string {
- // 优先根据MIME类型判断
- if strings.HasPrefix(mimeType, "image/") {
- return "image"
- }
- if strings.HasPrefix(mimeType, "video/") {
- return "video"
- }
- if strings.HasPrefix(mimeType, "audio/") {
- return "audio"
- }
- // 根据文件扩展名判断
- ext := strings.ToLower(filepath.Ext(filename))
- switch ext {
- case ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp", ".svg", ".ico":
- return "image"
- case ".mp4", ".avi", ".mov", ".wmv", ".flv", ".mkv", ".webm":
- return "video"
- case ".mp3", ".wav", ".ogg", ".flac", ".aac", ".m4a":
- return "audio"
- case ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".txt":
- return "document"
- default:
- return "other"
- }
- }
- func (s *FileService) DeleteFile(c *gin.Context) {
- filename, ok := c.GetQuery("filename")
- if !ok {
- response.Resp(c, "接收文件失败")
- return
- }
- err := config.EnvConf().File.DeleteFile(filename) // 文件上传后拿到文件路径
- if err != nil {
- response.Resp(c, err.Error())
- return
- }
- response.Resp(c)
- }
- func (s *FileService) OssAuth(c *gin.Context) {
- resp, err := config.StsAliEngin().AliSTS("fomo-sts")
- if err != nil {
- response.Resp(c, err.Error())
- return
- }
- savePath := utils.ToPath(config.EnvConf().StsOss.BasePath)
- response.Resp(c, map[string]any{
- "region": config.EnvConf().StsOss.StsRegion,
- "bucket": config.EnvConf().StsOss.BucketName,
- "bucketUrl": config.EnvConf().StsOss.BucketUrl,
- "credentials": resp.Body.Credentials,
- "filePath": savePath,
- })
- }
|