s3-share/internal/client/client.go

111 lines
2.1 KiB
Go
Raw Normal View History

2022-05-09 12:52:18 +00:00
package client
import (
"context"
"errors"
"math/rand"
2022-05-10 10:49:34 +00:00
"path/filepath"
2022-05-09 12:52:18 +00:00
"time"
"git.kapelle.org/niklas/s3share/internal/db"
"git.kapelle.org/niklas/s3share/internal/s3"
"git.kapelle.org/niklas/s3share/internal/types"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
type Client struct {
db db.DB
s3 s3.S3
}
func NewClient(db db.DB, s3 s3.S3) *Client {
rand.Seed(time.Now().UnixNano())
return &Client{
db: db,
s3: s3,
}
}
func createRandomString() string {
s := make([]rune, 6)
for i := range s {
s[i] = letters[rand.Intn(len(letters))]
}
return string(s)
}
func (c *Client) CreateValidSlug(ctx context.Context) (string, error) {
for i := 0; i < 10; i++ {
slug := createRandomString()
res, err := c.db.GetShare(ctx, slug)
if err != nil {
return "", err
}
if res == nil {
return slug, nil
}
}
return "", errors.New("could not create valid slug after 10 tries")
}
func (c *Client) GetShare(ctx context.Context, slug string) (*types.Share, error) {
return c.db.GetShare(ctx, slug)
}
func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, error) {
slug, err := c.CreateValidSlug(ctx)
if err != nil {
return nil, err
}
share := &types.Share{
Slug: slug,
Key: key,
}
2022-05-09 13:24:42 +00:00
exists, err := c.s3.KeyExists(ctx, key)
if err != nil {
return nil, err
}
if !exists {
return nil, errors.New("key does not exist")
}
2022-05-09 12:52:18 +00:00
err = c.db.CreateShare(ctx, share)
if err != nil {
return nil, err
}
return share, nil
}
func (c *Client) GetObjectFromShare(ctx context.Context, share *types.Share) (s3.ObjectReader, error) {
return c.s3.GetObject(ctx, share.Key)
}
2022-05-09 13:36:43 +00:00
func (c *Client) DeleteShare(ctx context.Context, slug string) error {
return c.db.DeleteShare(ctx, slug)
}
2022-05-10 10:49:34 +00:00
func (c *Client) GetObjectMetadata(ctx context.Context, key string) (*types.Metadata, error) {
metadata, err := c.s3.GetObjectMetadata(ctx, key)
if err != nil {
return nil, err
}
if metadata.Filename == "" {
metadata.Filename = filepath.Base(key)
}
return metadata, nil
}
2022-05-14 21:25:50 +00:00
func (c *Client) GetAllShares(ctx context.Context) ([]*types.Share, error) {
return c.db.GetAllShares(ctx)
}