use s3Service interface

This commit is contained in:
2021-11-23 20:12:24 +01:00
parent 60817c2249
commit 8d85d645d6
9 changed files with 162 additions and 257 deletions

View File

@@ -4,9 +4,7 @@ import (
"context"
"fmt"
"io"
"mime"
"net/http"
"path/filepath"
"time"
"github.com/golang-jwt/jwt"
@@ -15,12 +13,12 @@ import (
"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/gqlerrors"
"github.com/graphql-go/handler"
"github.com/minio/minio-go/v7"
log "github.com/sirupsen/logrus"
helper "git.kapelle.org/niklas/s3browser/internal/helper"
"git.kapelle.org/niklas/s3browser/internal/loader"
"git.kapelle.org/niklas/s3browser/internal/s3"
types "git.kapelle.org/niklas/s3browser/internal/types"
)
@@ -112,7 +110,7 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
return
}
s3Client := ctx.Value("s3Client").(*minio.Client)
s3Client := ctx.Value("s3Client").(s3.S3Service)
idString := r.URL.Query().Get("id")
id := types.ParseID(idString)
@@ -124,7 +122,7 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
}
log.Debug("S3 'StatObject': ", id)
objInfo, err := s3Client.StatObject(context.Background(), id.Bucket, id.Key, minio.GetObjectOptions{})
objInfo, err := s3Client.StatObject(context.Background(), *id)
if err != nil {
log.Error("Failed to get object info: ", err)
@@ -139,7 +137,7 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
}
log.Debug("S3 'GetObject': ", id)
obj, err := s3Client.GetObject(context.Background(), id.Bucket, id.Key, minio.GetObjectOptions{})
obj, err := s3Client.GetObject(context.Background(), *id)
if err != nil {
log.Error("Failed to get object: ", err)
@@ -148,7 +146,7 @@ func httpGetFile(ctx context.Context, rw http.ResponseWriter, r *http.Request) {
}
rw.Header().Set("Cache-Control", "must-revalidate")
rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filepath.Base((objInfo.Key))))
rw.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", id.Name()))
rw.Header().Set("Content-Type", objInfo.ContentType)
rw.Header().Set("ETag", objInfo.ETag)
@@ -166,7 +164,7 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request)
return
}
s3Client := ctx.Value("s3Client").(*minio.Client)
s3Client := ctx.Value("s3Client").(s3.S3Service)
idString := r.URL.Query().Get("id")
@@ -180,13 +178,11 @@ func httpPostFile(ctx context.Context, rw http.ResponseWriter, r *http.Request)
id.Normalize()
contentType := r.Header.Get("Content-Type")
mimeType, _, _ := mime.ParseMediaType(contentType)
// contentType := r.Header.Get("Content-Type")
// mimeType, _, _ := mime.ParseMediaType(contentType)
log.Debug("S3 'PutObject': ", id)
_, err := s3Client.PutObject(context.Background(), id.Bucket, id.Key, r.Body, r.ContentLength, minio.PutObjectOptions{
ContentType: mimeType,
})
err := s3Client.PutObject(context.Background(), *id, r.Body, r.ContentLength) // TODO: put content type
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)