plane_attachment.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package plane_v1
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "go.uber.org/zap"
  6. )
  7. // ==================== Work Item Attachment API ====================
  8. // AttachmentCreateResponse 附件创建响应
  9. type AttachmentCreateResponse struct {
  10. UploadData struct {
  11. URL string `json:"url"`
  12. Fields map[string]string `json:"fields"`
  13. } `json:"upload_data"`
  14. AssetId string `json:"asset_id"`
  15. AssetUrl string `json:"asset_url"`
  16. }
  17. // ListWorkItemAttachments 列出工作项附件
  18. func ListWorkItemAttachments(workItemID string) ([]byte, error) {
  19. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/work-items/%s/attachments/", planeWorkspaceSlug, planeProjectId, workItemID)
  20. resp, err := planeHttpGet(path)
  21. if err != nil {
  22. zap.L().Error("[Plane] ListWorkItemAttachments 请求失败", zap.Error(err))
  23. return nil, err
  24. }
  25. if !isSuccess(resp.StatusCode) {
  26. zap.L().Error("[Plane] ListWorkItemAttachments 返回错误", zap.String("status", resp.Status))
  27. return nil, fmt.Errorf("ListWorkItemAttachments 失败: %s", resp.Status)
  28. }
  29. return resp.Body, nil
  30. }
  31. // CreateWorkItemAttachment 创建附件(获取预签名 URL)
  32. func CreateWorkItemAttachment(workItemID, fileName, mimeType string, size int64) (*AttachmentCreateResponse, error) {
  33. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/work-items/%s/attachments/", planeWorkspaceSlug, planeProjectId, workItemID)
  34. payload := map[string]interface{}{
  35. "name": fileName,
  36. "type": mimeType,
  37. "size": size,
  38. }
  39. resp, err := planeHttpPost(path, payload)
  40. if err != nil {
  41. zap.L().Error("[Plane] CreateWorkItemAttachment 请求失败", zap.Error(err))
  42. return nil, err
  43. }
  44. if !isSuccess(resp.StatusCode) {
  45. zap.L().Error("[Plane] CreateWorkItemAttachment 返回错误", zap.String("status", resp.Status), zap.String("body", string(resp.Body)))
  46. return nil, fmt.Errorf("CreateWorkItemAttachment 失败: %s", resp.Status)
  47. }
  48. var result AttachmentCreateResponse
  49. if err := json.Unmarshal(resp.Body, &result); err != nil {
  50. return nil, fmt.Errorf("解析附件创建响应失败: %w", err)
  51. }
  52. return &result, nil
  53. }
  54. // ConfirmWorkItemAttachment 确认附件上传完成
  55. func ConfirmWorkItemAttachment(workItemID, assetID string) error {
  56. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/work-items/%s/attachments/%s/", planeWorkspaceSlug, planeProjectId, workItemID, assetID)
  57. payload := map[string]interface{}{"is_uploaded": true}
  58. resp, err := planeHttpPatch(path, payload)
  59. if err != nil {
  60. zap.L().Error("[Plane] ConfirmWorkItemAttachment 请求失败", zap.Error(err))
  61. return err
  62. }
  63. if resp.StatusCode != 204 && !isSuccess(resp.StatusCode) {
  64. zap.L().Error("[Plane] ConfirmWorkItemAttachment 返回错误", zap.String("status", resp.Status))
  65. return fmt.Errorf("ConfirmWorkItemAttachment 失败: %s", resp.Status)
  66. }
  67. return nil
  68. }
  69. // DeleteWorkItemAttachment 删除附件
  70. func DeleteWorkItemAttachment(workItemID, assetID string) error {
  71. path := fmt.Sprintf("/api/v1/workspaces/%s/projects/%s/work-items/%s/attachments/%s/", planeWorkspaceSlug, planeProjectId, workItemID, assetID)
  72. resp, err := planeHttpDelete(path)
  73. if err != nil {
  74. zap.L().Error("[Plane] DeleteWorkItemAttachment 请求失败", zap.Error(err))
  75. return err
  76. }
  77. if resp.StatusCode != 204 {
  78. zap.L().Error("[Plane] DeleteWorkItemAttachment 返回错误", zap.String("status", resp.Status))
  79. return fmt.Errorf("DeleteWorkItemAttachment 失败: %s", resp.Status)
  80. }
  81. return nil
  82. }
  83. // UploadWorkItemAttachment 完整附件上传流程(获取URL → 上传S3 → 确认)
  84. func UploadWorkItemAttachment(workItemID, fileName, mimeType string, fileData []byte) (*AssetUploadResult, error) {
  85. fileSize := int64(len(fileData))
  86. // Step 1: 获取预签名 URL
  87. createResp, err := CreateWorkItemAttachment(workItemID, fileName, mimeType, fileSize)
  88. if err != nil {
  89. return nil, fmt.Errorf("获取上传凭据失败: %w", err)
  90. }
  91. // Step 2: 上传到 S3
  92. if err := uploadToPresignedPost(&CreateAssetResponse{
  93. UploadData: createResp.UploadData,
  94. AssetId: createResp.AssetId,
  95. AssetUrl: createResp.AssetUrl,
  96. }, fileData, mimeType); err != nil {
  97. return nil, fmt.Errorf("文件上传失败: %w", err)
  98. }
  99. // Step 3: 确认上传完成
  100. if err := ConfirmWorkItemAttachment(workItemID, createResp.AssetId); err != nil {
  101. return nil, fmt.Errorf("确认上传失败: %w", err)
  102. }
  103. zap.L().Info("[Plane] WorkItem Attachment 上传完成",
  104. zap.String("assetId", createResp.AssetId),
  105. zap.String("fileName", fileName),
  106. )
  107. return &AssetUploadResult{
  108. AssetId: createResp.AssetId,
  109. AssetUrl: createResp.AssetUrl,
  110. }, nil
  111. }