Skip to content

Commit

Permalink
QA
Browse files Browse the repository at this point in the history
  • Loading branch information
begelundmuller committed Dec 23, 2024
1 parent 67f7eb2 commit 8e04105
Show file tree
Hide file tree
Showing 9 changed files with 58 additions and 15 deletions.
4 changes: 4 additions & 0 deletions admin/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (s *Service) InitOrganizationBilling(ctx context.Context, org *database.Org
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: org.QuotaProjects,
QuotaDeployments: org.QuotaDeployments,
Expand Down Expand Up @@ -124,6 +125,7 @@ func (s *Service) RepairOrganizationBilling(ctx context.Context, org *database.O
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: org.QuotaProjects,
QuotaDeployments: org.QuotaDeployments,
Expand Down Expand Up @@ -181,6 +183,7 @@ func (s *Service) RepairOrganizationBilling(ctx context.Context, org *database.O
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: biggerOfInt(sub.Plan.Quotas.NumProjects, org.QuotaProjects),
QuotaDeployments: biggerOfInt(sub.Plan.Quotas.NumDeployments, org.QuotaDeployments),
Expand Down Expand Up @@ -247,6 +250,7 @@ func (s *Service) StartTrial(ctx context.Context, org *database.Organization) (*
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: biggerOfInt(plan.Quotas.NumProjects, org.QuotaProjects),
QuotaDeployments: biggerOfInt(plan.Quotas.NumDeployments, org.QuotaDeployments),
Expand Down
1 change: 1 addition & 0 deletions admin/jobs/river/subscription_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (w *SubscriptionCancellationCheckWorker) subscriptionCancellationCheck(ctx
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: 0,
QuotaDeployments: 0,
Expand Down
1 change: 1 addition & 0 deletions admin/jobs/river/trial_checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ func (w *TrialGracePeriodCheckWorker) trialGracePeriodCheck(ctx context.Context)
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: 0,
QuotaDeployments: 0,
Expand Down
48 changes: 38 additions & 10 deletions admin/server/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,13 @@ func (s *Server) assetHandler(w http.ResponseWriter, r *http.Request) error {
// Check permissions
claims := auth.GetClaims(r.Context())
if !claims.OrganizationPermissions(r.Context(), *asset.OrganizationID).ReadOrg {
return httputil.Errorf(http.StatusForbidden, "does not have permission to access the asset")
ok, err := s.admin.DB.CheckOrganizationHasPublicProjects(r.Context(), *asset.OrganizationID)
if err != nil {
return err
}
if !ok {
return httputil.Errorf(http.StatusForbidden, "does not have permission to access the asset")
}
}

// Parse the asset's path, which has the form "gs://<bucket>/<path>"
Expand All @@ -127,8 +133,38 @@ func (s *Server) assetHandler(w http.ResponseWriter, r *http.Request) error {
return err
}

// Set caching headers if the asset is cacheable
if asset.Cacheable {
w.Header().Set("Cache-Control", "public, max-age=31536000")
} else {
w.Header().Set("Cache-Control", "no-store")
}

// Set the content type header
ext := path.Ext(u.Path)
switch ext {
case ".tar.gz":
w.Header().Set("Content-Type", "application/gzip")
case ".zip":
w.Header().Set("Content-Type", "application/zip")
case ".png":
w.Header().Set("Content-Type", "image/png")
case ".jpg", ".jpeg":
w.Header().Set("Content-Type", "image/jpeg")
case ".svg":
w.Header().Set("Content-Type", "image/svg+xml")
default:
w.Header().Set("Content-Type", "application/octet-stream")
}

// Set the content disposition header
w.Header().Set("Content-Disposition", fmt.Sprintf("inline; filename=%s", path.Base(u.Path)))

// Set the status code
w.WriteHeader(http.StatusOK)

// Download the asset and stream it to the client
data, err := s.admin.Assets.Object(u.Path).NewReader(r.Context())
data, err := s.admin.Assets.Object(strings.TrimPrefix(u.Path, "/")).NewReader(r.Context())
if err != nil {
if errors.Is(err, r.Context().Err()) {
return httputil.Error(http.StatusRequestTimeout, err)
Expand All @@ -137,14 +173,6 @@ func (s *Server) assetHandler(w http.ResponseWriter, r *http.Request) error {
}
defer data.Close()

// Set caching headers if the asset is cacheable
if asset.Cacheable {
w.Header().Set("Cache-Control", "public, max-age=31536000")
} else {
w.Header().Set("Cache-Control", "no-store")
}
w.WriteHeader(http.StatusOK)

// Copy the data reader to the response writer
_, err = io.Copy(w, data)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions admin/server/billing.go
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ func (s *Server) RenewBillingSubscription(ctx context.Context, req *adminv1.Rene
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: valOrDefault(sub.Plan.Quotas.NumProjects, org.QuotaProjects),
QuotaDeployments: valOrDefault(sub.Plan.Quotas.NumDeployments, org.QuotaDeployments),
Expand Down Expand Up @@ -485,6 +486,7 @@ func (s *Server) SudoUpdateOrganizationBillingCustomer(ctx context.Context, req
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: org.QuotaProjects,
QuotaDeployments: org.QuotaDeployments,
Expand Down Expand Up @@ -857,6 +859,7 @@ func (s *Server) updateQuotasAndHandleBillingIssues(ctx context.Context, org *da
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: valOrDefault(sub.Plan.Quotas.NumProjects, org.QuotaProjects),
QuotaDeployments: valOrDefault(sub.Plan.Quotas.NumDeployments, org.QuotaDeployments),
Expand Down
2 changes: 2 additions & 0 deletions admin/server/organizations.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,7 @@ func (s *Server) SudoUpdateOrganizationQuotas(ctx context.Context, req *adminv1.
Name: req.Organization,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: org.CustomDomain,
QuotaProjects: int(valOrDefault(req.Projects, int32(org.QuotaProjects))),
QuotaDeployments: int(valOrDefault(req.Deployments, int32(org.QuotaDeployments))),
Expand Down Expand Up @@ -917,6 +918,7 @@ func (s *Server) SudoUpdateOrganizationCustomDomain(ctx context.Context, req *ad
Name: org.Name,
DisplayName: org.DisplayName,
Description: org.Description,
LogoAssetID: org.LogoAssetID,
CustomDomain: req.CustomDomain,
QuotaProjects: org.QuotaProjects,
QuotaDeployments: org.QuotaDeployments,
Expand Down
4 changes: 2 additions & 2 deletions admin/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,8 @@ func (s *Server) HTTPHandler(ctx context.Context) (http.Handler, error) {
// Add Github-related endpoints (not gRPC handlers, just regular endpoints on /github/*)
s.registerGithubEndpoints(mux)

// Add project assets endpoint
mux.Handle("/v1/assets/{asset_id}", observability.Middleware("assets", s.logger, s.authenticator.HTTPMiddleware(httputil.Handler(s.assetHandler))))
// Add project assets endpoint.
mux.Handle("/v1/assets/{asset_id}/download", observability.Middleware("assets", s.logger, s.authenticator.HTTPMiddleware(httputil.Handler(s.assetHandler))))

// Add biller webhook handler if any
if s.admin.Biller != nil {
Expand Down
1 change: 1 addition & 0 deletions admin/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func (s *Service) CreateOrganizationForUser(ctx context.Context, userID, email,
Name: orgName,
DisplayName: orgName,
Description: description,
LogoAssetID: nil,
CustomDomain: "",
QuotaProjects: quotaProjects,
QuotaDeployments: quotaDeployments,
Expand Down
9 changes: 6 additions & 3 deletions cli/cmd/org/upload_logo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/rilldata/rill/cli/pkg/cmdutil"
adminv1 "github.com/rilldata/rill/proto/gen/rill/admin/v1"
Expand All @@ -21,7 +22,7 @@ func UploadLogoCmd(ch *cmdutil.Helper) *cobra.Command {
cmd := &cobra.Command{
Use: "upload-logo [<org-name> [<path-to-image>]]",
Args: cobra.MaximumNArgs(2),
Short: "Edit organization details",
Short: "Upload a custom logo",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := ch.Client()
if err != nil {
Expand Down Expand Up @@ -64,8 +65,10 @@ func UploadLogoCmd(ch *cmdutil.Helper) *cobra.Command {
}

// Check the file is an image
ext := filepath.Ext(path)
if ext != ".png" && ext != ".jpg" && ext != ".jpeg" {
ext := strings.TrimPrefix(filepath.Ext(path), ".")
switch ext {
case "png", "jpg", "jpeg":
default:
return fmt.Errorf("invalid file type %q (expected PNG or JPG)", ext)
}

Expand Down

0 comments on commit 8e04105

Please sign in to comment.