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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// createRandomString creates a random string of length 6 to be used as a slug
|
2022-05-09 12:52:18 +00:00
|
|
|
func createRandomString() string {
|
|
|
|
s := make([]rune, 6)
|
|
|
|
for i := range s {
|
|
|
|
s[i] = letters[rand.Intn(len(letters))]
|
|
|
|
}
|
|
|
|
return string(s)
|
|
|
|
}
|
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// createValidSlug creates a valid slug that is not yet in use
|
2022-06-01 09:56:15 +00:00
|
|
|
func (c *Client) createValidSlug(ctx context.Context) (string, error) {
|
2022-05-09 12:52:18 +00:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// GetShare returns the share with the given slug, nil if not found
|
2022-05-09 12:52:18 +00:00
|
|
|
func (c *Client) GetShare(ctx context.Context, slug string) (*types.Share, error) {
|
|
|
|
return c.db.GetShare(ctx, slug)
|
|
|
|
}
|
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// CreateShare creates a new share with the given key and returns the share with the slug
|
2022-05-09 12:52:18 +00:00
|
|
|
func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, error) {
|
2022-06-01 09:56:15 +00:00
|
|
|
slug, err := c.createValidSlug(ctx)
|
2022-05-09 12:52:18 +00:00
|
|
|
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 {
|
2022-06-03 12:27:40 +00:00
|
|
|
return nil, types.ErrKeyNotFound
|
2022-05-09 13:24:42 +00:00
|
|
|
}
|
2022-05-09 12:52:18 +00:00
|
|
|
|
|
|
|
err = c.db.CreateShare(ctx, share)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return share, nil
|
|
|
|
}
|
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// GetObjectFromShare returns the s3 object to the given share
|
2022-05-09 12:52:18 +00:00
|
|
|
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
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// DeleteShare deletes the share with the given slug
|
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
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// GetObjectMetadata returns the metadata of the object with the given key
|
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
|
|
|
|
}
|
|
|
|
|
2022-06-01 09:56:15 +00:00
|
|
|
if metadata == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2022-05-10 10:49:34 +00:00
|
|
|
if metadata.Filename == "" {
|
|
|
|
metadata.Filename = filepath.Base(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
return metadata, nil
|
|
|
|
}
|
2022-05-14 21:25:50 +00:00
|
|
|
|
2022-06-03 12:28:00 +00:00
|
|
|
// GetAllShares returns all shares
|
2022-05-14 21:25:50 +00:00
|
|
|
func (c *Client) GetAllShares(ctx context.Context) ([]*types.Share, error) {
|
|
|
|
return c.db.GetAllShares(ctx)
|
|
|
|
}
|