💥 Replace attachment id by rid when fetching

This commit is contained in:
LittleSheep 2024-08-18 21:10:10 +08:00
parent e92e5bad60
commit 165a6be985
4 changed files with 18 additions and 39 deletions

6
.idea/workspace.xml generated
View File

@ -4,8 +4,9 @@
<option name="autoReloadType" value="ALL" />
</component>
<component name="ChangeListManager">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":bug: Fix mark clean required issue">
<list default="true" id="18dd0d68-b4b8-40db-9734-9119b5c848bd" name="更改" comment=":sparkles: Support use rid to get file">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pkg/internal/server/api/attachment_dir_api.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/server/api/attachment_dir_api.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>
@ -135,7 +136,8 @@
<MESSAGE value=":bug: Fix doesn't get has lifecycle settings buckets correctly" />
<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" />
<MESSAGE value=":sparkles: Support use rid to get file" />
<option name="LAST_COMMIT_MESSAGE" value=":sparkles: Support use rid to get file" />
</component>
<component name="VgoProject">
<settings-migrated>true</settings-migrated>

View File

@ -1,7 +1,6 @@
package api
import (
"strconv"
"strings"
"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database"
@ -23,25 +22,20 @@ func listAttachment(c *fiber.Ctx) error {
needQuery := true
var result = make([]models.Attachment, take)
var idxList []uint
var idxList []string
if len(c.Query("id")) > 0 {
var pendingQueryId []uint
var pendingQueryId []string
idx := strings.Split(c.Query("id"), ",")
for p, raw := range idx {
id, err := strconv.Atoi(raw)
if err != nil {
continue
} else {
idxList = append(idxList, uint(id))
}
if val, ok := services.GetAttachmentCache(uint(id)); ok {
idxList = append(idxList, raw)
if val, ok := services.GetAttachmentCache(raw); ok {
result[p] = val
} else {
pendingQueryId = append(pendingQueryId, uint(id))
pendingQueryId = append(pendingQueryId, raw)
}
}
tx = tx.Where("id IN ?", pendingQueryId)
tx = tx.Where("rid IN ?", pendingQueryId)
needQuery = len(pendingQueryId) > 0
} else {
// Do sort this when doesn't filter by the id
@ -83,7 +77,7 @@ func listAttachment(c *fiber.Ctx) error {
} else {
for _, item := range out {
for p, id := range idxList {
if item.ID == id {
if item.Rid == id {
result[p] = item
}
}

View File

@ -2,13 +2,11 @@ package api
import (
"fmt"
"net/url"
"path/filepath"
"strconv"
"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"
@ -21,14 +19,7 @@ import (
func openAttachment(c *fiber.Ctx) error {
id := c.Params("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)
}
metadata, err := services.GetAttachmentByRID(id)
if err != nil {
return fiber.NewError(fiber.StatusNotFound)
}
@ -77,9 +68,9 @@ func openAttachment(c *fiber.Ctx) error {
}
func getAttachmentMeta(c *fiber.Ctx) error {
id, _ := c.ParamsInt("id")
id := c.Params("id")
metadata, err := services.GetAttachmentByID(uint(id))
metadata, err := services.GetAttachmentByRID(id)
if err != nil {
return fiber.NewError(fiber.StatusNotFound)
}

View File

@ -22,11 +22,6 @@ const metadataCacheLimit = 512
var metadataCache sync.Map
func GetAttachmentByID(id uint) (models.Attachment, error) {
strId := strconv.Itoa(int(id))
if val, ok := metadataCache.Load(strId); ok && val.(models.Attachment).Account.ID > 0 {
return val.(models.Attachment), nil
}
var attachment models.Attachment
if err := database.C.Where(models.Attachment{
BaseModel: models.BaseModel{ID: id},
@ -68,17 +63,14 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) {
return attachment, nil
}
func GetAttachmentCache(id uint) (models.Attachment, bool) {
strId := strconv.Itoa(int(id))
if val, ok := metadataCache.Load(strId); ok && val.(models.Attachment).Account.ID > 0 {
func GetAttachmentCache(id any) (models.Attachment, bool) {
if val, ok := metadataCache.Load(id); ok && val.(models.Attachment).Account.ID > 0 {
return val.(models.Attachment), ok
}
return models.Attachment{}, false
}
func CacheAttachment(item models.Attachment) {
strId := strconv.Itoa(int(item.ID))
metadataCache.Store(strId, item)
metadataCache.Store(item.Rid, item)
}