@@ -2,6 +2,8 @@ package proton
2
2
3
3
import (
4
4
"context"
5
+ "encoding/json"
6
+ "net/http"
5
7
6
8
"github.com/go-resty/resty/v2"
7
9
)
@@ -22,12 +24,39 @@ func (c *Client) GetLink(ctx context.Context, shareID, linkID string) (Link, err
22
24
23
25
func (c * Client ) CreateFile (ctx context.Context , shareID string , req CreateFileReq ) (CreateFileRes , error ) {
24
26
var res struct {
27
+ Code int
25
28
File CreateFileRes
26
29
}
27
30
28
- if err := c .do (ctx , func (r * resty.Request ) (* resty.Response , error ) {
31
+ resp , err := c .doRes (ctx , func (r * resty.Request ) (* resty.Response , error ) {
29
32
return r .SetResult (& res ).SetBody (req ).Post ("/drive/shares/" + shareID + "/files" )
30
- }); err != nil {
33
+ })
34
+ if err != nil { // if the status code is not 200~299, it's considered an error
35
+
36
+ // handle the file or folder name exists error
37
+ if resp .StatusCode () == http .StatusUnprocessableEntity /* 422 */ {
38
+ var apiError APIError
39
+ err := json .Unmarshal (resp .Body (), & apiError )
40
+ if err != nil {
41
+ return CreateFileRes {}, err
42
+ }
43
+ if apiError .Code == AFileOrFolderNameExist {
44
+ return CreateFileRes {}, ErrFileNameExist // since we are in CreateFile, so we return this error
45
+ }
46
+ }
47
+
48
+ // handle draft exists error
49
+ if resp .StatusCode () == http .StatusConflict /* 409 */ {
50
+ var apiError APIError
51
+ err := json .Unmarshal (resp .Body (), & apiError )
52
+ if err != nil {
53
+ return CreateFileRes {}, err
54
+ }
55
+ if apiError .Code == ADraftExist {
56
+ return CreateFileRes {}, ErrADraftExist
57
+ }
58
+ }
59
+
31
60
return CreateFileRes {}, err
32
61
}
33
62
@@ -39,9 +68,24 @@ func (c *Client) CreateFolder(ctx context.Context, shareID string, req CreateFol
39
68
Folder CreateFolderRes
40
69
}
41
70
42
- if err := c .do (ctx , func (r * resty.Request ) (* resty.Response , error ) {
71
+ resp , err := c .doRes (ctx , func (r * resty.Request ) (* resty.Response , error ) {
43
72
return r .SetResult (& res ).SetBody (req ).Post ("/drive/shares/" + shareID + "/folders" )
44
- }); err != nil {
73
+ })
74
+ if err != nil { // if the status code is not 200~299, it's considered an error
75
+
76
+ // handle the file or folder name exists error
77
+ if resp .StatusCode () == http .StatusUnprocessableEntity /* 422 */ {
78
+ // log.Println(resp.String())
79
+ var apiError APIError
80
+ err := json .Unmarshal (resp .Body (), & apiError )
81
+ if err != nil {
82
+ return CreateFolderRes {}, err
83
+ }
84
+ if apiError .Code == AFileOrFolderNameExist {
85
+ return CreateFolderRes {}, ErrFolderNameExist // since we are in CreateFolder, so we return this error
86
+ }
87
+ }
88
+
45
89
return CreateFolderRes {}, err
46
90
}
47
91
0 commit comments