added metadata
This commit is contained in:
parent
8cbeb1109f
commit
af763d828a
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -13,3 +13,9 @@ type AppConfig struct {
|
||||
S3Bucket string
|
||||
Address string
|
||||
}
|
||||
|
||||
type Metadata struct {
|
||||
Size int64
|
||||
ContentType string
|
||||
Filename string
|
||||
}
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user