Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storage): issue with file uploading #54

Merged
merged 1 commit into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions crowdin/crowdin.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,35 @@ func (c *Client) newRequest(ctx context.Context, method, path string, body any,
return req, nil
}

// newUploadRequest creates an upload request.
func (c *Client) newUploadRequest(ctx context.Context, method, path string, body io.Reader, opts ...RequestOption) (*http.Request, error) {
rel, err := url.Parse(path)
if err != nil {
return nil, err
}
u := c.baseURL.ResolveReference(rel)

req, err := http.NewRequestWithContext(ctx, method, u.String(), body)
if err != nil {
return nil, err
}

req.Header.Set("User-Agent", c.userAgent)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.token))

for _, opt := range opts {
if err := opt(req); err != nil {
return nil, err
}
}

if req.Header.Get("Content-Type") == "" {
req.Header.Set("Content-Type", "application/octet-stream")
}

return req, nil
}

// do sends an API request and returns the API response.
func (c *Client) do(r *http.Request, v any) (*Response, error) {
resp, err := c.httpClient.Do(r)
Expand Down Expand Up @@ -263,6 +292,16 @@ func (c *Client) Post(ctx context.Context, path string, body, v any, opts ...Req
return c.do(req, v)
}

// Upload makes a POST request to the specified path with a file.
func (c *Client) Upload(ctx context.Context, path string, file io.Reader, v any, opts ...RequestOption) (*Response, error) {
req, err := c.newUploadRequest(ctx, "POST", path, file, opts...)
if err != nil {
return nil, err
}

return c.do(req, v)
}

// Patch makes a PATCH request to the specified path.
func (c *Client) Patch(ctx context.Context, path string, body, v any) (*Response, error) {
// Body can be a single object or a slice of objects.
Expand Down
10 changes: 5 additions & 5 deletions crowdin/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package crowdin

import (
"context"
"errors"
"fmt"
"mime"
"net/url"
Expand Down Expand Up @@ -31,14 +32,13 @@ type StorageService struct {
//
// https://developer.crowdin.com/api/v2/#operation/api.storages.post
func (s *StorageService) Add(ctx context.Context, file *os.File) (*model.Storage, *Response, error) {
mediaType := mime.TypeByExtension(filepath.Ext(file.Name()))
if mediaType == "" {
mediaType = "application/octet-stream"
if file == nil {
return nil, nil, errors.New("file is required")
}

res := new(model.StorageGetResponse)
resp, err := s.client.Post(ctx, "/api/v2/storages", file, res,
Header("Content-Type", mediaType),
resp, err := s.client.Upload(ctx, "/api/v2/storages", file, res,
Header("Content-Type", mime.TypeByExtension(filepath.Ext(file.Name()))),
Header("Crowdin-API-FileName", url.QueryEscape(filepath.Base(file.Name()))),
)

Expand Down
6 changes: 6 additions & 0 deletions crowdin/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ func TestStorageService_Add(t *testing.T) {
t.Fatalf("Storages.Add failed to open temp file: %v", err)
}
defer os.RemoveAll(dir)
defer file.Close()

storage, _, err := client.Storages.Add(context.Background(), file)
if err != nil {
Expand All @@ -164,6 +165,11 @@ func TestStorageService_Add(t *testing.T) {
if !reflect.DeepEqual(storage, want) {
t.Errorf("Storages.Add returned %+v, want %+v", storage, want)
}

_, _, err = client.Storages.Add(context.Background(), nil)
if err == nil {
t.Errorf("Storages.Add should return an error, got nil")
}
}
}

Expand Down