diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 116194d..4290628 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,11 @@
-
+
+
+
@@ -36,27 +38,28 @@
- {
- "keyToString": {
- "DefaultGoTemplateProperty": "Go File",
- "Go Build.Backend.executor": "Run",
- "Go 构建.Backend.executor": "Run",
- "RunOnceActivity.ShowReadmeOnStart": "true",
- "RunOnceActivity.go.formatter.settings.were.checked": "true",
- "RunOnceActivity.go.migrated.go.modules.settings": "true",
- "RunOnceActivity.go.modules.automatic.dependencies.download": "true",
- "RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
- "git-widget-placeholder": "master",
- "go.import.settings.migrated": "true",
- "go.sdk.automatically.set": "true",
- "last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Paperclip/pkg/internal/grpc",
- "node.js.detected.package.eslint": "true",
- "node.js.selected.package.eslint": "(autodetect)",
- "nodejs_package_manager_path": "npm",
- "run.code.analysis.last.selected.profile": "pProject Default",
- "settings.editor.selected.configurable": "preferences.lookFeel"
+
+}]]>
@@ -113,7 +116,8 @@
-
+
+
true
diff --git a/pkg/internal/server/api/attachments_api.go b/pkg/internal/server/api/attachments_api.go
index 228e626..a7d0557 100644
--- a/pkg/internal/server/api/attachments_api.go
+++ b/pkg/internal/server/api/attachments_api.go
@@ -159,11 +159,11 @@ func updateAttachmentMeta(c *fiber.Ctx) error {
attachment.Metadata = data.Metadata
attachment.IsMature = data.IsMature
- if err := database.C.Save(&attachment).Error; err != nil {
+ if attachment, err := services.UpdateAttachment(attachment); err != nil {
return fiber.NewError(fiber.StatusBadRequest, err.Error())
+ } else {
+ return c.JSON(attachment)
}
-
- return c.JSON(attachment)
}
func deleteAttachment(c *fiber.Ctx) error {
diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go
index a256c91..8a75f93 100644
--- a/pkg/internal/services/attachments.go
+++ b/pkg/internal/services/attachments.go
@@ -13,23 +13,27 @@ import (
"gorm.io/gorm"
)
+const metadataCacheLimit = 512
+
+var metadataCache = make(map[uint]models.Attachment)
+
func GetAttachmentByID(id uint) (models.Attachment, error) {
+ if val, ok := metadataCache[id]; ok {
+ return val, nil
+ }
+
var attachment models.Attachment
if err := database.C.Where(models.Attachment{
BaseModel: models.BaseModel{ID: id},
}).First(&attachment).Error; err != nil {
return attachment, err
+ } else {
+ if len(metadataCache) > metadataCacheLimit {
+ clear(metadataCache)
+ }
+ metadataCache[id] = attachment
}
- return attachment, nil
-}
-func GetAttachmentByUUID(id string) (models.Attachment, error) {
- var attachment models.Attachment
- if err := database.C.Where(models.Attachment{
- Uuid: id,
- }).First(&attachment).Error; err != nil {
- return attachment, err
- }
return attachment, nil
}
@@ -61,7 +65,7 @@ func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.Fil
attachment.Name = file.Filename
attachment.AccountID = user.ID
- // If user didn't provide file mimetype manually, we gotta to detect it
+ // If the user didn't provide file mimetype manually, we have to detect it
if len(attachment.MimeType) == 0 {
if ext := filepath.Ext(attachment.Name); len(ext) > 0 {
// Detect mimetype by file extensions
@@ -87,11 +91,29 @@ func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.Fil
if err := tx.Save(&attachment).Error; err != nil {
return attachment, linked, fmt.Errorf("failed to save attachment record: %v", err)
+ } else {
+ if len(metadataCache) > metadataCacheLimit {
+ clear(metadataCache)
+ }
+ metadataCache[attachment.ID] = attachment
}
return attachment, linked, nil
}
+func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
+ if err := database.C.Save(&item).Error; err != nil {
+ return item, err
+ } else {
+ if len(metadataCache) > metadataCacheLimit {
+ clear(metadataCache)
+ }
+ metadataCache[item.ID] = item
+ }
+
+ return item, nil
+}
+
func DeleteAttachment(item models.Attachment) error {
var dupeCount int64
if err := database.C.
@@ -103,6 +125,8 @@ func DeleteAttachment(item models.Attachment) error {
if err := database.C.Delete(&item).Error; err != nil {
return err
+ } else {
+ delete(metadataCache, item.ID)
}
if dupeCount != -1 && dupeCount <= 1 {
diff --git a/settings.toml b/settings.toml
index c1d532a..6e5a988 100644
--- a/settings.toml
+++ b/settings.toml
@@ -27,7 +27,7 @@ prefix = "paperclip_"
[destinations.local]
type = "local"
-path = "/uploads"
+path = "uploads"
[destinations.s3]
type = "s3"