-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upload favicon backend support (#6606)
- Loading branch information
1 parent
edef8a1
commit fd0878b
Showing
18 changed files
with
3,928 additions
and
3,700 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
ALTER TABLE orgs ADD COLUMN favicon_asset_id UUID REFERENCES assets(id) ON DELETE SET NULL; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
package org | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/rilldata/rill/cli/pkg/cmdutil" | ||
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func UploadFaviconCmd(ch *cmdutil.Helper) *cobra.Command { | ||
var path string | ||
var remove bool | ||
|
||
cmd := &cobra.Command{ | ||
Use: "upload-favicon [<org-name> [<path-to-image>]]", | ||
Args: cobra.MaximumNArgs(2), | ||
Short: "Upload a custom favicon", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
client, err := ch.Client() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Parse positional args into flags | ||
if len(args) > 0 { | ||
ch.Org = args[0] | ||
if len(args) > 1 { | ||
path = args[1] | ||
} | ||
} | ||
if ch.Org == "" { | ||
return fmt.Errorf("an organization name is required") | ||
} | ||
|
||
// Handle --remove | ||
if remove { | ||
if path != "" { | ||
return fmt.Errorf("cannot specify both --remove and a path") | ||
} | ||
|
||
// Confirmation prompt | ||
if ok, err := cmdutil.ConfirmPrompt(fmt.Sprintf("You are removing the custom favicon for %q. Continue?", ch.Org), "", false); err != nil || !ok { | ||
return err | ||
} | ||
|
||
empty := "" | ||
_, err = client.UpdateOrganization(cmd.Context(), &adminv1.UpdateOrganizationRequest{ | ||
Name: ch.Org, | ||
FaviconAssetId: &empty, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ch.PrintfSuccess("Removed favicon from organization %q\n", ch.Org) | ||
return nil | ||
} | ||
|
||
// Check the file is an image | ||
ext := strings.TrimPrefix(filepath.Ext(path), ".") | ||
switch ext { | ||
case "png", "ico": | ||
default: | ||
return fmt.Errorf("invalid file type %q (expected PNG or JPG)", ext) | ||
} | ||
|
||
// Validate and open the path | ||
fi, err := os.Stat(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to read %q: %w", path, err) | ||
} | ||
if fi.IsDir() { | ||
return fmt.Errorf("failed to upload %q: the path is a directory", path) | ||
} | ||
if fi.Size() == 0 { | ||
return fmt.Errorf("failed to upload %q: the file is empty", path) | ||
} | ||
f, err := os.Open(path) | ||
if err != nil { | ||
return fmt.Errorf("failed to open %q: %w", path, err) | ||
} | ||
defer f.Close() | ||
|
||
// Confirmation prompt | ||
if ok, err := cmdutil.ConfirmPrompt(fmt.Sprintf("You are changing the custom favicon for %q. Continue?", ch.Org), "", false); err != nil || !ok { | ||
return err | ||
} | ||
|
||
// Generate the asset upload URL | ||
asset, err := client.CreateAsset(cmd.Context(), &adminv1.CreateAssetRequest{ | ||
OrganizationName: ch.Org, | ||
Type: "image", | ||
Name: "favicon", | ||
Extension: ext, | ||
Cacheable: true, | ||
EstimatedSizeBytes: fi.Size(), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Execute the upload | ||
req, err := http.NewRequestWithContext(cmd.Context(), http.MethodPut, asset.SignedUrl, f) | ||
if err != nil { | ||
return fmt.Errorf("failed to upload: %w", err) | ||
} | ||
for k, v := range asset.SigningHeaders { | ||
req.Header.Set(k, v) | ||
} | ||
resp, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to upload: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
if resp.StatusCode != http.StatusOK { | ||
body, _ := io.ReadAll(resp.Body) | ||
return fmt.Errorf("failed to upload: status=%d, error=%s", resp.StatusCode, string(body)) | ||
} | ||
|
||
// Update the favicon | ||
_, err = client.UpdateOrganization(cmd.Context(), &adminv1.UpdateOrganizationRequest{ | ||
Name: ch.Org, | ||
FaviconAssetId: &asset.AssetId, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to update: %w", err) | ||
} | ||
|
||
// Print confirmation message | ||
ch.PrintfSuccess("Updated the favicon for %q\n", ch.Org) | ||
return nil | ||
}, | ||
} | ||
cmd.Flags().SortFlags = false | ||
cmd.Flags().StringVar(&ch.Org, "org", ch.Org, "Organization name") | ||
cmd.Flags().StringVar(&path, "path", "", "Path to image file (PNG or JPEG)") | ||
cmd.Flags().BoolVar(&remove, "remove", false, "Remove the current favicon") | ||
|
||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
fd0878b
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉 Published on https://ui.rilldata.in as production
🚀 Deployed on https://67aa0a48e493bd2721b301f0--rill-ui-dev.netlify.app