✨ Support use rid to get file
This commit is contained in:
parent
0fb8e27e7c
commit
e92e5bad60
@ -4,9 +4,10 @@
|
|||||||
<option name="autoReloadType" value="ALL" />
|
<option name="autoReloadType" value="ALL" />
|
||||||
</component>
|
</component>
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":zap: Fix the RandString method cause the lag">
|
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":bug: Fix mark clean required issue">
|
||||||
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
|
||||||
<change beforePath="$PROJECT_DIR$/pkg/internal/services/recycler.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/recycler.go" afterDir="false" />
|
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/attachments_api.go" afterDir="false" />
|
||||||
|
<change beforePath="$PROJECT_DIR$/pkg/internal/services/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/attachments.go" afterDir="false" />
|
||||||
</list>
|
</list>
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
@ -36,33 +37,33 @@
|
|||||||
<option name="hideEmptyMiddlePackages" value="true" />
|
<option name="hideEmptyMiddlePackages" value="true" />
|
||||||
<option name="showLibraryContents" value="true" />
|
<option name="showLibraryContents" value="true" />
|
||||||
</component>
|
</component>
|
||||||
<component name="PropertiesComponent"><![CDATA[{
|
<component name="PropertiesComponent">{
|
||||||
"keyToString": {
|
"keyToString": {
|
||||||
"DefaultGoTemplateProperty": "Go File",
|
"DefaultGoTemplateProperty": "Go File",
|
||||||
"Go Build.Backend.executor": "Run",
|
"Go Build.Backend.executor": "Run",
|
||||||
"Go 构建.Backend.executor": "Run",
|
"Go 构建.Backend.executor": "Run",
|
||||||
"RunOnceActivity.ShowReadmeOnStart": "true",
|
"RunOnceActivity.ShowReadmeOnStart": "true",
|
||||||
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
"RunOnceActivity.go.formatter.settings.were.checked": "true",
|
||||||
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
"RunOnceActivity.go.migrated.go.modules.settings": "true",
|
||||||
"RunOnceActivity.go.modules.automatic.dependencies.download": "true",
|
"RunOnceActivity.go.modules.automatic.dependencies.download": "true",
|
||||||
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
"RunOnceActivity.go.modules.go.list.on.any.changes.was.set": "true",
|
||||||
"git-widget-placeholder": "master",
|
"git-widget-placeholder": "master",
|
||||||
"go.import.settings.migrated": "true",
|
"go.import.settings.migrated": "true",
|
||||||
"go.sdk.automatically.set": "true",
|
"go.sdk.automatically.set": "true",
|
||||||
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Paperclip/pkg/internal/grpc",
|
"last_opened_file_path": "/Users/littlesheep/Documents/Projects/Hydrogen/Paperclip/pkg/internal/grpc",
|
||||||
"node.js.detected.package.eslint": "true",
|
"node.js.detected.package.eslint": "true",
|
||||||
"node.js.selected.package.eslint": "(autodetect)",
|
"node.js.selected.package.eslint": "(autodetect)",
|
||||||
"nodejs_package_manager_path": "npm",
|
"nodejs_package_manager_path": "npm",
|
||||||
"run.code.analysis.last.selected.profile": "pProject Default",
|
"run.code.analysis.last.selected.profile": "pProject Default",
|
||||||
"settings.editor.selected.configurable": "preferences.pluginManager",
|
"settings.editor.selected.configurable": "preferences.pluginManager",
|
||||||
"vue.rearranger.settings.migration": "true"
|
"vue.rearranger.settings.migration": "true"
|
||||||
},
|
},
|
||||||
"keyToStringList": {
|
"keyToStringList": {
|
||||||
"DatabaseDriversLRU": [
|
"DatabaseDriversLRU": [
|
||||||
"postgresql"
|
"postgresql"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}]]></component>
|
}</component>
|
||||||
<component name="RecentsManager">
|
<component name="RecentsManager">
|
||||||
<key name="CopyFile.RECENT_KEYS">
|
<key name="CopyFile.RECENT_KEYS">
|
||||||
<recent name="$PROJECT_DIR$/pkg/internal/grpc" />
|
<recent name="$PROJECT_DIR$/pkg/internal/grpc" />
|
||||||
@ -133,7 +134,8 @@
|
|||||||
<MESSAGE value=":bug: Fix sql statement" />
|
<MESSAGE value=":bug: Fix sql statement" />
|
||||||
<MESSAGE value=":bug: Fix doesn't get has lifecycle settings buckets correctly" />
|
<MESSAGE value=":bug: Fix doesn't get has lifecycle settings buckets correctly" />
|
||||||
<MESSAGE value=":zap: Fix the RandString method cause the lag" />
|
<MESSAGE value=":zap: Fix the RandString method cause the lag" />
|
||||||
<option name="LAST_COMMIT_MESSAGE" value=":zap: Fix the RandString method cause the lag" />
|
<MESSAGE value=":bug: Fix mark clean required issue" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value=":bug: Fix mark clean required issue" />
|
||||||
</component>
|
</component>
|
||||||
<component name="VgoProject">
|
<component name="VgoProject">
|
||||||
<settings-migrated>true</settings-migrated>
|
<settings-migrated>true</settings-migrated>
|
||||||
|
@ -92,7 +92,7 @@ func listAttachment(c *fiber.Ctx) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, item := range result {
|
for _, item := range result {
|
||||||
services.CacheAttachment(item.ID, item)
|
services.CacheAttachment(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.JSON(fiber.Map{
|
return c.JSON(fiber.Map{
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/gap"
|
||||||
@ -18,9 +19,16 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func openAttachment(c *fiber.Ctx) error {
|
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 {
|
if err != nil {
|
||||||
return fiber.NewError(fiber.StatusNotFound)
|
return fiber.NewError(fiber.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
|
||||||
@ -21,7 +22,8 @@ const metadataCacheLimit = 512
|
|||||||
var metadataCache sync.Map
|
var metadataCache sync.Map
|
||||||
|
|
||||||
func GetAttachmentByID(id uint) (models.Attachment, error) {
|
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
|
return val.(models.Attachment), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -32,7 +34,25 @@ func GetAttachmentByID(id uint) (models.Attachment, error) {
|
|||||||
return attachment, err
|
return attachment, err
|
||||||
} else {
|
} else {
|
||||||
MaintainAttachmentCache()
|
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
|
return attachment, nil
|
||||||
@ -49,14 +69,17 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func GetAttachmentCache(id uint) (models.Attachment, bool) {
|
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 val.(models.Attachment), ok
|
||||||
}
|
}
|
||||||
return models.Attachment{}, false
|
return models.Attachment{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
func CacheAttachment(id uint, item models.Attachment) {
|
func CacheAttachment(item models.Attachment) {
|
||||||
metadataCache.Store(id, item)
|
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) {
|
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)
|
return attachment, fmt.Errorf("failed to save attachment record: %v", err)
|
||||||
} else {
|
} else {
|
||||||
MaintainAttachmentCache()
|
MaintainAttachmentCache()
|
||||||
metadataCache.Store(attachment.ID, attachment)
|
CacheAttachment(attachment)
|
||||||
}
|
}
|
||||||
|
|
||||||
return attachment, nil
|
return attachment, nil
|
||||||
@ -129,8 +152,8 @@ func TryLinkAttachment(tx *gorm.DB, og models.Attachment, hash string) (bool, er
|
|||||||
return true, err
|
return true, err
|
||||||
}
|
}
|
||||||
|
|
||||||
metadataCache.Store(prev.ID, prev)
|
CacheAttachment(prev)
|
||||||
metadataCache.Store(og.ID, og)
|
CacheAttachment(og)
|
||||||
|
|
||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
@ -140,7 +163,7 @@ func UpdateAttachment(item models.Attachment) (models.Attachment, error) {
|
|||||||
return item, err
|
return item, err
|
||||||
} else {
|
} else {
|
||||||
MaintainAttachmentCache()
|
MaintainAttachmentCache()
|
||||||
metadataCache.Store(item.ID, item)
|
CacheAttachment(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
return item, nil
|
return item, nil
|
||||||
@ -167,7 +190,9 @@ func DeleteAttachment(item models.Attachment) error {
|
|||||||
tx.Rollback()
|
tx.Rollback()
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
metadataCache.Delete(item.ID)
|
strId := strconv.Itoa(int(item.ID))
|
||||||
|
metadataCache.Delete(strId)
|
||||||
|
metadataCache.Delete(item.Rid)
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
|
Loading…
Reference in New Issue
Block a user