SoulAssetController.go 3.3 KB
Newer Older
Ford committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
package controllers

import (
	"WorldEpcho/src/utils"
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"os"
	"path/filepath"
	"strings"
)

// 处理文件夹上传
func UploadFolderHandler(c *gin.Context, digitalId int64) (string, error) {
	err := c.Request.ParseMultipartForm(0) // 不限制上传文件大小
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": err.Error(),
		})
		return "", errors.New("获取上传的文件失败")
	}
	//digitalId := 1
	// 数字人ID转换为字符串
	digital_Id := utils.Strval(digitalId)
	modePath := filepath.Join("./static/HuaSoul/asset/", digital_Id)
	fmt.Println("数字人模型资产上传路径 modePath: ", modePath)
	// 检查上传的文件是否存在
	form, err := c.MultipartForm()
	files := form.File["mode_file"] // 'files' 是前端 JavaScript 中指定的字段名
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "missing uploaded file"})
		return "", errors.New("丢失上传的文件")
	}
	// 检查目标文件夹是否存在,如果存在则删除
	if _, err := os.Stat(modePath); !os.IsNotExist(err) {
		if err := os.RemoveAll(modePath); err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code":    0,
				"message": "failed to remove existing model folder",
			})
			return "", errors.New("failed to remove existing model folder")
			fmt.Println("删除已存在的数字人模型资产文件夹失败!")
		}
		fmt.Println("检查数字人模型资产文件夹,存在则删除!")
	}
	// 创建存放模型文件的文件夹
	if err := os.MkdirAll(modePath, 0755); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    0,
			"message": "failed to create model folder",
		})
		return "", errors.New("failed to create model folder")
	}
	file1 := files[0].Header["Content-Disposition"]
	path1, _ := GetFileName(file1)
	fmt.Println("path1", path1)

	FirstPath := strings.Split(path1, "/")
	fmt.Println(FirstPath[0])

	// 遍历上传的文件
	for _, file := range files {
		fileName := file.Header["Content-Disposition"]
		path, _ := GetFileName(fileName)
		fmt.Println("path: ", path)
		savePath := filepath.Join(modePath, path) // 保存文件的路径,确保'uploads'文件夹已存在或自动创建
		if err := c.SaveUploadedFile(file, savePath); err != nil {
			c.String(http.StatusOK, fmt.Sprintf("'%s' could not be saved: %v", file.Filename, err))
			return "", err
		}
	}
	// 上传成功后返回响应
	//c.String(http.StatusOK, fmt.Sprintf("%d files uploaded!", len(files)))
	fmt.Sprintf("%d files uploaded!", len(files))
	return FirstPath[0], nil
}

//fileName [form-data; name="model_folder"; filename="test/头像.jpg"] ;取出文件名
func GetFileName(fileName []string) (string, error) {
	for _, f := range fileName {
		// 判断字符串是否包含了需要查找的文件名关键字
		if strings.Contains(f, `filename=`) {
			// 按照 filename=" 分割
			parts := strings.Split(f, `filename="`)
			if len(parts) < 2 {
				// 没有找到分隔符,跳到下一个元素
				continue
			}
			// 按照 " 分割以获取实际的文件路径
			filePathParts := strings.SplitN(parts[1], `"`, 2)
			// 如果成功找到路径就返回
			if len(filePathParts) >= 2 {
				return filePathParts[0], nil // 返回找到的路径
			}
		}
	}
	// 如果没有找到,返回错误
	return "", fmt.Errorf("no path found")
}