package restricted import ( "strconv" "git.ma-al.com/goc_daniel/b2b/app/config" "git.ma-al.com/goc_daniel/b2b/app/service/storageService" "git.ma-al.com/goc_daniel/b2b/app/utils/i18n" "git.ma-al.com/goc_daniel/b2b/app/utils/nullable" "git.ma-al.com/goc_daniel/b2b/app/utils/response" "git.ma-al.com/goc_daniel/b2b/app/utils/responseErrors" "github.com/gofiber/fiber/v3" ) type StorageHandler struct { storageService *storageService.StorageService config *config.Config } func NewStorageHandler() *StorageHandler { return &StorageHandler{ storageService: storageService.New(), config: config.Get(), } } func StorageHandlerRoutes(r fiber.Router) fiber.Router { handler := NewStorageHandler() r.Get("/list-content", handler.ListContent) r.Get("/download-file", handler.DownloadFile) r.Post("/upload-file", handler.CreateFolder) r.Get("/create-folder", handler.CreateFolder) r.Get("/delete-file", handler.DeleteFile) r.Get("/delete-folder", handler.DeleteFolder) return r } // accepted path looks like e.g. "/folder1/" or "folder1" func (h *StorageHandler) ListContent(c fiber.Ctx) error { // relative path defaults to root directory abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("path")) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } entries_in_list, err := h.storageService.ListContent(abs_path) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(entries_in_list, 0, i18n.T_(c, response.Message_OK))) } func (h *StorageHandler) DownloadFile(c fiber.Ctx) error { abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("path")) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } f, filename, filesize, err := h.storageService.DownloadFilePrep(abs_path) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } c.Attachment(filename) c.Set("Content-Length", strconv.FormatInt(filesize, 10)) return c.SendStream(f, int(filesize)) } func (h *StorageHandler) UploadFile(c fiber.Ctx) error { abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("path")) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } f, err := c.FormFile("document") if err != nil { return c.Status(responseErrors.GetErrorStatus(responseErrors.ErrMissingFileFieldDocument)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, responseErrors.ErrMissingFileFieldDocument))) } err = h.storageService.UploadFile(abs_path, c.Query("name"), f) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } err = c.SaveFile(f, abs_path) return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) } func (h *StorageHandler) CreateFolder(c fiber.Ctx) error { abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("path")) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } err = h.storageService.CreateFolder(abs_path, c.Query("name")) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) } func (h *StorageHandler) DeleteFile(c fiber.Ctx) error { abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("path")) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } err = h.storageService.DeleteFile(abs_path) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) } func (h *StorageHandler) DeleteFolder(c fiber.Ctx) error { abs_path, err := h.storageService.AbsPath(h.config.Storage.RootFolder, c.Query("path")) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } err = h.storageService.DeleteFolder(abs_path) if err != nil { return c.Status(responseErrors.GetErrorStatus(err)). JSON(response.Make(nullable.GetNil(""), 0, responseErrors.GetErrorCode(c, err))) } return c.JSON(response.Make(nullable.GetNil(""), 0, i18n.T_(c, response.Message_OK))) }