2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2024-12-13 13:00:31 +00:00
|
|
|
"archive/tar"
|
2023-08-31 08:37:03 +00:00
|
|
|
"archive/zip"
|
|
|
|
"bufio"
|
2024-12-13 13:00:31 +00:00
|
|
|
"compress/gzip"
|
2023-08-31 08:37:03 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/data"
|
2023-08-31 08:37:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/response"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/utils"
|
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"github.com/valyala/fasthttp"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DownloadByAddressOrBucketName handles download requests using simple cid/oid or bucketname/key format.
|
|
|
|
func (h *Handler) DownloadByAddressOrBucketName(c *fasthttp.RequestCtx) {
|
2024-10-10 08:59:53 +00:00
|
|
|
oidURLParam := c.UserValue("oid").(string)
|
|
|
|
downloadQueryParam := c.QueryArgs().GetBool("download")
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case isObjectID(oidURLParam):
|
|
|
|
h.byNativeAddress(c, h.receiveFile)
|
|
|
|
case !isContainerRoot(oidURLParam) && (downloadQueryParam || !isDir(oidURLParam)):
|
|
|
|
h.byS3Path(c, h.receiveFile)
|
|
|
|
default:
|
|
|
|
h.browseIndex(c)
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadByAttribute handles attribute-based download requests.
|
|
|
|
func (h *Handler) DownloadByAttribute(c *fasthttp.RequestCtx) {
|
|
|
|
h.byAttribute(c, h.receiveFile)
|
|
|
|
}
|
|
|
|
|
2024-10-10 08:59:53 +00:00
|
|
|
func (h *Handler) search(ctx context.Context, cnrID cid.ID, key, val string, op object.SearchMatchType) (ResObjectSearch, error) {
|
2023-08-31 08:37:03 +00:00
|
|
|
filters := object.NewSearchFilters()
|
|
|
|
filters.AddRootFilter()
|
|
|
|
filters.AddFilter(key, val, op)
|
|
|
|
|
2024-06-17 14:56:43 +00:00
|
|
|
prm := PrmObjectSearch{
|
|
|
|
PrmAuth: PrmAuth{
|
|
|
|
BearerToken: bearerToken(ctx),
|
|
|
|
},
|
2024-10-10 08:59:53 +00:00
|
|
|
Container: cnrID,
|
2024-06-17 14:56:43 +00:00
|
|
|
Filters: filters,
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
2024-06-17 14:56:43 +00:00
|
|
|
return h.frostfs.SearchObjects(ctx, prm)
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
// DownloadZipped handles zip by prefix requests.
|
|
|
|
func (h *Handler) DownloadZipped(c *fasthttp.RequestCtx) {
|
|
|
|
scid, _ := c.UserValue("cid").(string)
|
|
|
|
|
|
|
|
ctx := utils.GetContextFromRequest(c)
|
|
|
|
log := utils.GetReqLogOrDefault(ctx, h.log)
|
|
|
|
bktInfo, err := h.getBucketInfo(ctx, scid, log)
|
|
|
|
if err != nil {
|
|
|
|
logAndSendBucketError(c, log, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resSearch, err := h.searchObjectsByPrefix(c, log, bktInfo.CID)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderContentType, "application/zip")
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderContentDisposition, "attachment; filename=\"archive.zip\"")
|
|
|
|
|
|
|
|
c.SetBodyStreamWriter(h.getZipResponseWriter(ctx, log, resSearch, bktInfo))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) getZipResponseWriter(ctx context.Context, log *zap.Logger, resSearch ResObjectSearch, bktInfo *data.BucketInfo) func(w *bufio.Writer) {
|
|
|
|
return func(w *bufio.Writer) {
|
|
|
|
defer resSearch.Close()
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(w)
|
|
|
|
var bufZip []byte
|
|
|
|
|
|
|
|
errIter := resSearch.Iterate(h.putObjectToArchive(ctx, log, bktInfo.CID, &bufZip,
|
|
|
|
func(obj *object.Object) (io.Writer, error) {
|
|
|
|
return h.createZipFile(zipWriter, obj)
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
if errIter != nil {
|
|
|
|
log.Error(logs.IteratingOverSelectedObjectsFailed, zap.Error(errIter))
|
|
|
|
} else if bufZip == nil {
|
|
|
|
log.Error(logs.ObjectsNotFound)
|
|
|
|
}
|
|
|
|
if err := zipWriter.Close(); err != nil {
|
|
|
|
log.Error(logs.CloseZipWriter, zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) createZipFile(zw *zip.Writer, obj *object.Object) (io.Writer, error) {
|
2023-08-31 08:37:03 +00:00
|
|
|
method := zip.Store
|
2023-09-05 15:17:22 +00:00
|
|
|
if h.config.ZipCompression() {
|
2023-08-31 08:37:03 +00:00
|
|
|
method = zip.Deflate
|
|
|
|
}
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
filePath := getFilePath(obj)
|
2023-08-31 08:37:03 +00:00
|
|
|
if len(filePath) == 0 || filePath[len(filePath)-1] == '/' {
|
|
|
|
return nil, fmt.Errorf("invalid filepath '%s'", filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return zw.CreateHeader(&zip.FileHeader{
|
|
|
|
Name: filePath,
|
|
|
|
Method: method,
|
|
|
|
Modified: time.Now(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
// DownloadTar forms tar.gz from objects by prefix.
|
|
|
|
func (h *Handler) DownloadTar(c *fasthttp.RequestCtx) {
|
2023-08-31 08:37:03 +00:00
|
|
|
scid, _ := c.UserValue("cid").(string)
|
2024-02-29 09:50:56 +00:00
|
|
|
|
2024-10-08 09:08:39 +00:00
|
|
|
ctx := utils.GetContextFromRequest(c)
|
|
|
|
log := utils.GetReqLogOrDefault(ctx, h.log)
|
2023-10-04 11:50:37 +00:00
|
|
|
bktInfo, err := h.getBucketInfo(ctx, scid, log)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
2023-10-04 11:50:37 +00:00
|
|
|
logAndSendBucketError(c, log, err)
|
2023-08-31 08:37:03 +00:00
|
|
|
return
|
|
|
|
}
|
2024-12-13 13:00:31 +00:00
|
|
|
resSearch, err := h.searchObjectsByPrefix(c, log, bktInfo.CID)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
c.Response.Header.Set(fasthttp.HeaderContentType, "application/x-gzip")
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderContentDisposition, "attachment; filename=\"archive.tar.gz\"")
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
c.SetBodyStreamWriter(h.getTarResponseWriter(ctx, log, resSearch, bktInfo))
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
func (h *Handler) getTarResponseWriter(ctx context.Context, log *zap.Logger, resSearch ResObjectSearch, bktInfo *data.BucketInfo) func(w *bufio.Writer) {
|
|
|
|
return func(w *bufio.Writer) {
|
|
|
|
defer resSearch.Close()
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
var gzipWriter *gzip.Writer
|
|
|
|
if h.config.ZipCompression() {
|
|
|
|
gzipWriter, _ = gzip.NewWriterLevel(w, gzip.DefaultCompression)
|
|
|
|
} else {
|
|
|
|
gzipWriter, _ = gzip.NewWriterLevel(w, gzip.NoCompression)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err := gzipWriter.Close(); err != nil {
|
|
|
|
log.Error(logs.CloseGzipWriter, zap.Error(err))
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
2024-12-13 13:00:31 +00:00
|
|
|
}()
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
tarWriter := tar.NewWriter(gzipWriter)
|
|
|
|
defer func() {
|
|
|
|
if err := tarWriter.Close(); err != nil {
|
|
|
|
log.Error(logs.CloseTarWriter, zap.Error(err))
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
2024-12-13 13:00:31 +00:00
|
|
|
}()
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
var bufZip []byte
|
|
|
|
|
|
|
|
errIter := resSearch.Iterate(h.putObjectToArchive(ctx, log, bktInfo.CID, &bufZip,
|
|
|
|
func(obj *object.Object) (io.Writer, error) {
|
|
|
|
return h.createTarFile(tarWriter, obj)
|
|
|
|
}),
|
|
|
|
)
|
2023-08-31 08:37:03 +00:00
|
|
|
if errIter != nil {
|
|
|
|
log.Error(logs.IteratingOverSelectedObjectsFailed, zap.Error(errIter))
|
2024-12-13 13:00:31 +00:00
|
|
|
} else if bufZip == nil {
|
2023-08-31 08:37:03 +00:00
|
|
|
log.Error(logs.ObjectsNotFound)
|
|
|
|
}
|
2024-12-13 13:00:31 +00:00
|
|
|
}
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
func (h *Handler) createTarFile(tw *tar.Writer, obj *object.Object) (io.Writer, error) {
|
|
|
|
filePath := getFilePath(obj)
|
|
|
|
if len(filePath) == 0 || filePath[len(filePath)-1] == '/' {
|
|
|
|
return nil, fmt.Errorf("invalid filepath '%s'", filePath)
|
|
|
|
}
|
|
|
|
|
|
|
|
return tw, tw.WriteHeader(&tar.Header{
|
|
|
|
Name: filePath,
|
|
|
|
Mode: 0655,
|
|
|
|
Size: int64(obj.PayloadSize()),
|
2023-08-31 08:37:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
func (h *Handler) putObjectToArchive(ctx context.Context, log *zap.Logger, cnrID cid.ID, bufZip *[]byte, createArchiveHeader func(obj *object.Object) (io.Writer, error)) func(id oid.ID) bool {
|
|
|
|
return func(id oid.ID) bool {
|
|
|
|
log := log.With(zap.String("oid", id.EncodeToString()))
|
|
|
|
|
|
|
|
if *bufZip == nil {
|
|
|
|
*bufZip = make([]byte, 3<<20) // the same as for upload
|
|
|
|
}
|
|
|
|
|
|
|
|
prm := PrmObjectGet{
|
|
|
|
PrmAuth: PrmAuth{
|
|
|
|
BearerToken: bearerToken(ctx),
|
|
|
|
},
|
|
|
|
Address: newAddress(cnrID, id),
|
|
|
|
}
|
|
|
|
|
|
|
|
resGet, err := h.frostfs.GetObject(ctx, prm)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(logs.FailedToGetObject, zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
fileWriter, err := createArchiveHeader(&resGet.Header)
|
|
|
|
if err != nil {
|
|
|
|
log.Error(logs.FailedToAddObjectToArchive, zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.writeToArchive(resGet, fileWriter, *bufZip); err != nil {
|
|
|
|
log.Error(logs.FailedToAddObjectToArchive, zap.Error(err))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
2024-12-13 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) searchObjectsByPrefix(c *fasthttp.RequestCtx, log *zap.Logger, cnrID cid.ID) (ResObjectSearch, error) {
|
|
|
|
scid := cnrID.EncodeToString()
|
|
|
|
prefix, _ := c.UserValue("prefix").(string)
|
|
|
|
|
|
|
|
ctx := utils.GetContextFromRequest(c)
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
prefix, err := url.QueryUnescape(prefix)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
2024-12-13 13:00:31 +00:00
|
|
|
log.Error(logs.FailedToUnescapeQuery, zap.String("cid", scid), zap.String("prefix", prefix), zap.Error(err))
|
|
|
|
response.Error(c, "could not unescape prefix: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return nil, err
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
log = log.With(zap.String("cid", scid), zap.String("prefix", prefix))
|
|
|
|
|
|
|
|
resSearch, err := h.search(ctx, cnrID, object.AttributeFilePath, prefix, object.MatchCommonPrefix)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
2024-12-13 13:00:31 +00:00
|
|
|
log.Error(logs.CouldNotSearchForObjects, zap.Error(err))
|
|
|
|
response.Error(c, "could not search for objects: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return nil, err
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
2024-12-13 13:00:31 +00:00
|
|
|
return resSearch, nil
|
|
|
|
}
|
2023-08-31 08:37:03 +00:00
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
func (h *Handler) writeToArchive(resGet *Object, objWriter io.Writer, bufZip []byte) error {
|
|
|
|
var err error
|
2023-08-31 08:37:03 +00:00
|
|
|
if _, err = io.CopyBuffer(objWriter, resGet.Payload, bufZip); err != nil {
|
|
|
|
return fmt.Errorf("copy object payload to zip file: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = resGet.Payload.Close(); err != nil {
|
|
|
|
return fmt.Errorf("object body close error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-12-13 13:00:31 +00:00
|
|
|
func getFilePath(obj *object.Object) string {
|
2023-08-31 08:37:03 +00:00
|
|
|
for _, attr := range obj.Attributes() {
|
|
|
|
if attr.Key() == object.AttributeFilePath {
|
|
|
|
return attr.Value()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|