Skip to content
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
28 changes: 28 additions & 0 deletions api/uploader/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package uploader

import (
"context"
"encoding/json"
"net/url"
"time"

Expand Down Expand Up @@ -59,6 +60,15 @@ type CreateArchiveParams struct {
UseOriginalFilename *bool `json:"use_original_filename,omitempty"`
}

// MarshalJSON sends ExpiresAt as a unix timestamp, as the API expects.
func (p CreateArchiveParams) MarshalJSON() ([]byte, error) {
type params CreateArchiveParams
return json.Marshal(struct {
params
ExpiresAt *int64 `json:"expires_at,omitempty"`
}{params(p), unixTime(p.ExpiresAt)})
}

// CreateArchive creates a new archive in the server and returns information in JSON format.
func (u *API) CreateArchive(ctx context.Context, params CreateArchiveParams) (*CreateArchiveResult, error) {
res := &CreateArchiveResult{}
Expand Down Expand Up @@ -179,6 +189,24 @@ type PrivateDownloadURLParams struct {
ResourceType api.AssetType `json:"-"`
}

// MarshalJSON sends ExpiresAt as a unix timestamp, as the API expects.
func (p PrivateDownloadURLParams) MarshalJSON() ([]byte, error) {
type params PrivateDownloadURLParams
return json.Marshal(struct {
params
ExpiresAt *int64 `json:"expires_at,omitempty"`
}{params(p), unixTime(p.ExpiresAt)})
}

// unixTime returns t as unix seconds, or nil when t is nil.
func unixTime(t *time.Time) *int64 {
if t == nil {
return nil
}
unix := t.Unix()
return &unix
}

// PrivateDownloadURL returns a URL that when invoked downloads the asset.
func (u *API) PrivateDownloadURL(params PrivateDownloadURLParams) (string, error) {
queryParams, err := api.StructToParams(params)
Expand Down
20 changes: 20 additions & 0 deletions api/uploader/archive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/url"
"strings"
"testing"
"time"

"github.com/cloudinary/cloudinary-go/v2/api"
"github.com/cloudinary/cloudinary-go/v2/api/uploader"
Expand Down Expand Up @@ -100,3 +101,22 @@ func TestUploader_PrivateDownloadURL(t *testing.T) {
assert.Contains(t, privateDownloadURL, "signature")
assert.Contains(t, privateDownloadURL, "timestamp")
}

func TestUploader_ExpiresAtIsUnixTimestamp(t *testing.T) {
expiresAt := time.Unix(1800000000, 0).UTC()

archiveURL, err := uploadAPI.DownloadZipURL(uploader.CreateArchiveParams{
PublicIDs: []string{cldtest.PublicID},
ExpiresAt: &expiresAt,
})
assert.NoError(t, err)
assert.Contains(t, archiveURL, "expires_at=1800000000")

privateDownloadURL, err := uploadAPI.PrivateDownloadURL(uploader.PrivateDownloadURLParams{
PublicID: cldtest.PublicID,
Format: "png",
ExpiresAt: &expiresAt,
})
assert.NoError(t, err)
assert.Contains(t, privateDownloadURL, "expires_at=1800000000")
}
Loading