388 lines
11 KiB
Go
388 lines
11 KiB
Go
package api
|
||
|
||
import (
|
||
"fmt"
|
||
"gitea.pena/PenaSide/penadisk/dal"
|
||
"github.com/gofiber/fiber/v2"
|
||
"strings"
|
||
)
|
||
|
||
type ReqGetResources struct {
|
||
Path string `query:"path"`
|
||
Recursive bool `query:"recursive"`
|
||
WithVersions bool `query:"with_versions"`
|
||
}
|
||
|
||
// GetResources - получить информацию о папке или файле по пути
|
||
func (a *API) GetResources(c *fiber.Ctx) error {
|
||
var req ReqGetResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
result := client.GetResourcesList(c.Context(), req.Path, req.Recursive, req.WithVersions)
|
||
|
||
if len(result) == 0 {
|
||
return fiber.NewError(fiber.StatusNotFound, "resource not found")
|
||
}
|
||
|
||
return c.Status(fiber.StatusOK).JSON(result)
|
||
}
|
||
|
||
type ReqPutResources struct {
|
||
Path string `query:"path" validate:"required"`
|
||
}
|
||
|
||
// PutResources - создать папку
|
||
func (a *API) PutResources(c *fiber.Ctx) error {
|
||
var req ReqPutResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
err = client.PutResources(c.Context(), req.Path)
|
||
if err != nil {
|
||
return err
|
||
}
|
||
|
||
return c.SendStatus(fiber.StatusCreated)
|
||
}
|
||
|
||
type ReqUploadResources struct {
|
||
Path string `json:"path" validate:"required" `
|
||
Overwrite bool `json:"overwrite"`
|
||
}
|
||
|
||
// UploadResources - загрузить ресурс в хранилище.
|
||
// Ограничение на загрузку 50МБ установлено в fiber.Config.BodyLimit
|
||
func (a *API) UploadResources(c *fiber.Ctx) error {
|
||
var req ReqUploadResources
|
||
|
||
err := c.BodyParser(&req)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
formFile, err := c.FormFile("file")
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
// make vars for upload
|
||
contentType := formFile.Header.Get("Content-Type")
|
||
size := formFile.Size
|
||
|
||
var fullPath string
|
||
if !strings.HasSuffix(req.Path, "/") {
|
||
fullPath = fmt.Sprintf("%v/%v", req.Path, formFile.Filename)
|
||
} else {
|
||
fullPath = req.Path + formFile.Filename
|
||
}
|
||
|
||
file, err := formFile.Open()
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
// connect to minio
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
isExist, err := client.IsBucketExists(c.Context())
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
if !isExist {
|
||
err = client.CreateBucket(c.Context())
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
err = client.SetBucketQuota(100 << 20)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
}
|
||
|
||
fmt.Println("VAAAAALUEABLE", file, size, fullPath, contentType)
|
||
// upload file
|
||
err = client.UploadResources(c.Context(), file, size, fullPath, contentType)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.SendStatus(fiber.StatusCreated)
|
||
}
|
||
|
||
type ReqDownloadResources struct {
|
||
Path string `query:"path" validate:"required"`
|
||
}
|
||
|
||
type RespDownloadResources struct {
|
||
Href string `json:"href"`
|
||
}
|
||
|
||
// DownloadResources - получить ссылку на скачивание файла
|
||
func (a *API) DownloadResources(c *fiber.Ctx) error {
|
||
var req ReqDownloadResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
// connect to minio
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
result, err := client.DownloadResources(c.Context(), req.Path)
|
||
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.Status(fiber.StatusOK).JSON(&RespDownloadResources{Href: result})
|
||
}
|
||
|
||
type ReqCopyResources struct {
|
||
From string `query:"from" validate:"required"` // Путь к копируемому ресурсу
|
||
Path string `query:"path" validate:"required"` // Путь к создаваемому ресурсу
|
||
Overwrite bool `query:"overwrite"` // Перезаписать существующий ресурс
|
||
}
|
||
|
||
// CopyResources - скопировать ресурс
|
||
func (a *API) CopyResources(c *fiber.Ctx) error {
|
||
var req ReqCopyResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
// connect to minio
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
if !req.Overwrite {
|
||
list := client.GetResourcesList(c.Context(), req.Path, false, false)
|
||
if list != nil {
|
||
return fiber.NewError(fiber.StatusLocked, "file already exist")
|
||
}
|
||
}
|
||
|
||
err = client.CopyResources(c.Context(), req.From, req.Path)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.SendStatus(fiber.StatusCreated)
|
||
}
|
||
|
||
type ReqMoveResources struct {
|
||
From string `query:"from" validate:"required"` // Путь к перемещаемому ресурсу
|
||
Path string `query:"path" validate:"required"` // Путь к создаваемому ресурсу
|
||
Overwrite bool `query:"overwrite"` // Перезаписать существующий ресурс
|
||
}
|
||
|
||
// MoveResources - переместить ресурс
|
||
func (a *API) MoveResources(c *fiber.Ctx) error {
|
||
var req ReqMoveResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
if !req.Overwrite {
|
||
list := client.GetResourcesList(c.Context(), req.Path, false, false)
|
||
if list != nil {
|
||
return fiber.NewError(fiber.StatusLocked, "file already exist")
|
||
}
|
||
}
|
||
|
||
err = client.MoveResources(c.Context(), req.From, req.Path)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.SendStatus(fiber.StatusCreated)
|
||
}
|
||
|
||
type ReqDeleteResources struct {
|
||
Path string `query:"path" validate:"required"` // Путь к удаляемому ресурсу
|
||
Permanently bool `query:"permanently"` // Удалить все версии
|
||
}
|
||
|
||
// DeleteResources - удалить ресурс
|
||
func (a *API) DeleteResources(c *fiber.Ctx) error {
|
||
var req ReqDeleteResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
err = client.DeleteResources(c.Context(), req.Path, req.Permanently)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.SendStatus(fiber.StatusNoContent)
|
||
}
|
||
|
||
type ReqPublishResources struct {
|
||
Path string `query:"path" validate:"required"` // Путь публикуемого ресурса
|
||
}
|
||
|
||
type RespPublishResources struct {
|
||
Href string `json:"href"`
|
||
}
|
||
|
||
// PublishResources - опубликовать ресурс
|
||
func (a *API) PublishResources(c *fiber.Ctx) error {
|
||
var req ReqDeleteResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
href, err := client.PublishResources(c.Context(), req.Path)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.Status(fiber.StatusOK).JSON(&RespPublishResources{Href: href})
|
||
}
|
||
|
||
type ReqUnpublishResources struct {
|
||
Path string `query:"path" validate:"required"`
|
||
}
|
||
|
||
// UnpublishResources - отменить публикацию ресурса
|
||
func (a *API) UnpublishResources(c *fiber.Ctx) error {
|
||
var req ReqUnpublishResources
|
||
|
||
err := c.QueryParser(&req)
|
||
if err != nil {
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusBadRequest, err.Error())
|
||
}
|
||
}
|
||
|
||
errValidate := ValidateStruct(c.Context(), &req)
|
||
|
||
if errValidate != nil {
|
||
return c.Status(fiber.StatusBadRequest).JSON(errValidate)
|
||
}
|
||
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
err = client.UnpublishResources(c.Context(), req.Path)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.SendStatus(fiber.StatusOK)
|
||
}
|
||
|
||
// GetPublicResources - получить список опубликованных ресурсов
|
||
func (a *API) GetPublicResources(c *fiber.Ctx) error {
|
||
client, err := dal.NewClient(c.Context(), a.logger, c.Locals("userID").(string), a.minio)
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
result, err := client.GetPublicResources(c.Context())
|
||
if err != nil {
|
||
return fiber.NewError(fiber.StatusInternalServerError, err.Error())
|
||
}
|
||
|
||
return c.Status(fiber.StatusOK).JSON(result)
|
||
}
|