feat(files): implement file storage functionality with save, load, and list operations
This commit is contained in:
@@ -35,6 +35,7 @@ func Fallback(capture config.CaptureConfig) thoughttypes.ThoughtMetadata {
|
||||
Topics: []string{topicFallback},
|
||||
Type: normalizeType(capture.MetadataDefaults.Type),
|
||||
Source: normalizeSource(capture.Source),
|
||||
Attachments: []thoughttypes.ThoughtAttachment{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,6 +47,7 @@ func Normalize(in thoughttypes.ThoughtMetadata, capture config.CaptureConfig) th
|
||||
Topics: normalizeList(in.Topics, maxTopics),
|
||||
Type: normalizeType(in.Type),
|
||||
Source: normalizeSource(in.Source),
|
||||
Attachments: normalizeAttachments(in.Attachments),
|
||||
}
|
||||
|
||||
if len(out.Topics) == 0 {
|
||||
@@ -127,10 +129,42 @@ func Merge(base, patch thoughttypes.ThoughtMetadata, capture config.CaptureConfi
|
||||
if strings.TrimSpace(patch.Source) != "" {
|
||||
merged.Source = patch.Source
|
||||
}
|
||||
if len(patch.Attachments) > 0 {
|
||||
merged.Attachments = append(append([]thoughttypes.ThoughtAttachment{}, merged.Attachments...), patch.Attachments...)
|
||||
}
|
||||
|
||||
return Normalize(merged, capture)
|
||||
}
|
||||
|
||||
func normalizeAttachments(values []thoughttypes.ThoughtAttachment) []thoughttypes.ThoughtAttachment {
|
||||
seen := make(map[string]struct{}, len(values))
|
||||
result := make([]thoughttypes.ThoughtAttachment, 0, len(values))
|
||||
|
||||
for _, value := range values {
|
||||
if value.FileID.String() == "" || value.FileID.String() == "00000000-0000-0000-0000-000000000000" {
|
||||
continue
|
||||
}
|
||||
|
||||
key := value.FileID.String()
|
||||
if _, ok := seen[key]; ok {
|
||||
continue
|
||||
}
|
||||
|
||||
value.Name = strings.TrimSpace(value.Name)
|
||||
value.MediaType = strings.TrimSpace(value.MediaType)
|
||||
value.Kind = strings.TrimSpace(value.Kind)
|
||||
if value.SizeBytes < 0 {
|
||||
value.SizeBytes = 0
|
||||
}
|
||||
value.SHA256 = strings.TrimSpace(value.SHA256)
|
||||
|
||||
seen[key] = struct{}{}
|
||||
result = append(result, value)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func SortedTopCounts(in map[string]int, limit int) []thoughttypes.KeyCount {
|
||||
out := make([]thoughttypes.KeyCount, 0, len(in))
|
||||
for key, count := range in {
|
||||
|
||||
@@ -4,6 +4,8 @@ import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"git.warky.dev/wdevs/amcs/internal/config"
|
||||
thoughttypes "git.warky.dev/wdevs/amcs/internal/types"
|
||||
)
|
||||
@@ -79,3 +81,24 @@ func TestMergeAddsPatchAndNormalizes(t *testing.T) {
|
||||
t.Fatalf("Topics len = %d, want 2", len(got.Topics))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeDedupesAttachmentsByFileID(t *testing.T) {
|
||||
id := uuid.New()
|
||||
|
||||
got := Normalize(thoughttypes.ThoughtMetadata{
|
||||
Attachments: []thoughttypes.ThoughtAttachment{
|
||||
{FileID: id, Name: " one.png ", MediaType: " image/png ", Kind: " image ", SizeBytes: 12, SHA256: " abc "},
|
||||
{FileID: id, Name: "two.png", MediaType: "image/png", Kind: "image", SizeBytes: 99, SHA256: "def"},
|
||||
},
|
||||
}, testCaptureConfig())
|
||||
|
||||
if len(got.Attachments) != 1 {
|
||||
t.Fatalf("Attachments len = %d, want 1", len(got.Attachments))
|
||||
}
|
||||
if got.Attachments[0].Name != "one.png" {
|
||||
t.Fatalf("Attachment name = %q, want one.png", got.Attachments[0].Name)
|
||||
}
|
||||
if got.Attachments[0].Kind != "image" {
|
||||
t.Fatalf("Attachment kind = %q, want image", got.Attachments[0].Kind)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user