added metadata
This commit is contained in:
parent
8cbeb1109f
commit
af763d828a
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"git.kapelle.org/niklas/s3share/internal/db"
|
"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 {
|
func (c *Client) DeleteShare(ctx context.Context, slug string) error {
|
||||||
return c.db.DeleteShare(ctx, slug)
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"git.kapelle.org/niklas/s3share/internal/types"
|
||||||
"github.com/minio/minio-go/v7"
|
"github.com/minio/minio-go/v7"
|
||||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -54,3 +55,15 @@ func (m *minioClient) KeyExists(ctx context.Context, key string) (bool, error) {
|
|||||||
|
|
||||||
return true, nil
|
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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
|
"git.kapelle.org/niklas/s3share/internal/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ObjectReader interface {
|
type ObjectReader interface {
|
||||||
@ -15,4 +17,5 @@ type ObjectReader interface {
|
|||||||
type S3 interface {
|
type S3 interface {
|
||||||
GetObject(ctx context.Context, key string) (ObjectReader, error)
|
GetObject(ctx context.Context, key string) (ObjectReader, error)
|
||||||
KeyExists(ctx context.Context, key string) (bool, 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
|
S3Bucket string
|
||||||
Address string
|
Address string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Metadata struct {
|
||||||
|
Size int64
|
||||||
|
ContentType string
|
||||||
|
Filename string
|
||||||
|
}
|
@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"git.kapelle.org/niklas/s3share/internal/client"
|
"git.kapelle.org/niklas/s3share/internal/client"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
@ -30,6 +31,7 @@ func StartWebserver(addr string, client client.Client) error {
|
|||||||
|
|
||||||
share, err := client.GetShare(r.Context(), vars["slug"])
|
share, err := client.GetShare(r.Context(), vars["slug"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logrus.Error(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -39,8 +41,20 @@ func StartWebserver(addr string, client client.Client) error {
|
|||||||
return
|
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)
|
obj, err := client.GetObjectFromShare(r.Context(), share)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logrus.Error(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -63,6 +77,7 @@ func StartWebserver(addr string, client client.Client) error {
|
|||||||
var shareParams createShare
|
var shareParams createShare
|
||||||
err := json.NewDecoder(r.Body).Decode(&shareParams)
|
err := json.NewDecoder(r.Body).Decode(&shareParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logrus.Error(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -70,6 +85,7 @@ func StartWebserver(addr string, client client.Client) error {
|
|||||||
share, err := client.CreateShare(r.Context(), shareParams.Key)
|
share, err := client.CreateShare(r.Context(), shareParams.Key)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logrus.Error(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -85,12 +101,14 @@ func StartWebserver(addr string, client client.Client) error {
|
|||||||
var shareParams deleteShare
|
var shareParams deleteShare
|
||||||
err := json.NewDecoder(r.Body).Decode(&shareParams)
|
err := json.NewDecoder(r.Body).Decode(&shareParams)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logrus.Error(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = client.DeleteShare(r.Context(), shareParams.Slug)
|
err = client.DeleteShare(r.Context(), shareParams.Slug)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
logrus.Error(err.Error())
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user