MCPcopy Create free account
hub / github.com/53AI/53AIHub / executeWPSFileUpload

Function executeWPSFileUpload

api/controller/wps.go:708–923  ·  view source on GitHub ↗

executeWPSFileUpload 执行WPS文件上传

(c *gin.Context)

Source from the content-addressed store, hash-verified

706
707// executeWPSFileUpload 执行WPS文件上传
708func executeWPSFileUpload(c *gin.Context) (any, error) {
709 // 记录请求开始
710 logger.Infof(c, "开始处理WPS文件上传请求,文件ID: %s", c.Param("file_id"))
711
712 // 获取文件信息
713 fileIDStr := c.Param("file_id")
714 // 转换成 int64
715 fileID, err := strconv.ParseInt(fileIDStr, 10, 64)
716 if err != nil {
717 return nil, weboffice.ErrInvalidArguments.WithMessage("无效的文件ID")
718 }
719
720 // 获取上传许可
721 uploadToken := c.GetHeader("X-Upload-Token")
722 if uploadToken == "" {
723 logger.Error(c, "缺少上传许可")
724 return nil, weboffice.ErrPermissionDenied.WithMessage("缺少上传许可")
725 }
726
727 logger.Infof(c, "获取到上传许可: %s", uploadToken)
728
729 // 从 Redis 验证上传许可
730 redisKey := fmt.Sprintf("wps_upload_token_%s", uploadToken)
731 uploadTokenRaw, err := common.RedisGet(redisKey)
732 if err != nil {
733 logger.Errorf(c, "从Redis验证上传许可失败: %v", err)
734 return nil, weboffice.ErrPermissionDenied.WithMessage("上传许可无效或已过期")
735 }
736
737 if uploadTokenRaw == "" {
738 logger.Errorf(c, "上传许可不存在,Redis Key: %s", redisKey)
739 return nil, weboffice.ErrPermissionDenied.WithMessage("上传许可不存在")
740 }
741
742 // 从uploadTokenRaw中提取文件ID并验证
743 // uploadTokenRaw格式: "wps_upload_%d_%d_%d",第一个%d是文件ID
744 parts := strings.Split(uploadTokenRaw, "_")
745 if len(parts) < 4 {
746 logger.Errorf(c, "上传许可格式无效: %s", uploadTokenRaw)
747 return nil, weboffice.ErrPermissionDenied.WithMessage("上传许可格式无效")
748 }
749
750 tokenFileID, err := strconv.ParseInt(parts[2], 10, 64)
751 if err != nil {
752 logger.Errorf(c, "解析上传许可中的文件ID失败: %v", err)
753 return nil, weboffice.ErrPermissionDenied.WithMessage("上传许可中的文件ID无效")
754 }
755
756 // 验证文件ID是否匹配
757 if tokenFileID != fileID {
758 logger.Errorf(c, "文件ID不匹配,上传许可中的文件ID: %d,请求的文件ID: %d", tokenFileID, fileID)
759 return nil, weboffice.ErrPermissionDenied.WithMessage("文件ID不匹配")
760 }
761
762 logger.Infof(c, "文件ID验证成功,文件ID: %d", fileID)
763
764 file, wpsErr := model.GetFileByIDOlny(fileID)
765 if wpsErr != nil {

Callers

nothing calls this directly

Calls 12

calculateFileHashFunction · 0.85
InfofMethod · 0.80
WithMessageMethod · 0.80
ErrorfMethod · 0.80
WarnfMethod · 0.80
LoadUploadFileMethod · 0.80
InfoMethod · 0.80
generateUniqueFileNameFunction · 0.70
QueryMethod · 0.65
SaveMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected