Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/handlers/thread.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ type threadMessagesResponse struct {
Messages []threadMessage `json:"messages"`
}

func partCacheKey(messageHash, kind string, position int) string {
func partCacheKey(messageHash, kind string, position int, filename string) string {
if messageHash == "" {
return ""
}
if kind == "body" {
return "sha256:" + messageHash + ":body"
}
return "sha256:" + messageHash + ":attachment:" + strconv.Itoa(position)
return "sha256:" + messageHash + ":attachment:" + strconv.Itoa(position) + ":" + url.PathEscape(filename)
}

func threadDownloadPath(id int64, filename string) string {
Expand All @@ -96,7 +96,7 @@ func populateThreadBodies(messages []threadMessage, dataDir string, maxTextBytes
if !m.Visible {
continue
}
key := partCacheKey(m.MessageSHA256, "body", 0)
key := partCacheKey(m.MessageSHA256, "body", 0, "")
m.Body = &threadBody{Type: m.Type, Size: m.Size, Download: threadDownloadPath(m.ID, ""), CacheKey: key, Cacheable: key != ""}
if !isTextType(m.Type) {
continue
Expand Down Expand Up @@ -203,7 +203,7 @@ func loadThreadRelations(ctx context.Context, tx pgx.Tx, messages []threadMessag
}
m := byID[msgID]
a.Download = threadDownloadPath(msgID, a.Filename)
a.CacheKey = partCacheKey(m.MessageSHA256, "attachment", a.Position)
a.CacheKey = partCacheKey(m.MessageSHA256, "attachment", a.Position, a.Filename)
a.Cacheable = a.CacheKey != ""
m.Attachments = append(m.Attachments, a)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/handlers/thread_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,10 @@ func TestThreadMessagesJSONDoesNotExposeInternalPath(t *testing.T) {
}

func TestThreadAttachmentCacheKey(t *testing.T) {
if got := partCacheKey("deadbeef", "attachment", 4); got != "sha256:deadbeef:attachment:4" {
if got := partCacheKey("deadbeef", "attachment", 4, "report final.pdf"); got != "sha256:deadbeef:attachment:4:report%20final.pdf" {
t.Fatalf("got %q", got)
}
if got := partCacheKey("", "attachment", 4); got != "" {
if got := partCacheKey("", "attachment", 4, "report.pdf"); got != "" {
t.Fatalf("hashless message must not be cacheable: %q", got)
}
}
Loading