✨ Independent mode
This commit is contained in:
parent
acd610464a
commit
9ceb9925dd
@ -17,6 +17,6 @@ type Attachment struct {
|
||||
Metadata datatypes.JSONMap `json:"metadata"`
|
||||
IsMature bool `json:"is_mature"`
|
||||
|
||||
Account Account `json:"account"`
|
||||
AccountID uint `json:"account_id"`
|
||||
Account *Account `json:"account"`
|
||||
AccountID *uint `json:"account_id"`
|
||||
}
|
||||
|
1
pkg/internal/server/api/attachment_dir_api.go
Normal file
1
pkg/internal/server/api/attachment_dir_api.go
Normal file
@ -0,0 +1 @@
|
||||
package api
|
@ -2,11 +2,12 @@ package api
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
|
||||
"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"
|
||||
"net/url"
|
||||
"path/filepath"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services"
|
||||
@ -72,10 +73,13 @@ func getAttachmentMeta(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
func createAttachment(c *fiber.Ctx) error {
|
||||
var user *models.Account
|
||||
if gap.H != nil {
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
user := c.Locals("user").(models.Account)
|
||||
user = lo.ToPtr(c.Locals("user").(models.Account))
|
||||
}
|
||||
|
||||
destName := c.Query("destination", viper.GetString("preferred_destination"))
|
||||
|
||||
@ -93,9 +97,11 @@ func createAttachment(c *fiber.Ctx) error {
|
||||
return err
|
||||
}
|
||||
|
||||
if gap.H != nil {
|
||||
if err := gap.H.EnsureGrantedPerm(c, "CreateAttachments", file.Size); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
usermeta := make(map[string]any)
|
||||
_ = jsoniter.UnmarshalFromString(c.FormValue("metadata"), &usermeta)
|
||||
@ -130,6 +136,10 @@ func createAttachment(c *fiber.Ctx) error {
|
||||
func updateAttachmentMeta(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("id", 0)
|
||||
|
||||
if gap.H == nil {
|
||||
return fiber.NewError(fiber.StatusUnprocessableEntity, "server running in independent mode, unable to modify attachment meta")
|
||||
}
|
||||
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -169,6 +179,10 @@ func updateAttachmentMeta(c *fiber.Ctx) error {
|
||||
func deleteAttachment(c *fiber.Ctx) error {
|
||||
id, _ := c.ParamsInt("id", 0)
|
||||
|
||||
if gap.H == nil {
|
||||
return fiber.NewError(fiber.StatusUnprocessableEntity, "server running in independent mode, unable to delete attachment")
|
||||
}
|
||||
|
||||
if err := gap.H.EnsureAuthenticated(c); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -177,7 +191,7 @@ func deleteAttachment(c *fiber.Ctx) error {
|
||||
attachment, err := services.GetAttachmentByID(uint(id))
|
||||
if err != nil {
|
||||
return fiber.NewError(fiber.StatusNotFound, err.Error())
|
||||
} else if attachment.AccountID != user.ID {
|
||||
} else if attachment.AccountID == nil || *attachment.AccountID != user.ID {
|
||||
return fiber.NewError(fiber.StatusNotFound, "record not created by you")
|
||||
}
|
||||
|
||||
|
@ -7,6 +7,7 @@ func MapAPIs(app *fiber.App, baseURL string) {
|
||||
|
||||
api := app.Group(baseURL).Name("API")
|
||||
{
|
||||
api.Get("/attachments", list)
|
||||
api.Get("/attachments/:id/meta", getAttachmentMeta)
|
||||
api.Get("/attachments/:id", openAttachment)
|
||||
api.Post("/attachments", createAttachment)
|
||||
|
@ -2,12 +2,13 @@ package services
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||
|
||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/models"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
@ -47,7 +48,7 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, bool, error) {
|
||||
func NewAttachmentMetadata(tx *gorm.DB, user *models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, bool, error) {
|
||||
linked := false
|
||||
exists, pickupErr := GetAttachmentByHash(attachment.HashCode)
|
||||
if pickupErr == nil {
|
||||
@ -57,13 +58,19 @@ func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.Fil
|
||||
exists.Metadata = attachment.Metadata
|
||||
attachment = exists
|
||||
attachment.ID = 0
|
||||
attachment.AccountID = user.ID
|
||||
|
||||
if user != nil {
|
||||
attachment.AccountID = &user.ID
|
||||
}
|
||||
} else {
|
||||
// Upload the new file
|
||||
attachment.Uuid = uuid.NewString()
|
||||
attachment.Size = file.Size
|
||||
attachment.Name = file.Filename
|
||||
attachment.AccountID = user.ID
|
||||
|
||||
if user != nil {
|
||||
attachment.AccountID = &user.ID
|
||||
}
|
||||
|
||||
// If the user didn't provide file mimetype manually, we have to detect it
|
||||
if len(attachment.MimeType) == 0 {
|
||||
|
@ -44,9 +44,13 @@ func main() {
|
||||
}
|
||||
|
||||
// Connect other services
|
||||
if !viper.GetBool("independent_mode") {
|
||||
if err := gap.RegisterService(); err != nil {
|
||||
log.Error().Err(err).Msg("An error occurred when registering service to dealer...")
|
||||
}
|
||||
} else {
|
||||
log.Warn().Msg("WATCHOUT! Running in independent mode, everyone with access to API can upload their file without authenticate!")
|
||||
}
|
||||
|
||||
// Configure timed tasks
|
||||
quartz := cron.New(cron.WithLogger(cron.VerbosePrintfLogger(&log.Logger)))
|
||||
|
@ -5,6 +5,8 @@ grpc_bind = "0.0.0.0:7443"
|
||||
domain = "usercontent.solsynth.dev"
|
||||
secret = "LtTjzAGFLshwXhN4ZD4nG5KlMv1MWcsvfv03TSZYnT1VhiAnLIZFTnHUwR0XhGgi"
|
||||
|
||||
independent_mode = false
|
||||
|
||||
preferred_destination = "local"
|
||||
accepts_usage = ["p.avatar", "p.banner", "i.attachment", "m.attachment"]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user