98 lines
2.6 KiB
Go
98 lines
2.6 KiB
Go
// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
|
|
//
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
// copyright notice and this permission notice appear in all copies.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
package trust
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"time"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
"github.com/getprobo/probo/pkg/coredata"
|
|
"github.com/getprobo/probo/pkg/gid"
|
|
"go.gearno.de/kit/pg"
|
|
)
|
|
|
|
type OrganizationService struct {
|
|
svc *TenantService
|
|
}
|
|
|
|
func (s OrganizationService) Get(
|
|
ctx context.Context,
|
|
organizationID gid.GID,
|
|
) (*coredata.Organization, error) {
|
|
organization := &coredata.Organization{}
|
|
|
|
err := s.svc.pg.WithConn(
|
|
ctx,
|
|
func(conn pg.Conn) error {
|
|
err := organization.LoadByID(
|
|
ctx,
|
|
conn,
|
|
s.svc.scope,
|
|
organizationID,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("cannot load organization: %w", err)
|
|
}
|
|
|
|
return nil
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return organization, nil
|
|
}
|
|
|
|
func (s OrganizationService) GenerateLogoURL(
|
|
ctx context.Context,
|
|
organizationID gid.GID,
|
|
expiresIn time.Duration,
|
|
) (*string, error) {
|
|
organization, err := s.Get(ctx, organizationID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot get organization: %w", err)
|
|
}
|
|
|
|
if organization.LogoObjectKey == "" {
|
|
return nil, nil
|
|
}
|
|
|
|
presignClient := s3.NewPresignClient(s.svc.s3)
|
|
|
|
encodedFilename := url.QueryEscape(organization.Name)
|
|
contentDisposition := fmt.Sprintf("attachment; filename=\"%s\"; filename*=UTF-8''%s",
|
|
encodedFilename, encodedFilename)
|
|
|
|
presignedReq, err := presignClient.PresignGetObject(ctx, &s3.GetObjectInput{
|
|
Bucket: aws.String(s.svc.bucket),
|
|
Key: aws.String(organization.LogoObjectKey),
|
|
ResponseCacheControl: aws.String("max-age=3600, public"),
|
|
ResponseContentDisposition: aws.String(contentDisposition),
|
|
}, func(opts *s3.PresignOptions) {
|
|
opts.Expires = expiresIn
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("cannot presign GetObject request: %w", err)
|
|
}
|
|
|
|
return &presignedReq.URL, nil
|
|
}
|