completeWPSFileUpload 完成WPS文件上传回调
(c *gin.Context)
| 943 | |
| 944 | // completeWPSFileUpload 完成WPS文件上传回调 |
| 945 | func completeWPSFileUpload(c *gin.Context) (any, error) { |
| 946 | // 获取文件信息 |
| 947 | file, wpsErr := getWPSFileCommon(c, true) |
| 948 | if wpsErr != nil { |
| 949 | return nil, wpsErr |
| 950 | } |
| 951 | |
| 952 | // 验证文件是否存在 |
| 953 | if file == nil { |
| 954 | return nil, weboffice.ErrFileNotExists |
| 955 | } |
| 956 | |
| 957 | // 解析请求体 |
| 958 | var req UploadCompleteRequest |
| 959 | if err := c.ShouldBindJSON(&req); err != nil { |
| 960 | return nil, weboffice.ErrInvalidArguments.WithMessage("请求参数错误") |
| 961 | } |
| 962 | |
| 963 | // 验证 send_back_params 中的 upload_token |
| 964 | if req.SendBackParams == nil { |
| 965 | return nil, weboffice.ErrInvalidArguments.WithMessage("缺少 send_back_params") |
| 966 | } |
| 967 | |
| 968 | uploadToken, exists := req.SendBackParams["upload_token"] |
| 969 | if !exists || uploadToken == "" { |
| 970 | return nil, weboffice.ErrInvalidArguments.WithMessage("缺少 upload_token") |
| 971 | } |
| 972 | |
| 973 | // 从 Redis 验证上传许可 |
| 974 | redisKey := fmt.Sprintf("wps_upload_token_%s", uploadToken) |
| 975 | uploadTokenRaw, err := common.RedisGet(redisKey) |
| 976 | if err != nil { |
| 977 | return nil, weboffice.ErrPermissionDenied.WithMessage("上传许可无效或已过期") |
| 978 | } |
| 979 | |
| 980 | if uploadTokenRaw == "" { |
| 981 | return nil, weboffice.ErrPermissionDenied.WithMessage("上传许可不存在") |
| 982 | } |
| 983 | |
| 984 | // 解析原始数据验证匹配 |
| 985 | ctx, err := weboffice.ParseContext(c.Request, true) |
| 986 | if err != nil { |
| 987 | return nil, weboffice.ErrInvalidArguments.WithMessage(err.Error()) |
| 988 | } |
| 989 | expectedPrefix := fmt.Sprintf("wps_upload_%d_%d_", file.ID, ctx.UserModel().UserID) |
| 990 | if !strings.HasPrefix(uploadTokenRaw, expectedPrefix) { |
| 991 | return nil, weboffice.ErrPermissionDenied.WithMessage("上传许可不匹配") |
| 992 | } |
| 993 | |
| 994 | // 解析请求信息 |
| 995 | rawNameValue, _ := req.Request["name"] |
| 996 | name, ok := rawNameValue.(string) |
| 997 | if !ok { |
| 998 | return nil, weboffice.ErrInvalidArguments.WithMessage("缺少文档名称") |
| 999 | } |
| 1000 | name = strings.TrimSpace(path.Base(name)) |
| 1001 | if name == "" || name == "." || name == "/" { |
| 1002 | return nil, weboffice.ErrInvalidArguments.WithMessage("缺少文档名称") |
nothing calls this directly
no test coverage detected