commented client functions

This commit is contained in:
Djeeberjr 2022-06-03 14:28:00 +02:00
parent 2f5cd563ad
commit 339dbe617f

View File

@ -27,6 +27,7 @@ func NewClient(db db.DB, s3 s3.S3) *Client {
} }
} }
// createRandomString creates a random string of length 6 to be used as a slug
func createRandomString() string { func createRandomString() string {
s := make([]rune, 6) s := make([]rune, 6)
for i := range s { for i := range s {
@ -35,6 +36,7 @@ func createRandomString() string {
return string(s) return string(s)
} }
// createValidSlug creates a valid slug that is not yet in use
func (c *Client) createValidSlug(ctx context.Context) (string, error) { func (c *Client) createValidSlug(ctx context.Context) (string, error) {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
slug := createRandomString() slug := createRandomString()
@ -52,10 +54,12 @@ func (c *Client) createValidSlug(ctx context.Context) (string, error) {
return "", errors.New("could not create valid slug after 10 tries") return "", errors.New("could not create valid slug after 10 tries")
} }
// GetShare returns the share with the given slug, nil if not found
func (c *Client) GetShare(ctx context.Context, slug string) (*types.Share, error) { func (c *Client) GetShare(ctx context.Context, slug string) (*types.Share, error) {
return c.db.GetShare(ctx, slug) return c.db.GetShare(ctx, slug)
} }
// CreateShare creates a new share with the given key and returns the share with the slug
func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, error) { func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, error) {
slug, err := c.createValidSlug(ctx) slug, err := c.createValidSlug(ctx)
if err != nil { if err != nil {
@ -84,14 +88,17 @@ func (c *Client) CreateShare(ctx context.Context, key string) (*types.Share, err
return share, nil return share, nil
} }
// GetObjectFromShare returns the s3 object to the given share
func (c *Client) GetObjectFromShare(ctx context.Context, share *types.Share) (s3.ObjectReader, error) { func (c *Client) GetObjectFromShare(ctx context.Context, share *types.Share) (s3.ObjectReader, error) {
return c.s3.GetObject(ctx, share.Key) return c.s3.GetObject(ctx, share.Key)
} }
// DeleteShare deletes the share with the given slug
func (c *Client) DeleteShare(ctx context.Context, slug string) error { func (c *Client) DeleteShare(ctx context.Context, slug string) error {
return c.db.DeleteShare(ctx, slug) return c.db.DeleteShare(ctx, slug)
} }
// GetObjectMetadata returns the metadata of the object with the given key
func (c *Client) GetObjectMetadata(ctx context.Context, key string) (*types.Metadata, error) { func (c *Client) GetObjectMetadata(ctx context.Context, key string) (*types.Metadata, error) {
metadata, err := c.s3.GetObjectMetadata(ctx, key) metadata, err := c.s3.GetObjectMetadata(ctx, key)
if err != nil { if err != nil {
@ -109,6 +116,7 @@ func (c *Client) GetObjectMetadata(ctx context.Context, key string) (*types.Meta
return metadata, nil return metadata, nil
} }
// GetAllShares returns all shares
func (c *Client) GetAllShares(ctx context.Context) ([]*types.Share, error) { func (c *Client) GetAllShares(ctx context.Context) ([]*types.Share, error) {
return c.db.GetAllShares(ctx) return c.db.GetAllShares(ctx)
} }