From 96085269a422d0455481c4dd69a4060fb253aa0d Mon Sep 17 00:00:00 2001 From: P4vlushaaa <112944483+P4vlushaaa@users.noreply.github.com> Date: Thu, 23 Jan 2025 14:05:35 +0300 Subject: [PATCH] Create upload_handler.go --- .../internal/handlers/upload_handler.go | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 services/frostfs-uploader/internal/handlers/upload_handler.go diff --git a/services/frostfs-uploader/internal/handlers/upload_handler.go b/services/frostfs-uploader/internal/handlers/upload_handler.go new file mode 100644 index 0000000..c376df7 --- /dev/null +++ b/services/frostfs-uploader/internal/handlers/upload_handler.go @@ -0,0 +1,65 @@ +package handlers + +import ( + "context" + "encoding/json" + "io" + "net/http" + + "github.com/nspcc-dev/neofs-sdk-go/client" + "github.com/nspcc-dev/neofs-sdk-go/container" + "web3-onlyfans/services/frostfs-uploader/internal/frostuploader" + "web3-onlyfans/services/frostfs-uploader/internal/utils" +) + +func UploadHandler(w http.ResponseWriter, r *http.Request, cfg *utils.Config, logger utils.Logger) { + file, header, err := r.FormFile("file") + if err != nil { + http.Error(w, "no file found", http.StatusBadRequest) + return + } + defer file.Close() + + data, err := io.ReadAll(file) + if err != nil { + http.Error(w, "read file error", http.StatusInternalServerError) + return + } + + // Подключаемся к FrostFS + cli, err := client.New(client.WithDefaultPrivateKeyStr(cfg.FrostfsPrivKey)) + if err != nil { + logger.Errorf("client init: %v", err) + http.Error(w, "FrostFS init error", http.StatusInternalServerError) + return + } + defer cli.Close() + + if err = cli.Dial(cfg.FrostfsEndpoint); err != nil { + logger.Errorf("dial error: %v", err) + http.Error(w, "FrostFS dial error", http.StatusInternalServerError) + return + } + + cntr, err := container.IDFromString(cfg.FrostfsContainer) + if err != nil { + logger.Errorf("invalid container: %v", err) + http.Error(w, "invalid container", http.StatusInternalServerError) + return + } + + oid, err := frostuploader.UploadFile(context.Background(), cli, cntr, data) + if err != nil { + logger.Errorf("upload error: %v", err) + http.Error(w, "upload error", http.StatusInternalServerError) + return + } + + resp := map[string]any{ + "status": "ok", + "object_id": oid, + "filename": header.Filename, + } + w.Header().Set("Content-Type", "application/json") + json.NewEncoder(w).Encode(resp) +}