From af763d828a2911267c847045a1658ee7075936c4 Mon Sep 17 00:00:00 2001 From: Djeeberjr Date: Tue, 10 May 2022 12:49:34 +0200 Subject: [PATCH] added metadata --- internal/client/client.go | 14 ++++++++++++++ internal/s3/minio.go | 13 +++++++++++++ internal/s3/s3.go | 3 +++ internal/types/{share.go => types.go} | 6 ++++++ internal/web/web.go | 18 ++++++++++++++++++ 5 files changed, 54 insertions(+) rename internal/types/{share.go => types.go} (73%) diff --git a/internal/client/client.go b/internal/client/client.go index e250bce..842d153 100644 --- a/internal/client/client.go +++ b/internal/client/client.go @@ -4,6 +4,7 @@ import ( "context" "errors" "math/rand" + "path/filepath" "time" "git.kapelle.org/niklas/s3share/internal/db" @@ -90,3 +91,16 @@ func (c *Client) GetObjectFromShare(ctx context.Context, share *types.Share) (s3 func (c *Client) DeleteShare(ctx context.Context, slug string) error { return c.db.DeleteShare(ctx, slug) } + +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 +} diff --git a/internal/s3/minio.go b/internal/s3/minio.go index 323008c..f526382 100644 --- a/internal/s3/minio.go +++ b/internal/s3/minio.go @@ -3,6 +3,7 @@ package s3 import ( "context" + "git.kapelle.org/niklas/s3share/internal/types" "github.com/minio/minio-go/v7" "github.com/minio/minio-go/v7/pkg/credentials" "github.com/sirupsen/logrus" @@ -54,3 +55,15 @@ func (m *minioClient) KeyExists(ctx context.Context, key string) (bool, error) { return true, nil } + +func (m *minioClient) GetObjectMetadata(ctx context.Context, key string) (*types.Metadata, error) { + object, err := m.client.StatObject(ctx, m.bucket, key, minio.StatObjectOptions{}) + if err != nil { + return nil, err + } + + return &types.Metadata{ + Size: object.Size, + ContentType: object.ContentType, + }, nil +} diff --git a/internal/s3/s3.go b/internal/s3/s3.go index c59a67f..7f1ac27 100644 --- a/internal/s3/s3.go +++ b/internal/s3/s3.go @@ -3,6 +3,8 @@ package s3 import ( "context" "io" + + "git.kapelle.org/niklas/s3share/internal/types" ) type ObjectReader interface { @@ -15,4 +17,5 @@ type ObjectReader interface { type S3 interface { GetObject(ctx context.Context, key string) (ObjectReader, error) KeyExists(ctx context.Context, key string) (bool, error) + GetObjectMetadata(ctx context.Context, key string) (*types.Metadata, error) } diff --git a/internal/types/share.go b/internal/types/types.go similarity index 73% rename from internal/types/share.go rename to internal/types/types.go index 9284306..53ab430 100644 --- a/internal/types/share.go +++ b/internal/types/types.go @@ -13,3 +13,9 @@ type AppConfig struct { S3Bucket string Address string } + +type Metadata struct { + Size int64 + ContentType string + Filename string +} diff --git a/internal/web/web.go b/internal/web/web.go index be48e04..44dbf4a 100644 --- a/internal/web/web.go +++ b/internal/web/web.go @@ -4,6 +4,7 @@ import ( "encoding/json" "io" "net/http" + "strconv" "git.kapelle.org/niklas/s3share/internal/client" "github.com/gorilla/mux" @@ -30,6 +31,7 @@ func StartWebserver(addr string, client client.Client) error { share, err := client.GetShare(r.Context(), vars["slug"]) if err != nil { + logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -39,8 +41,20 @@ func StartWebserver(addr string, client client.Client) error { return } + metadata, err := client.GetObjectMetadata(r.Context(), share.Key) + if err != nil { + logrus.Error(err.Error()) + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + + w.Header().Set("Content-Type", metadata.ContentType) + w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10)) + w.Header().Set("Content-Disposition", "attachment; filename=\""+metadata.Filename+"\"") + obj, err := client.GetObjectFromShare(r.Context(), share) if err != nil { + logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -63,6 +77,7 @@ func StartWebserver(addr string, client client.Client) error { var shareParams createShare err := json.NewDecoder(r.Body).Decode(&shareParams) if err != nil { + logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusBadRequest) return } @@ -70,6 +85,7 @@ func StartWebserver(addr string, client client.Client) error { share, err := client.CreateShare(r.Context(), shareParams.Key) if err != nil { + logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return } @@ -85,12 +101,14 @@ func StartWebserver(addr string, client client.Client) error { var shareParams deleteShare err := json.NewDecoder(r.Body).Decode(&shareParams) if err != nil { + logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusBadRequest) return } err = client.DeleteShare(r.Context(), shareParams.Slug) if err != nil { + logrus.Error(err.Error()) http.Error(w, err.Error(), http.StatusInternalServerError) return }