♻️ Migrated to nexus
This commit is contained in:
@@ -3,8 +3,8 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
|
||||
@@ -82,11 +82,7 @@ func getAttachmentMeta(c *fiber.Ctx) error {
|
||||
|
||||
func updateAttachmentMeta(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("id", 0)
|
||||
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Alternative string `json:"alt"`
|
||||
@@ -116,11 +112,7 @@ func updateAttachmentMeta(c *fiber.Ctx) error {
|
||||
|
||||
func deleteAttachment(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("id", 0)
|
||||
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
attachment, err := services.GetAttachmentByID(uint(id))
|
||||
if err != nil {
|
||||
|
@@ -1,6 +1,9 @@
|
||||
package api
|
||||
|
||||
import "github.com/gofiber/fiber/v2"
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
func MapAPIs(app *fiber.App, baseURL string) {
|
||||
app.Get("/.well-known/destinations", getDestinations)
|
||||
@@ -9,32 +12,32 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
{
|
||||
api.Get("/pools", listPost)
|
||||
api.Get("/pools/:id", getPool)
|
||||
api.Post("/pools", createPool)
|
||||
api.Put("/pools/:id", updatePool)
|
||||
api.Delete("/pools/:id", deletePool)
|
||||
api.Post("/pools", sec.ValidatorMiddleware, createPool)
|
||||
api.Put("/pools/:id", sec.ValidatorMiddleware, updatePool)
|
||||
api.Delete("/pools/:id", sec.ValidatorMiddleware, deletePool)
|
||||
|
||||
api.Get("/attachments", listAttachment)
|
||||
api.Get("/attachments/:id/meta", getAttachmentMeta)
|
||||
api.Get("/attachments/:id", openAttachment)
|
||||
api.Post("/attachments", createAttachmentDirectly)
|
||||
api.Put("/attachments/:id", updateAttachmentMeta)
|
||||
api.Delete("/attachments/:id", deleteAttachment)
|
||||
api.Post("/attachments", sec.ValidatorMiddleware, createAttachmentDirectly)
|
||||
api.Put("/attachments/:id", sec.ValidatorMiddleware, updateAttachmentMeta)
|
||||
api.Delete("/attachments/:id", sec.ValidatorMiddleware, deleteAttachment)
|
||||
|
||||
api.Post("/attachments/multipart", createAttachmentMultipartPlaceholder)
|
||||
api.Post("/attachments/multipart/:file/:chunk", uploadAttachmentMultipart)
|
||||
api.Post("/attachments/multipart", sec.ValidatorMiddleware, createAttachmentMultipartPlaceholder)
|
||||
api.Post("/attachments/multipart/:file/:chunk", sec.ValidatorMiddleware, uploadAttachmentMultipart)
|
||||
|
||||
api.Get("/stickers/lookup", lookupStickerBatch)
|
||||
api.Get("/stickers/lookup/:alias", lookupSticker)
|
||||
api.Get("/stickers/packs", listStickerPacks)
|
||||
api.Get("/stickers/packs/:packId", getStickerPack)
|
||||
api.Post("/stickers/packs", createStickerPack)
|
||||
api.Put("/stickers/packs/:packId", updateStickerPack)
|
||||
api.Delete("/stickers/packs/:packId", deleteStickerPack)
|
||||
api.Post("/stickers/packs", sec.ValidatorMiddleware, createStickerPack)
|
||||
api.Put("/stickers/packs/:packId", sec.ValidatorMiddleware, updateStickerPack)
|
||||
api.Delete("/stickers/packs/:packId", sec.ValidatorMiddleware, deleteStickerPack)
|
||||
|
||||
api.Get("/stickers", listStickers)
|
||||
api.Get("/stickers/:stickerId", getSticker)
|
||||
api.Post("/stickers", createSticker)
|
||||
api.Put("/stickers/:stickerId", updateSticker)
|
||||
api.Delete("/stickers/:stickerId", deleteSticker)
|
||||
api.Post("/stickers", sec.ValidatorMiddleware, createSticker)
|
||||
api.Put("/stickers/:stickerId", sec.ValidatorMiddleware, updateSticker)
|
||||
api.Delete("/stickers/:stickerId", sec.ValidatorMiddleware, deleteSticker)
|
||||
}
|
||||
}
|
||||
|
@@ -1,10 +1,10 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"gorm.io/datatypes"
|
||||
)
|
||||
@@ -27,10 +27,7 @@ func getPool(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func createPool(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreateAttachmentPools", true); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
@@ -59,10 +56,7 @@ func createPool(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func updatePool(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required"`
|
||||
@@ -94,10 +88,7 @@ func updatePool(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func deletePool(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
id, _ := c.ParamsInt("id")
|
||||
pool, err := services.GetAttachmentPoolWithUser(uint(id), user.ID)
|
||||
|
@@ -2,10 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
@@ -55,10 +55,7 @@ func getStickerPack(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func createStickerPack(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Prefix string `json:"prefix" validate:"required,alphanum,min=2,max=12"`
|
||||
@@ -79,10 +76,7 @@ func createStickerPack(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func updateStickerPack(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Prefix string `json:"prefix" validate:"required,alphanum,min=2,max=12"`
|
||||
@@ -112,10 +106,7 @@ func updateStickerPack(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func deleteStickerPack(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
id, _ := c.ParamsInt("packId", 0)
|
||||
pack, err := services.GetStickerPackWithUser(uint(id), user.ID)
|
||||
|
@@ -2,10 +2,10 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
@@ -80,10 +80,7 @@ func getSticker(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func createSticker(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required,alphanum,min=2,max=12"`
|
||||
@@ -128,10 +125,7 @@ func createSticker(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func updateSticker(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Alias string `json:"alias" validate:"required,alphanum,min=2,max=12"`
|
||||
@@ -179,10 +173,7 @@ func updateSticker(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func deleteSticker(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
id, _ := c.ParamsInt("stickerId", 0)
|
||||
sticker, err := services.GetStickerWithUser(uint(id), user.ID)
|
||||
|
@@ -3,19 +3,16 @@ package api
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
jsoniter "github.com/json-iterator/go"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func createAttachmentDirectly(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
poolAlias := c.FormValue("pool")
|
||||
|
||||
@@ -34,8 +31,8 @@ func createAttachmentDirectly(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = gap.H.EnsureGrantedPerm(c, "CreateAttachments", file.Size); err != nil {
|
||||
return err
|
||||
if !user.HasPermNode("CreateAttachments", file.Size) {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you are not permitted to create attachments like this large")
|
||||
} else if pool.Config.Data().MaxFileSize != nil && file.Size > *pool.Config.Data().MaxFileSize {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment pool %s doesn't allow file larger than %d", pool.Alias, *pool.Config.Data().MaxFileSize))
|
||||
}
|
||||
@@ -68,7 +65,7 @@ func createAttachmentDirectly(c *fiber.Ctx) error {
|
||||
|
||||
tx.Commit()
|
||||
|
||||
metadata.Account = user
|
||||
metadata.Account = models.Account{UserInfo: user}
|
||||
metadata.Pool = &pool
|
||||
services.PublishAnalyzeTask(metadata)
|
||||
|
||||
|
@@ -4,19 +4,16 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func createAttachmentMultipartPlaceholder(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
var data struct {
|
||||
Pool string `json:"pool" validate:"required"`
|
||||
@@ -42,8 +39,8 @@ func createAttachmentMultipartPlaceholder(c *fiber.Ctx) error {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get attachment pool info: %v", err))
|
||||
}
|
||||
|
||||
if err = gap.H.EnsureGrantedPerm(c, "CreateAttachments", data.Size); err != nil {
|
||||
return err
|
||||
if !user.HasPermNode("CreateAttachments", data.Size) {
|
||||
return fiber.NewError(fiber.StatusForbidden, "you are not permitted to create attachments like this large")
|
||||
} else if pool.Config.Data().MaxFileSize != nil && *pool.Config.Data().MaxFileSize > data.Size {
|
||||
return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("attachment pool %s doesn't allow file larger than %d", pool.Alias, *pool.Config.Data().MaxFileSize))
|
||||
}
|
||||
@@ -72,10 +69,7 @@ func createAttachmentMultipartPlaceholder(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func uploadAttachmentMultipart(c *fiber.Ctx) error {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user := c.Locals("nex_user").(sec.UserInfo)
|
||||
|
||||
rid := c.Params("file")
|
||||
cid := c.Params("chunk")
|
||||
|
@@ -1,12 +1,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"git.solsynth.dev/hypernet/nexus/pkg/nex/sec"
|
||||
"strings"
|
||||
|
||||
"git.solsynth.dev/hydrogen/dealer/pkg/hyper"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/api"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
@@ -18,10 +15,14 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
var app *fiber.App
|
||||
var IReader *sec.InternalTokenReader
|
||||
|
||||
func NewServer() {
|
||||
app = fiber.New(fiber.Config{
|
||||
type App struct {
|
||||
app *fiber.App
|
||||
}
|
||||
|
||||
func NewServer() *App {
|
||||
app := fiber.New(fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
EnableIPValidation: true,
|
||||
ServerHeader: "Hydrogen.Paperclip",
|
||||
@@ -55,23 +56,15 @@ func NewServer() {
|
||||
Output: log.Logger,
|
||||
}))
|
||||
|
||||
tablePrefix := viper.GetString("database.prefix")
|
||||
app.Use(gap.H.AuthMiddleware)
|
||||
app.Use(hyper.LinkAccountMiddleware(
|
||||
database.C,
|
||||
tablePrefix+"accounts",
|
||||
func(u hyper.BaseUser) models.Account {
|
||||
return models.Account{
|
||||
BaseUser: u,
|
||||
}
|
||||
},
|
||||
))
|
||||
app.Use(sec.ContextMiddleware(IReader))
|
||||
|
||||
api.MapAPIs(app, "/api")
|
||||
|
||||
return &App{app}
|
||||
}
|
||||
|
||||
func Listen() {
|
||||
if err := app.Listen(viper.GetString("bind")); err != nil {
|
||||
func (v *App) Listen() {
|
||||
if err := v.app.Listen(viper.GetString("bind")); err != nil {
|
||||
log.Fatal().Err(err).Msg("An error occurred when starting server...")
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user