From fbd2208e97a66fef961b4d91c046b6ab53ccacd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20Sonck?= Date: Sat, 17 Aug 2024 18:06:02 +0200 Subject: [PATCH] fix(webdav): handle api error and use url.Values encoding (#283) - fix(webdav): handle api errors in patch/create from s3 - fix(webdav): utilize url.Values for query encoding --- webdav/client/api_client/file_client.go | 64 +++++++++++++++---------- 1 file changed, 39 insertions(+), 25 deletions(-) diff --git a/webdav/client/api_client/file_client.go b/webdav/client/api_client/file_client.go index 5a3eacc30..67103ecde 100644 --- a/webdav/client/api_client/file_client.go +++ b/webdav/client/api_client/file_client.go @@ -19,6 +19,7 @@ import ( "net/http" "net/url" "os" + "strconv" "strings" "github.com/kouprlabs/voltaserve/webdav/config" @@ -145,20 +146,21 @@ func (cl *FileClient) CreateFromS3(opts FileCreateFromS3Options) (*File, error) if err != nil { return nil, err } + args := url.Values{ + "api_key": []string{cl.config.Security.APIKey}, + "access_token": []string{cl.token.AccessToken}, + "workspace_id": []string{opts.WorkspaceID}, + "parent_id": []string{opts.ParentID}, + "name": []string{opts.Name}, + "s3_key": []string{opts.S3Reference.Key}, + "s3_bucket": []string{opts.S3Reference.Bucket}, + "snapshot_id": []string{opts.S3Reference.SnapshotID}, + "content_type": []string{opts.S3Reference.ContentType}, + "size": []string{strconv.FormatInt(opts.S3Reference.Size, 10)}, + } + reqUrl := cl.config.APIURL + "/v2/files/create_from_s3?" + args.Encode() req, err := http.NewRequest("POST", - fmt.Sprintf("%s/v2/files/create_from_s3?api_key=%s&access_token=%s&workspace_id=%s&parent_id=%s&name=%s&s3_key=%s&s3_bucket=%s&snapshot_id=%s&content_type=%s&size=%d", - cl.config.APIURL, - cl.config.Security.APIKey, - cl.token.AccessToken, - opts.WorkspaceID, - opts.ParentID, - opts.Name, - opts.S3Reference.Key, - opts.S3Reference.Bucket, - opts.S3Reference.SnapshotID, - opts.S3Reference.ContentType, - opts.S3Reference.Size, - ), + reqUrl, bytes.NewBuffer(body)) if err != nil { return nil, err @@ -176,6 +178,12 @@ func (cl *FileClient) CreateFromS3(opts FileCreateFromS3Options) (*File, error) return } }(res.Body) + + body, err = cl.jsonResponseOrThrow(res) + if err != nil { + return nil, err + } + var file File if err = json.Unmarshal(body, &file); err != nil { return nil, err @@ -194,19 +202,19 @@ func (cl *FileClient) PatchFromS3(opts FilePatchFromS3Options) (*File, error) { if err != nil { return nil, err } + args := url.Values{ + "api_key": []string{cl.config.Security.APIKey}, + "access_token": []string{cl.token.AccessToken}, + "name": []string{opts.Name}, + "s3_key": []string{opts.S3Reference.Key}, + "s3_bucket": []string{opts.S3Reference.Bucket}, + "snapshot_id": []string{opts.S3Reference.SnapshotID}, + "content_type": []string{opts.S3Reference.ContentType}, + "size": []string{strconv.FormatInt(opts.S3Reference.Size, 10)}, + } + reqUrl := cl.config.APIURL + "/v2/files/" + opts.ID + "/patch_from_s3?" + args.Encode() req, err := http.NewRequest("PATCH", - fmt.Sprintf("%s/v2/files/%s/patch_from_s3?api_key=%s&access_token=%s&name=%s&s3_key=%s&s3_bucket=%s&snapshot_id=%s&content_type=%s&size=%d", - cl.config.APIURL, - opts.ID, - cl.config.Security.APIKey, - cl.token.AccessToken, - opts.Name, - opts.S3Reference.Key, - opts.S3Reference.Bucket, - opts.S3Reference.SnapshotID, - opts.S3Reference.ContentType, - opts.S3Reference.Size, - ), + reqUrl, bytes.NewBuffer(body)) if err != nil { return nil, err @@ -223,6 +231,12 @@ func (cl *FileClient) PatchFromS3(opts FilePatchFromS3Options) (*File, error) { return } }(res.Body) + + body, err = cl.jsonResponseOrThrow(res) + if err != nil { + return nil, err + } + var file File if err = json.Unmarshal(body, &file); err != nil { return nil, err