Skip to content

Commit

Permalink
Fix theme image uploads
Browse files Browse the repository at this point in the history
  • Loading branch information
sshaw committed May 16, 2023
1 parent 733647d commit 879ec22
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 16 additions & 5 deletions cmd/themes/themes.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package themes

import (
"encoding/base64"
"fmt"
"net/http"
"os"
"strings"

Expand All @@ -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 {
Expand All @@ -30,17 +32,26 @@ 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)
if err != nil {
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)
Expand Down

0 comments on commit 879ec22

Please sign in to comment.