diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 5271071..541b3fa 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,9 +4,10 @@
-
+
-
+
+
@@ -36,33 +37,33 @@
- {
+ "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.pluginManager",
+ "vue.rearranger.settings.migration": "true"
},
- "keyToStringList": {
- "DatabaseDriversLRU": [
- "postgresql"
+ "keyToStringList": {
+ "DatabaseDriversLRU": [
+ "postgresql"
]
}
-}]]>
+}
@@ -133,7 +134,8 @@
-
+
+
true
diff --git a/pkg/internal/server/api/attachment_dir_api.go b/pkg/internal/server/api/attachment_dir_api.go
index b689927..8a4d505 100644
--- a/pkg/internal/server/api/attachment_dir_api.go
+++ b/pkg/internal/server/api/attachment_dir_api.go
@@ -92,7 +92,7 @@ func listAttachment(c *fiber.Ctx) error {
}
for _, item := range result {
- services.CacheAttachment(item.ID, item)
+ services.CacheAttachment(item)
}
return c.JSON(fiber.Map{
diff --git a/pkg/internal/server/api/attachments_api.go b/pkg/internal/server/api/attachments_api.go
index 1f408bb..aa64f0f 100644
--- a/pkg/internal/server/api/attachments_api.go
+++ b/pkg/internal/server/api/attachments_api.go
@@ -4,6 +4,7 @@ import (
"fmt"
"net/url"
"path/filepath"
+ "strconv"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
@@ -18,9 +19,16 @@ import (
)
func openAttachment(c *fiber.Ctx) error {
- id, _ := c.ParamsInt("id", 0)
+ id := c.Params("id")
- metadata, err := services.GetAttachmentByID(uint(id))
+ var err error
+ var metadata models.Attachment
+
+ if numericId, numericErr := strconv.Atoi(id); numericErr == nil {
+ metadata, err = services.GetAttachmentByID(uint(numericId))
+ } else {
+ metadata, err = services.GetAttachmentByRID(id)
+ }
if err != nil {
return fiber.NewError(fiber.StatusNotFound)
}
diff --git a/pkg/internal/services/attachments.go b/pkg/internal/services/attachments.go
index 0415a68..b802554 100644
--- a/pkg/internal/services/attachments.go
+++ b/pkg/internal/services/attachments.go
@@ -6,6 +6,7 @@ import (
"mime/multipart"
"net/http"
"path/filepath"
+ "strconv"
"sync"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
@@ -21,7 +22,8 @@ const metadataCacheLimit = 512
var metadataCache sync.Map
func GetAttachmentByID(id uint) (models.Attachment, error) {
- if val, ok := metadataCache.Load(id); ok && val.(models.Attachment).Account.ID > 0 {
+ strId := strconv.Itoa(int(id))
+ if val, ok := metadataCache.Load(strId); ok && val.(models.Attachment).Account.ID > 0 {
return val.(models.Attachment), nil
}
@@ -32,7 +34,25 @@ func GetAttachmentByID(id uint) (models.Attachment, error) {
return attachment, err
} else {
MaintainAttachmentCache()
- metadataCache.Store(id, attachment)
+ CacheAttachment(attachment)
+ }
+
+ return attachment, nil
+}
+
+func GetAttachmentByRID(rid string) (models.Attachment, error) {
+ if val, ok := metadataCache.Load(rid); ok && val.(models.Attachment).Account.ID > 0 {
+ return val.(models.Attachment), nil
+ }
+
+ var attachment models.Attachment
+ if err := database.C.Where(models.Attachment{
+ Rid: rid,
+ }).Preload("Pool").Preload("Account").First(&attachment).Error; err != nil {
+ return attachment, err
+ } else {
+ MaintainAttachmentCache()
+ CacheAttachment(attachment)
}
return attachment, nil
@@ -49,14 +69,17 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
}
func GetAttachmentCache(id uint) (models.Attachment, bool) {
- if val, ok := metadataCache.Load(id); ok && val.(models.Attachment).Account.ID > 0 {
+ strId := strconv.Itoa(int(id))
+ if val, ok := metadataCache.Load(strId); ok && val.(models.Attachment).Account.ID > 0 {
return val.(models.Attachment), ok
}
return models.Attachment{}, false
}
-func CacheAttachment(id uint, item models.Attachment) {
- metadataCache.Store(id, item)
+func CacheAttachment(item models.Attachment) {
+ strId := strconv.Itoa(int(item.ID))
+ metadataCache.Store(strId, item)
+ metadataCache.Store(item.Rid, item)
}
func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.FileHeader, attachment models.Attachment) (models.Attachment, error) {
@@ -93,7 +116,7 @@ func NewAttachmentMetadata(tx *gorm.DB, user models.Account, file *multipart.Fil
return attachment, fmt.Errorf("failed to save attachment record: %v", err)
} else {
MaintainAttachmentCache()
- metadataCache.Store(attachment.ID, attachment)
+ CacheAttachment(attachment)
}
return attachment, nil
@@ -129,8 +152,8 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er
return true, err
}
- metadataCache.Store(prev.ID, prev)
- metadataCache.Store(og.ID, og)
+ CacheAttachment(prev)
+ CacheAttachment(og)
return true, nil
}
@@ -140,7 +163,7 @@ func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
return item, err
} else {
MaintainAttachmentCache()
- metadataCache.Store(item.ID, item)
+ CacheAttachment(item)
}
return item, nil
@@ -167,7 +190,9 @@ func DeleteAttachment(item models.Attachment) error {
tx.Rollback()
return err
} else {
- metadataCache.Delete(item.ID)
+ strId := strconv.Itoa(int(item.ID))
+ metadataCache.Delete(strId)
+ metadataCache.Delete(item.Rid)
}
tx.Commit()