Skip to content

Commit 879ec22

Browse files
committed
Fix theme image uploads
1 parent 733647d commit 879ec22

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,8 @@ Open admin pages
247247
--api-key value Shopify API key to for shop [$SHOPIFY_API_KEY]
248248
--help, -h show help (default: false)
249249

250+
Currently `source` can only be a local file
251+
250252
#### Webhooks
251253

252254
Webhooks utilities

cmd/themes/themes.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package themes
22

33
import (
4+
"encoding/base64"
45
"fmt"
6+
"net/http"
57
"os"
68
"strings"
79

@@ -18,7 +20,7 @@ func isDir(path string) bool {
1820
return err == nil && stat.IsDir()
1921
}
2022

21-
func uploadFile(client *shopify.Client, themeID int64, source, destination string) error {
23+
func destinationPath(source, destination string) string {
2224
const themePathSeperator = "/"
2325

2426
if strings.Index(destination, ".") == -1 {
@@ -30,17 +32,26 @@ func uploadFile(client *shopify.Client, themeID int64, source, destination strin
3032
destination = destination + path[len(path) - 1]
3133
}
3234

35+
return destination
36+
}
37+
38+
func uploadFile(client *shopify.Client, themeID int64, source, destination string) error {
39+
destination = destinationPath(source, destination)
40+
3341
fmt.Printf("Uploading '%s' to '%s'\n", source, destination)
3442

3543
value, err := os.ReadFile(source)
3644
if err != nil {
3745
return fmt.Errorf("Failed to read file '%s': %s", source, err)
3846
}
3947

40-
asset := shopify.Asset{
41-
Key: destination,
42-
Value: string(value),
43-
ThemeID: themeID,
48+
asset := shopify.Asset{Key: destination, ThemeID: themeID}
49+
50+
// Others? Maybe always b64 encode?
51+
if strings.HasPrefix(http.DetectContentType(value), "image/") {
52+
asset.Attachment = base64.StdEncoding.EncodeToString(value)
53+
} else {
54+
asset.Value = string(value)
4455
}
4556

4657
_, err = client.Asset.Update(themeID, asset)

0 commit comments

Comments
 (0)