-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Upload favicon backend support #6606
Merged
+3,928
−3,700
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can we add gif, svg and jpg also. Might as well just allow every image type at this point
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.
I believe that's not supported for favicons. But I can add it for normal logo uploads.
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.
#6631