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

Upload: Only sign required params #25

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
14 changes: 13 additions & 1 deletion api/uploader/upload.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,23 @@ func (u *API) signRequest(requestParams url.Values) (url.Values, error) {
if u.Config.Cloud.APISecret == "" {
return nil, errors.New("must provide API Secret")
}
// https://cloudinary.com/documentation/upload_images#generating_authentication_signatures
// All parameters added to the method call should be included except: file, cloud_name, resource_type and your api_key.
signatureParams := make(url.Values)
for k, v := range requestParams {
switch k {
case "file", "cloud_name", "resource_type", "api_key":
// omit
default:
signatureParams[k] = v
}
}

signature, err := api.SignParameters(requestParams, u.Config.Cloud.APISecret)
signature, err := api.SignParameters(signatureParams, u.Config.Cloud.APISecret)
if err != nil {
return nil, err
}
requestParams.Set("timestamp", signatureParams.Get("timestamp"))
requestParams.Add("signature", signature)
requestParams.Add("api_key", u.Config.Cloud.APIKey)

Expand Down
20 changes: 19 additions & 1 deletion api/uploader/upload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package uploader_test
import (
"context"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"log"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/cloudinary/cloudinary-go/api"
"github.com/cloudinary/cloudinary-go/api/uploader"
"github.com/cloudinary/cloudinary-go/internal/cldtest"
Expand Down Expand Up @@ -87,6 +88,23 @@ func TestUploader_UploadURL(t *testing.T) {
}
}

func TestUploader_UploadVideoURL(t *testing.T) {
params := uploader.UploadParams{
PublicID: cldtest.PublicID,
ResourceType: "video",
Overwrite: true,
}

resp, err := uploadAPI.Upload(ctx, cldtest.VideoURL, params)

if err != nil {
t.Error(err)
}
if resp == nil || resp.PublicID != cldtest.PublicID || resp.Error.Message != "" {
t.Error(resp)
}
}

func TestUploader_UploadBase64Image(t *testing.T) {
params := uploader.UploadParams{
PublicID: cldtest.PublicID,
Expand Down
6 changes: 5 additions & 1 deletion internal/cldtest/cldtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package cldtest
import (
"context"
"fmt"
"github.com/cloudinary/cloudinary-go/api/uploader"
"math/rand"
"os"
"path"
Expand All @@ -12,11 +11,16 @@ import (
"strconv"
"testing"
"time"

"github.com/cloudinary/cloudinary-go/api/uploader"
)

// LogoURL is the URL of the publicly available logo.
const LogoURL = "https://cloudinary-res.cloudinary.com/image/upload/cloudinary_logo.png"

// VideoURL is the URL of the publicly available video.
const VideoURL = "https://res.cloudinary.com/demo/video/upload/dog.mp4"

// Base64Image us a base64 encoded test image.
const Base64Image = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7"

Expand Down