💥 Replace attachment id by rid when fetching
This commit is contained in:
		
							
								
								
									
										6
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										6
									
								
								.idea/workspace.xml
									
									
									
										generated
									
									
									
								
							| @@ -4,8 +4,9 @@ | |||||||
|     <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=":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$/.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/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" /> |       <change beforePath="$PROJECT_DIR$/pkg/internal/services/attachments.go" beforeDir="false" afterPath="$PROJECT_DIR$/pkg/internal/services/attachments.go" afterDir="false" /> | ||||||
|     </list> |     </list> | ||||||
| @@ -135,7 +136,8 @@ | |||||||
|     <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" /> | ||||||
|     <MESSAGE value=":bug: Fix mark clean required issue" /> |     <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> | ||||||
|   <component name="VgoProject"> |   <component name="VgoProject"> | ||||||
|     <settings-migrated>true</settings-migrated> |     <settings-migrated>true</settings-migrated> | ||||||
|   | |||||||
| @@ -1,7 +1,6 @@ | |||||||
| package api | package api | ||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"strconv" |  | ||||||
| 	"strings" | 	"strings" | ||||||
|  |  | ||||||
| 	"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" | 	"git.solsynth.dev/hydrogen/paperclip/pkg/internal/database" | ||||||
| @@ -23,25 +22,20 @@ func listAttachment(c *fiber.Ctx) error { | |||||||
| 	needQuery := true | 	needQuery := true | ||||||
|  |  | ||||||
| 	var result = make([]models.Attachment, take) | 	var result = make([]models.Attachment, take) | ||||||
| 	var idxList []uint | 	var idxList []string | ||||||
|  |  | ||||||
| 	if len(c.Query("id")) > 0 { | 	if len(c.Query("id")) > 0 { | ||||||
| 		var pendingQueryId []uint | 		var pendingQueryId []string | ||||||
| 		idx := strings.Split(c.Query("id"), ",") | 		idx := strings.Split(c.Query("id"), ",") | ||||||
| 		for p, raw := range idx { | 		for p, raw := range idx { | ||||||
| 			id, err := strconv.Atoi(raw) | 			idxList = append(idxList, raw) | ||||||
| 			if err != nil { | 			if val, ok := services.GetAttachmentCache(raw); ok { | ||||||
| 				continue |  | ||||||
| 			} else { |  | ||||||
| 				idxList = append(idxList, uint(id)) |  | ||||||
| 			} |  | ||||||
| 			if val, ok := services.GetAttachmentCache(uint(id)); ok { |  | ||||||
| 				result[p] = val | 				result[p] = val | ||||||
| 			} else { | 			} 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 | 		needQuery = len(pendingQueryId) > 0 | ||||||
| 	} else { | 	} else { | ||||||
| 		// Do sort this when doesn't filter by the id | 		// Do sort this when doesn't filter by the id | ||||||
| @@ -83,7 +77,7 @@ func listAttachment(c *fiber.Ctx) error { | |||||||
| 		} else { | 		} else { | ||||||
| 			for _, item := range out { | 			for _, item := range out { | ||||||
| 				for p, id := range idxList { | 				for p, id := range idxList { | ||||||
| 					if item.ID == id { | 					if item.Rid == id { | ||||||
| 						result[p] = item | 						result[p] = item | ||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
|   | |||||||
| @@ -2,13 +2,11 @@ package api | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"net/url" |  | ||||||
| 	"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" | ||||||
| 	"git.solsynth.dev/hydrogen/paperclip/pkg/internal/server/exts" | 	"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/models" | ||||||
| 	"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services" | 	"git.solsynth.dev/hydrogen/paperclip/pkg/internal/services" | ||||||
| @@ -21,14 +19,7 @@ import ( | |||||||
| func openAttachment(c *fiber.Ctx) error { | func openAttachment(c *fiber.Ctx) error { | ||||||
| 	id := c.Params("id") | 	id := c.Params("id") | ||||||
|  |  | ||||||
| 	var err error | 	metadata, err := services.GetAttachmentByRID(id) | ||||||
| 	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) | ||||||
| 	} | 	} | ||||||
| @@ -77,9 +68,9 @@ func openAttachment(c *fiber.Ctx) error { | |||||||
| } | } | ||||||
|  |  | ||||||
| func getAttachmentMeta(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 { | 	if err != nil { | ||||||
| 		return fiber.NewError(fiber.StatusNotFound) | 		return fiber.NewError(fiber.StatusNotFound) | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -22,11 +22,6 @@ 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) { | ||||||
| 	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 | 	var attachment models.Attachment | ||||||
| 	if err := database.C.Where(models.Attachment{ | 	if err := database.C.Where(models.Attachment{ | ||||||
| 		BaseModel: models.BaseModel{ID: id}, | 		BaseModel: models.BaseModel{ID: id}, | ||||||
| @@ -68,17 +63,14 @@ func GetAttachmentByHash(hash string) (models.Attachment, error) { | |||||||
| 	return attachment, nil | 	return attachment, nil | ||||||
| } | } | ||||||
|  |  | ||||||
| func GetAttachmentCache(id uint) (models.Attachment, bool) { | func GetAttachmentCache(id any) (models.Attachment, bool) { | ||||||
| 	strId := strconv.Itoa(int(id)) | 	if val, ok := metadataCache.Load(id); ok && val.(models.Attachment).Account.ID > 0 { | ||||||
| 	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(item models.Attachment) { | func CacheAttachment(item models.Attachment) { | ||||||
| 	strId := strconv.Itoa(int(item.ID)) |  | ||||||
| 	metadataCache.Store(strId, item) |  | ||||||
| 	metadataCache.Store(item.Rid, item) | 	metadataCache.Store(item.Rid, item) | ||||||
| } | } | ||||||
|  |  | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user