diff --git a/pkg/filekit/models/attachments.go b/pkg/filekit/models/attachments.go index d42ba17..865318b 100644 --- a/pkg/filekit/models/attachments.go +++ b/pkg/filekit/models/attachments.go @@ -14,7 +14,8 @@ import ( ) const ( - AttachmentDstTemporary = 0 // The destination 0 is a reserved config for pre-upload processing + AttachmentDstExternal = -1 // The destination marked the file did not store inside our service + AttachmentDstTemporary = 0 // Destination 0 is a reserved config for pre-upload processing ) const ( diff --git a/pkg/internal/fs/downloader.go b/pkg/internal/fs/downloader.go index 9c92554..c01fbcc 100644 --- a/pkg/internal/fs/downloader.go +++ b/pkg/internal/fs/downloader.go @@ -6,8 +6,8 @@ import ( "os" "path/filepath" - pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + pkg "git.solsynth.dev/hypernet/paperclip/pkg/internal" jsoniter "github.com/json-iterator/go" "github.com/minio/minio-go/v7" "github.com/spf13/viper" diff --git a/pkg/internal/server/api/boost_api.go b/pkg/internal/server/api/boost_api.go index e369ef1..c0b84fd 100644 --- a/pkg/internal/server/api/boost_api.go +++ b/pkg/internal/server/api/boost_api.go @@ -2,8 +2,8 @@ package api import ( "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts" "git.solsynth.dev/hypernet/paperclip/pkg/internal/services" "github.com/gofiber/fiber/v2" diff --git a/pkg/internal/server/api/index.go b/pkg/internal/server/api/index.go index dba7dc2..4efc084 100644 --- a/pkg/internal/server/api/index.go +++ b/pkg/internal/server/api/index.go @@ -37,6 +37,7 @@ func MapAPIs(app *fiber.App, baseURL string) { attachments.Get("/:id/meta", getAttachmentMeta) attachments.Get("/:id", openAttachment) attachments.Post("/", sec.ValidatorMiddleware, createAttachmentDirectly) + attachments.Post("/referenced", sec.ValidatorMiddleware, createAttachmentWithURL) attachments.Put("/:id", sec.ValidatorMiddleware, updateAttachmentMeta) attachments.Delete("/:id", sec.ValidatorMiddleware, deleteAttachment) } diff --git a/pkg/internal/server/api/sticker_packs_api.go b/pkg/internal/server/api/sticker_packs_api.go index 920f9f5..4b5b746 100644 --- a/pkg/internal/server/api/sticker_packs_api.go +++ b/pkg/internal/server/api/sticker_packs_api.go @@ -2,9 +2,9 @@ package api import ( "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/gap" - "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts" "git.solsynth.dev/hypernet/paperclip/pkg/internal/services" "git.solsynth.dev/hypernet/passport/pkg/authkit" diff --git a/pkg/internal/server/api/up_direct_api.go b/pkg/internal/server/api/up_direct_api.go index 6b21ea1..d88456f 100644 --- a/pkg/internal/server/api/up_direct_api.go +++ b/pkg/internal/server/api/up_direct_api.go @@ -2,10 +2,11 @@ package api import ( "fmt" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/services" "github.com/gofiber/fiber/v2" jsoniter "github.com/json-iterator/go" @@ -81,3 +82,50 @@ func createAttachmentDirectly(c *fiber.Ctx) error { return c.JSON(metadata) } + +func createAttachmentWithURL(c *fiber.Ctx) error { + user := c.Locals("nex_user").(*sec.UserInfo) + + poolAlias := c.FormValue("pool") + aliasingMap := viper.GetStringMapString("pools.aliases") + if val, ok := aliasingMap[poolAlias]; ok { + poolAlias = val + } + + var data struct { + URL string `json:"url"` + Metadata map[string]any `json:"metadata"` + Mimetype string `json:"mimetype"` + Name string `json:"filename"` + Alternative string `json:"alt"` + } + if err := exts.BindAndValidate(c, &data); err != nil { + return err + } + + if !user.HasPermNode("CreateReferencedAttachments", true) { + return fiber.NewError(fiber.StatusForbidden, "you are not permitted to create attachments with URL") + } + + pool, err := services.GetAttachmentPoolByAlias(poolAlias) + if err != nil { + return fiber.NewError(fiber.StatusBadRequest, fmt.Sprintf("unable to get attachment pool info: %v", err)) + } + + attachment := models.Attachment{ + Name: data.Name, + Alternative: data.Alternative, + MimeType: c.FormValue("mimetype"), + Usermeta: data.Metadata, + IsAnalyzed: true, + Destination: models.AttachmentDstExternal, + Pool: &pool, + PoolID: &pool.ID, + } + + if attachment, err = services.NewRefURLAttachment(database.C, user, attachment); err != nil { + return fiber.NewError(fiber.StatusBadRequest, err.Error()) + } + + return c.JSON(attachment) +} diff --git a/pkg/internal/server/api/up_multipart_api.go b/pkg/internal/server/api/up_multipart_api.go index 6201a06..45ba3d0 100644 --- a/pkg/internal/server/api/up_multipart_api.go +++ b/pkg/internal/server/api/up_multipart_api.go @@ -5,9 +5,9 @@ import ( "fmt" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" - "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/server/exts" "git.solsynth.dev/hypernet/paperclip/pkg/internal/services" "github.com/gofiber/fiber/v2" diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go index 3d46cac..369a8e2 100644 --- a/pkg/internal/services/attachments.go +++ b/pkg/internal/services/attachments.go @@ -158,6 +158,25 @@ func NewAttachmentMetadata(tx *gorm.DB, user *sec.UserInfo, file *multipart.File return attachment, nil } +func NewRefURLAttachment(tx *gorm.DB, user *sec.UserInfo, attachment models.Attachment) (models.Attachment, error) { + if attachment.RefURL == nil { + return attachment, fmt.Errorf("attachment doesn't have a ref url") + } + + attachment.Uuid = uuid.NewString() + attachment.Rid = RandString(16) + attachment.Size = 0 + attachment.Destination = models.AttachmentDstExternal + attachment.Type = models.AttachmentTypeNormal + attachment.AccountID = user.ID + + if err := tx.Save(&attachment).Error; err != nil { + return attachment, fmt.Errorf("failed to save attachment record: %v", err) + } + + return attachment, nil +} + func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, error) { prev, err := GetAttachmentByHash(hash) if err != nil { diff --git a/pkg/internal/services/boost.go b/pkg/internal/services/boost.go index b1618ac..ec2d1b2 100644 --- a/pkg/internal/services/boost.go +++ b/pkg/internal/services/boost.go @@ -5,9 +5,9 @@ import ( "fmt" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" - "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" jsoniter "github.com/json-iterator/go" "github.com/rs/zerolog/log" "github.com/spf13/cast" diff --git a/pkg/internal/services/cleaner.go b/pkg/internal/services/cleaner.go index 4086e4e..b52ea16 100644 --- a/pkg/internal/services/cleaner.go +++ b/pkg/internal/services/cleaner.go @@ -3,8 +3,8 @@ package services import ( "time" - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "github.com/rs/zerolog/log" ) diff --git a/pkg/internal/services/fragments.go b/pkg/internal/services/fragments.go index 34c616d..8926c90 100644 --- a/pkg/internal/services/fragments.go +++ b/pkg/internal/services/fragments.go @@ -12,9 +12,9 @@ import ( "git.solsynth.dev/hypernet/nexus/pkg/nex/cachekit" "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" + "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/gap" - "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "github.com/gofiber/fiber/v2" "github.com/google/uuid" jsoniter "github.com/json-iterator/go" diff --git a/pkg/internal/services/opener.go b/pkg/internal/services/opener.go index 04283fc..c3220b3 100644 --- a/pkg/internal/services/opener.go +++ b/pkg/internal/services/opener.go @@ -66,6 +66,12 @@ func OpenAttachmentByRID(rid string, preferredSize int, region ...string) (url s mimetype = result.Attachment.MimeType } + if result.Attachment.RefURL != nil { + url = *result.Attachment.RefURL + filesize = 0 + return + } + filesize = result.Attachment.Size var dest models.BaseDestination diff --git a/pkg/internal/services/payment.go b/pkg/internal/services/payment.go index 7436f88..70cec4b 100644 --- a/pkg/internal/services/payment.go +++ b/pkg/internal/services/payment.go @@ -5,8 +5,8 @@ import ( "fmt" "time" - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/gap" wproto "git.solsynth.dev/hypernet/wallet/pkg/proto" diff --git a/pkg/internal/services/pools.go b/pkg/internal/services/pools.go index 732cbf3..0492579 100644 --- a/pkg/internal/services/pools.go +++ b/pkg/internal/services/pools.go @@ -1,8 +1,8 @@ package services import ( - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" ) func ListAttachmentPool() ([]models.AttachmentPool, error) { diff --git a/pkg/internal/services/related.go b/pkg/internal/services/related.go index 9e6a4b5..4de331c 100644 --- a/pkg/internal/services/related.go +++ b/pkg/internal/services/related.go @@ -4,8 +4,8 @@ import ( "fmt" "strings" - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" ) func SetAttachmentAsThumbnail(item models.Attachment) (models.Attachment, error) { diff --git a/pkg/internal/services/sticker_packs.go b/pkg/internal/services/sticker_packs.go index 00c053e..96ee0ce 100644 --- a/pkg/internal/services/sticker_packs.go +++ b/pkg/internal/services/sticker_packs.go @@ -2,8 +2,8 @@ package services import ( "git.solsynth.dev/hypernet/nexus/pkg/nex/sec" - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "gorm.io/gorm" ) diff --git a/pkg/internal/services/stickers.go b/pkg/internal/services/stickers.go index 4a9b2c8..641e644 100644 --- a/pkg/internal/services/stickers.go +++ b/pkg/internal/services/stickers.go @@ -6,8 +6,8 @@ import ( "gorm.io/gorm" - "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" + "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "github.com/spf13/viper" ) diff --git a/pkg/internal/services/uploader.go b/pkg/internal/services/uploader.go index d57c950..d9cb7bb 100644 --- a/pkg/internal/services/uploader.go +++ b/pkg/internal/services/uploader.go @@ -8,9 +8,9 @@ import ( "os" "path/filepath" + "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "git.solsynth.dev/hypernet/paperclip/pkg/internal/database" "git.solsynth.dev/hypernet/paperclip/pkg/internal/fs" - "git.solsynth.dev/hypernet/paperclip/pkg/filekit/models" "github.com/gofiber/fiber/v2" jsoniter "github.com/json-iterator/go" "github.com/minio/minio-go/v7"