package v1 import ( "io" "os" "path/filepath" "quyun/v2/app/errorx" "quyun/v2/providers/storage" "github.com/gofiber/fiber/v3" ) // @provider type Storage struct { storage *storage.Storage } // Upload file // // @Router /v1/t/:tenantCode/storage/* [put] // @Summary Upload file // @Tags Storage // @Accept octet-stream // @Produce json // @Param expires query string true "Expiry" // @Param sign query string true "Signature" // @Success 200 {string} string "success" // @Bind expires query // @Bind sign query func (storageHandler *Storage) Upload(ctx fiber.Ctx, expires, sign string) (string, error) { key := ctx.Params("*") if err := storageHandler.storage.Verify("PUT", key, expires, sign); err != nil { return "", fiber.NewError(fiber.StatusForbidden, err.Error()) } // Save file localPath := storageHandler.storage.Config.LocalPath if localPath == "" { localPath = "./storage" } fullPath := filepath.Join(localPath, key) if err := os.MkdirAll(filepath.Dir(fullPath), 0o755); err != nil { return "", errorx.ErrFileSystemError.WithCause(err) } file, err := os.Create(fullPath) if err != nil { return "", errorx.ErrFileSystemError.WithCause(err) } defer file.Close() if _, err := io.Copy(file, ctx.Request().BodyStream()); err != nil { return "", errorx.ErrFileSystemError.WithCause(err) } return "success", nil } // Download file // // @Router /v1/t/:tenantCode/storage/* [get] // @Summary Download file // @Tags Storage // @Accept json // @Produce octet-stream // @Param expires query string true "Expiry" // @Param sign query string true "Signature" // @Success 200 {file} file // @Bind expires query // @Bind sign query func (storageHandler *Storage) Download(ctx fiber.Ctx, expires, sign string) error { key := ctx.Params("*") if err := storageHandler.storage.Verify("GET", key, expires, sign); err != nil { return fiber.NewError(fiber.StatusForbidden, err.Error()) } localPath := storageHandler.storage.Config.LocalPath if localPath == "" { localPath = "./storage" } fullPath := filepath.Join(localPath, key) if err := ctx.SendFile(fullPath); err != nil { return errorx.ErrFileSystemError.WithCause(err) } return nil }