From 879ec22a5490b24c54cbd9ce04ad9501e1310a82 Mon Sep 17 00:00:00 2001 From: sshaw Date: Tue, 16 May 2023 00:43:17 -0400 Subject: [PATCH] Fix theme image uploads --- README.md | 2 ++ cmd/themes/themes.go | 21 ++++++++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 429ee43..cb17b97 100644 --- a/README.md +++ b/README.md @@ -247,6 +247,8 @@ Open admin pages --api-key value Shopify API key to for shop [$SHOPIFY_API_KEY] --help, -h show help (default: false) +Currently `source` can only be a local file + #### Webhooks Webhooks utilities diff --git a/cmd/themes/themes.go b/cmd/themes/themes.go index abc65bd..4607ff3 100644 --- a/cmd/themes/themes.go +++ b/cmd/themes/themes.go @@ -1,7 +1,9 @@ package themes import ( + "encoding/base64" "fmt" + "net/http" "os" "strings" @@ -18,7 +20,7 @@ func isDir(path string) bool { return err == nil && stat.IsDir() } -func uploadFile(client *shopify.Client, themeID int64, source, destination string) error { +func destinationPath(source, destination string) string { const themePathSeperator = "/" if strings.Index(destination, ".") == -1 { @@ -30,6 +32,12 @@ func uploadFile(client *shopify.Client, themeID int64, source, destination strin destination = destination + path[len(path) - 1] } + return destination +} + +func uploadFile(client *shopify.Client, themeID int64, source, destination string) error { + destination = destinationPath(source, destination) + fmt.Printf("Uploading '%s' to '%s'\n", source, destination) value, err := os.ReadFile(source) @@ -37,10 +45,13 @@ func uploadFile(client *shopify.Client, themeID int64, source, destination strin return fmt.Errorf("Failed to read file '%s': %s", source, err) } - asset := shopify.Asset{ - Key: destination, - Value: string(value), - ThemeID: themeID, + asset := shopify.Asset{Key: destination, ThemeID: themeID} + + // Others? Maybe always b64 encode? + if strings.HasPrefix(http.DetectContentType(value), "image/") { + asset.Attachment = base64.StdEncoding.EncodeToString(value) + } else { + asset.Value = string(value) } _, err = client.Asset.Update(themeID, asset)