2023-08-31 08:37:03 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"bufio"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/bearer"
|
|
|
|
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) {
|
|
|
|
test, _ := c.UserValue("oid").(string)
|
|
|
|
var id oid.ID
|
|
|
|
err := id.DecodeString(test)
|
|
|
|
if err != nil {
|
2023-10-04 11:50:37 +00:00
|
|
|
h.byObjectName(c, h.receiveFile)
|
2023-08-31 08:37:03 +00:00
|
|
|
} else {
|
|
|
|
h.byAddress(c, h.receiveFile)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) newRequest(ctx *fasthttp.RequestCtx, log *zap.Logger) *request {
|
|
|
|
return &request{
|
|
|
|
RequestCtx: ctx,
|
|
|
|
log: log,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadByAttribute handles attribute-based download requests.
|
|
|
|
func (h *Handler) DownloadByAttribute(c *fasthttp.RequestCtx) {
|
|
|
|
h.byAttribute(c, h.receiveFile)
|
|
|
|
}
|
|
|
|
|
2024-06-17 14:56:43 +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),
|
|
|
|
},
|
|
|
|
Container: *cnrID,
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) addObjectToZip(zw *zip.Writer, obj *object.Object) (io.Writer, error) {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
filePath := getZipFilePath(obj)
|
|
|
|
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(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadZipped handles zip by prefix requests.
|
|
|
|
func (h *Handler) DownloadZipped(c *fasthttp.RequestCtx) {
|
|
|
|
scid, _ := c.UserValue("cid").(string)
|
2024-02-29 09:50:56 +00:00
|
|
|
prefix, _ := c.UserValue("prefix").(string)
|
|
|
|
|
2024-10-08 09:08:39 +00:00
|
|
|
ctx := utils.GetContextFromRequest(c)
|
|
|
|
log := utils.GetReqLogOrDefault(ctx, h.log)
|
|
|
|
|
2024-02-29 09:50:56 +00:00
|
|
|
prefix, err := url.QueryUnescape(prefix)
|
|
|
|
if err != nil {
|
2024-10-08 09:08:39 +00:00
|
|
|
log.Error(logs.FailedToUnescapeQuery, zap.String("cid", scid), zap.String("prefix", prefix), zap.Error(err))
|
2024-02-29 09:50:56 +00:00
|
|
|
response.Error(c, "could not unescape prefix: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-08 09:08:39 +00:00
|
|
|
log = log.With(zap.String("cid", scid), zap.String("prefix", prefix))
|
2023-08-31 08:37:03 +00:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
resSearch, err := h.search(ctx, &bktInfo.CID, object.AttributeFilePath, prefix, object.MatchCommonPrefix)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(logs.CouldNotSearchForObjects, zap.Error(err))
|
|
|
|
response.Error(c, "could not search for objects: "+err.Error(), fasthttp.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderContentType, "application/zip")
|
|
|
|
c.Response.Header.Set(fasthttp.HeaderContentDisposition, "attachment; filename=\"archive.zip\"")
|
|
|
|
c.Response.SetStatusCode(http.StatusOK)
|
|
|
|
|
|
|
|
c.SetBodyStreamWriter(func(w *bufio.Writer) {
|
|
|
|
defer resSearch.Close()
|
|
|
|
|
|
|
|
zipWriter := zip.NewWriter(w)
|
|
|
|
|
|
|
|
var bufZip []byte
|
|
|
|
var addr oid.Address
|
|
|
|
|
|
|
|
empty := true
|
|
|
|
called := false
|
|
|
|
btoken := bearerToken(ctx)
|
2023-10-04 11:50:37 +00:00
|
|
|
addr.SetContainer(bktInfo.CID)
|
2023-08-31 08:37:03 +00:00
|
|
|
|
|
|
|
errIter := resSearch.Iterate(func(id oid.ID) bool {
|
|
|
|
called = true
|
|
|
|
|
|
|
|
if empty {
|
|
|
|
bufZip = make([]byte, 3<<20) // the same as for upload
|
|
|
|
}
|
|
|
|
empty = false
|
|
|
|
|
|
|
|
addr.SetObject(id)
|
|
|
|
if err = h.zipObject(ctx, zipWriter, addr, btoken, bufZip); err != nil {
|
|
|
|
log.Error(logs.FailedToAddObjectToArchive, zap.String("oid", id.EncodeToString()), zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if errIter != nil {
|
|
|
|
log.Error(logs.IteratingOverSelectedObjectsFailed, zap.Error(errIter))
|
|
|
|
} else if !called {
|
|
|
|
log.Error(logs.ObjectsNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = zipWriter.Close(); err != nil {
|
|
|
|
log.Error(logs.CloseZipWriter, zap.Error(err))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *Handler) zipObject(ctx context.Context, zipWriter *zip.Writer, addr oid.Address, btoken *bearer.Token, bufZip []byte) error {
|
2024-07-15 13:35:08 +00:00
|
|
|
prm := PrmObjectGet{
|
2024-06-17 14:56:43 +00:00
|
|
|
PrmAuth: PrmAuth{
|
|
|
|
BearerToken: btoken,
|
|
|
|
},
|
2024-06-25 12:31:46 +00:00
|
|
|
Address: addr,
|
2023-08-31 08:37:03 +00:00
|
|
|
}
|
|
|
|
|
2024-07-15 13:35:08 +00:00
|
|
|
resGet, err := h.frostfs.GetObject(ctx, prm)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("get FrostFS object: %v", err)
|
|
|
|
}
|
|
|
|
|
2024-07-15 13:35:08 +00:00
|
|
|
objWriter, err := h.addObjectToZip(zipWriter, &resGet.Header)
|
2023-08-31 08:37:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("zip create header: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = zipWriter.Flush(); err != nil {
|
|
|
|
return fmt.Errorf("flush zip writer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getZipFilePath(obj *object.Object) string {
|
|
|
|
for _, attr := range obj.Attributes() {
|
|
|
|
if attr.Key() == object.AttributeFilePath {
|
|
|
|
return attr.Value()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|