实现上传功能
This commit is contained in:
+157
-7
@@ -2,8 +2,14 @@ package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"server/models"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
@@ -178,8 +184,8 @@ func (c *FileController) CreateFile() {
|
||||
}
|
||||
|
||||
// 验证必填字段
|
||||
if file.TenantID == "" || file.FileName == "" || file.OriginalName == "" ||
|
||||
file.FilePath == "" || file.FileType == "" || file.FileExt == "" ||
|
||||
if file.TenantID == "" || file.FileName == "" || file.OriginalName == "" ||
|
||||
file.FilePath == "" || file.FileType == "" || file.FileExt == "" ||
|
||||
file.Category == "" || file.UploadBy == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
@@ -344,7 +350,7 @@ func (c *FileController) GetFileStatistics() {
|
||||
func (c *FileController) SearchFiles() {
|
||||
keyword := c.GetString("keyword")
|
||||
tenantID := c.GetString("tenant_id")
|
||||
|
||||
|
||||
if keyword == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
@@ -353,7 +359,7 @@ func (c *FileController) SearchFiles() {
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
if tenantID == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
@@ -404,7 +410,7 @@ func (c *FileController) GetMyFiles() {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "获取成功",
|
||||
"data": files,
|
||||
"data": files,
|
||||
}
|
||||
}
|
||||
c.ServeJSON()
|
||||
@@ -429,4 +435,148 @@ func (c *FileController) GetFilesPublic() {
|
||||
}
|
||||
|
||||
c.ServeJSON()
|
||||
}
|
||||
}
|
||||
|
||||
// Post 处理文件上传
|
||||
func (c *FileController) Post() {
|
||||
// 从JWT中间件获取用户信息
|
||||
userID, ok := c.Ctx.Input.GetData("userId").(int)
|
||||
if !ok || userID <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "用户未登录或登录已过期",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
username, _ := c.Ctx.Input.GetData("username").(string)
|
||||
if username == "" {
|
||||
username = "unknown"
|
||||
}
|
||||
|
||||
// 获取上传的文件
|
||||
file, header, err := c.GetFile("file")
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "获取上传文件失败: " + err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// 获取文件基本信息
|
||||
originalName := header.Filename
|
||||
fileSize := header.Size
|
||||
fileExt := strings.ToLower(filepath.Ext(originalName))
|
||||
fileName := strings.TrimSuffix(originalName, fileExt)
|
||||
|
||||
// 获取分类(可选)
|
||||
category := c.GetString("category")
|
||||
if category == "" {
|
||||
category = "未分类"
|
||||
}
|
||||
|
||||
// 获取租户ID(可选,从请求参数或中间件获取)
|
||||
tenantID := c.GetString("tenant_id")
|
||||
if tenantID == "" {
|
||||
tenantID = "default"
|
||||
}
|
||||
|
||||
// 生成日期路径(年/月/日)
|
||||
now := time.Now()
|
||||
datePath := now.Format("2006/01/02")
|
||||
|
||||
// 目录改成 server 文件夹目录下的 uploads
|
||||
uploadDir := filepath.Join("uploads", datePath)
|
||||
|
||||
// 清理路径
|
||||
uploadDir = filepath.Clean(uploadDir)
|
||||
|
||||
// 确保目录存在
|
||||
if err := os.MkdirAll(uploadDir, 0755); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "创建上传目录失败: " + err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 生成唯一文件名(时间戳 + 原始文件名)
|
||||
timestamp := now.Format("20060102150405")
|
||||
uniqueFileName := timestamp + "_" + originalName
|
||||
savePath := path.Join(uploadDir, uniqueFileName)
|
||||
|
||||
// 计算相对路径(用于存储到数据库)
|
||||
relativePath := path.Join("uploads", datePath, uniqueFileName)
|
||||
|
||||
// 保存文件
|
||||
if err := c.SaveToFile("file", savePath); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "保存文件失败: " + err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 获取文件类型
|
||||
fileType := "other"
|
||||
switch fileExt {
|
||||
case ".jpg", ".jpeg", ".png", ".gif", ".bmp", ".webp":
|
||||
fileType = "image"
|
||||
case ".pdf", ".doc", ".docx", ".xls", ".xlsx", ".txt":
|
||||
fileType = "document"
|
||||
case ".mp4", ".avi", ".mov", ".wmv":
|
||||
fileType = "video"
|
||||
case ".mp3", ".wav", ".flac":
|
||||
fileType = "audio"
|
||||
case ".zip", ".rar", ".7z":
|
||||
fileType = "archive"
|
||||
}
|
||||
|
||||
// 构造文件URL(相对路径)
|
||||
fileURL := "/" + relativePath
|
||||
|
||||
// 创建文件信息记录
|
||||
fileInfo := models.FileInfo{
|
||||
TenantID: tenantID,
|
||||
UserID: userID,
|
||||
FileName: fileName,
|
||||
OriginalName: originalName,
|
||||
FilePath: relativePath,
|
||||
FileURL: fileURL,
|
||||
FileSize: fileSize,
|
||||
FileType: fileType,
|
||||
FileExt: fileExt,
|
||||
Category: category,
|
||||
UploadBy: username,
|
||||
UploadTime: now,
|
||||
}
|
||||
|
||||
// 保存到数据库
|
||||
id, err := models.AddFile(&fileInfo)
|
||||
if err != nil {
|
||||
// 如果数据库保存失败,删除已上传的文件
|
||||
os.Remove(savePath)
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": false,
|
||||
"message": "保存文件信息失败: " + err.Error(),
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
fileInfo.ID = id
|
||||
|
||||
// 返回成功响应
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"success": true,
|
||||
"message": "文件上传成功",
|
||||
"data": fileInfo,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user