Files
nl-im-service/internal/api/attachment_handler.go
2025-12-03 11:00:47 +08:00

154 lines
3.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* package api
* 作用附件管理相关API处理器
*/
package api
import (
"strconv"
"xk-websocket-v2/internal/model"
"xk-websocket-v2/internal/service"
"xk-websocket-v2/internal/utils"
"github.com/gin-gonic/gin"
)
/**
* UploadAttachmentHandler
* 功能:上传附件
* 路径POST /api/attachments/upload
*/
func UploadAttachmentHandler(c *gin.Context) {
userID, _ := c.Get("user_id")
// 获取文件
file, err := c.FormFile("file")
if err != nil {
utils.BadRequest(c, "文件上传失败: "+err.Error())
return
}
// 获取文件类型
fileType := c.PostForm("type")
if fileType == "" {
// 根据文件扩展名推断类型
ext := file.Filename[len(file.Filename)-4:]
if ext == ".jpg" || ext == ".png" || ext == ".gif" || ext == "webp" || ext == "jpeg" {
fileType = "image"
} else {
fileType = "video"
}
}
// 验证文件类型
if fileType != "image" && fileType != "video" {
utils.BadRequest(c, "文件类型必须是image或video")
return
}
// 验证文件大小
if fileType == "image" && file.Size > model.MaxImageSize {
utils.BadRequest(c, "图片大小不能超过10MB")
return
}
if fileType == "video" && file.Size > model.MaxVideoSize {
utils.BadRequest(c, "视频大小不能超过500MB")
return
}
// 打开文件
src, err := file.Open()
if err != nil {
utils.BadRequest(c, "打开文件失败: "+err.Error())
return
}
defer src.Close()
// 上传文件
attachment, err := service.AttachmentSvc.UploadFile(
userID.(string),
file.Filename,
fileType,
file.Size,
src,
)
if err != nil {
utils.BadRequest(c, err.Error())
return
}
utils.SuccessWithData(c, attachment, "上传成功")
}
/**
* GetAttachmentHandler
* 功能:获取附件信息
* 路径GET /api/attachments/:id
*/
func GetAttachmentHandler(c *gin.Context) {
attachmentID, _ := strconv.ParseUint(c.Param("id"), 10, 32)
attachment, err := service.AttachmentSvc.GetAttachment(uint(attachmentID))
if err != nil {
utils.NotFound(c, "附件不存在")
return
}
utils.SuccessWithData(c, attachment, "获取成功")
}
/**
* DeleteAttachmentHandler
* 功能:删除附件(仅上传者可删除)
* 路径DELETE /api/attachments/:id
*/
func DeleteAttachmentHandler(c *gin.Context) {
userID, _ := c.Get("user_id")
attachmentID, _ := strconv.ParseUint(c.Param("id"), 10, 32)
if err := service.AttachmentSvc.DeleteAttachment(uint(attachmentID), userID.(string)); err != nil {
utils.BadRequest(c, err.Error())
return
}
utils.Success(c, "附件已删除")
}
/**
* GetAttachmentsHandler
* 功能:获取附件列表
* 路径GET /api/attachments
*/
func GetAttachmentsHandler(c *gin.Context) {
userID, _ := c.Get("user_id")
fileType := c.Query("type")
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
pageSize, _ := strconv.Atoi(c.DefaultQuery("page_size", "20"))
if page < 1 {
page = 1
}
if pageSize < 1 || pageSize > 100 {
pageSize = 20
}
attachments, total, err := service.AttachmentSvc.GetUserAttachments(userID.(string), fileType, page, pageSize)
if err != nil {
utils.InternalError(c, "查询失败")
return
}
// 确保返回空数组而不是null
if attachments == nil {
attachments = []model.Attachment{}
}
utils.SuccessWithData(c, gin.H{
"data": attachments,
"total": total,
"page": page,
"size": pageSize,
}, "获取成功")
}