feat: file upload and download
This commit is contained in:
+49
-8
@@ -4,10 +4,12 @@ package controllers
|
||||
|
||||
import (
|
||||
"context" // 在此处添加 context 导入
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/sashabaranov/go-openai"
|
||||
"hr_receiver/config"
|
||||
"hr_receiver/models"
|
||||
"hr_receiver/util"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
@@ -15,6 +17,9 @@ import (
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -57,6 +62,14 @@ func readDocxContent(fileHeader *multipart.FileHeader) (string, error) {
|
||||
return str, nil
|
||||
}
|
||||
|
||||
func readDocxContentFromPath(filePath string) (string, error) {
|
||||
str, err := util.DocxToStructuredPrompt(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to parse docx with go-docx: %w", err)
|
||||
}
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// readCSVContent 读取 .csv 文件内容
|
||||
// 修改为先保存临时文件再读取
|
||||
func readCSVContent(fileHeader *multipart.FileHeader) (string, error) {
|
||||
@@ -246,16 +259,19 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
}
|
||||
|
||||
// 2. 获取文件列表
|
||||
docxFiles := form.File["teaching_plan"] // 假设前端字段名为 'teaching_plan'
|
||||
csvFiles := form.File["heart_rate_data"] // 假设前端字段名为 'heart_rate_data'
|
||||
stepFiles := form.File["step_data"]
|
||||
analysisType := c.PostForm("analysis_type")
|
||||
teachingPlanSource := c.PostForm("teaching_plan_source")
|
||||
if analysisType == "" {
|
||||
analysisType = analysisTypeHeartRateOnly
|
||||
}
|
||||
if teachingPlanSource == "" {
|
||||
teachingPlanSource = "upload"
|
||||
}
|
||||
|
||||
if len(docxFiles) == 0 || len(csvFiles) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing required files: teaching_plan (.docx) or heart_rate_data (.csv)"})
|
||||
if len(csvFiles) == 0 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Missing required file: heart_rate_data (.csv)"})
|
||||
return
|
||||
}
|
||||
if analysisType == analysisTypeHeartRateWithSteps && len(stepFiles) == 0 {
|
||||
@@ -265,13 +281,15 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
|
||||
// 3. 读取文件内容
|
||||
// 注意:这里我们只取第一个上传的文件
|
||||
teachingPlanFileHeader := docxFiles[0]
|
||||
heartRateFileHeader := csvFiles[0]
|
||||
|
||||
teachingPlanContent, err := readDocxContent(teachingPlanFileHeader)
|
||||
teachingPlanContent, err := resolveTeachingPlanContent(c, form, teachingPlanSource)
|
||||
if err != nil {
|
||||
log.Printf("Error reading teaching plan file (%s): %v", teachingPlanFileHeader.Filename, err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to process teaching plan file: %v", err)})
|
||||
log.Printf("Error resolving teaching plan: %v", err)
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Cloud teaching plan file not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -314,3 +332,26 @@ func (tc *TrainingController) AnalyzeByAI(c *gin.Context) {
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func resolveTeachingPlanContent(c *gin.Context, form *multipart.Form, source string) (string, error) {
|
||||
switch strings.ToLower(strings.TrimSpace(source)) {
|
||||
case "upload":
|
||||
docxFiles := form.File["teaching_plan"]
|
||||
if len(docxFiles) == 0 {
|
||||
return "", fmt.Errorf("Missing required file: teaching_plan (.docx)")
|
||||
}
|
||||
return readDocxContent(docxFiles[0])
|
||||
case "cloud":
|
||||
lessonPlanID := c.PostForm("lesson_plan_id")
|
||||
if strings.TrimSpace(lessonPlanID) == "" {
|
||||
return "", fmt.Errorf("missing required field: lesson_plan_id")
|
||||
}
|
||||
var fileRecord models.AppFile
|
||||
if err := config.DB.Where("id = ? AND file_type = ?", lessonPlanID, models.AppFileTypeLessonPlan).First(&fileRecord).Error; err != nil {
|
||||
return "", err
|
||||
}
|
||||
return readDocxContentFromPath(fileRecord.FilePath)
|
||||
default:
|
||||
return "", fmt.Errorf("invalid teaching_plan_source, expected upload or cloud")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user