fix: correct duplicate message reporting in Go implementation
- Add DocumentSkippedError custom error type to distinguish between skipped and stored documents - Fix counter bug where skipped messages were incorrectly reported as "stored" - Enhance status reporting to show "X skipped as duplicates" for better visibility - Fix Rust implementation binary attachment handling to support all file types (images, PDFs, etc.) - Update test scripts to use correct binary names (mail2couch-go, mail2couch-rs) - Add comprehensive test configurations for implementation comparison Before: "Summary: Processed 30 messages, stored 30 new messages" (misleading when all were duplicates) After: "Summary: Processed 30 messages, stored 0 new messages" with detailed "Stored 0/30 messages from INBOX (30 skipped as duplicates)" 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
e6ab28bc9e
commit
436276f0ef
9 changed files with 127 additions and 18 deletions
|
|
@ -15,6 +15,15 @@ import (
|
||||||
"mail2couch/mail"
|
"mail2couch/mail"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// DocumentSkippedError indicates that a document was skipped because it already exists
|
||||||
|
type DocumentSkippedError struct {
|
||||||
|
DocumentID string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e *DocumentSkippedError) Error() string {
|
||||||
|
return fmt.Sprintf("document %s already exists, skipped", e.DocumentID)
|
||||||
|
}
|
||||||
|
|
||||||
// Client wraps the Kivik client
|
// Client wraps the Kivik client
|
||||||
type Client struct {
|
type Client struct {
|
||||||
*kivik.Client
|
*kivik.Client
|
||||||
|
|
@ -149,7 +158,7 @@ func (c *Client) StoreMessage(ctx context.Context, dbName string, doc *MailDocum
|
||||||
}
|
}
|
||||||
|
|
||||||
if exists {
|
if exists {
|
||||||
return nil // Document already exists, skip
|
return &DocumentSkippedError{DocumentID: doc.ID}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store the document first (without attachments)
|
// Store the document first (without attachments)
|
||||||
|
|
|
||||||
BIN
go/mail2couch-go
BIN
go/mail2couch-go
Binary file not shown.
11
go/main.go
11
go/main.go
|
|
@ -2,6 +2,7 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -174,16 +175,26 @@ func processImapSource(source *config.MailSource, couchClient *couch.Client, dbN
|
||||||
stored := 0
|
stored := 0
|
||||||
if !dryRun {
|
if !dryRun {
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||||
|
var skipped int
|
||||||
for i, doc := range docs {
|
for i, doc := range docs {
|
||||||
err := couchClient.StoreMessage(ctx, dbName, doc, messages[i])
|
err := couchClient.StoreMessage(ctx, dbName, doc, messages[i])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
var skipErr *couch.DocumentSkippedError
|
||||||
|
if errors.As(err, &skipErr) {
|
||||||
|
skipped++
|
||||||
|
} else {
|
||||||
log.Printf(" ERROR: Failed to store message %s: %v", doc.ID, err)
|
log.Printf(" ERROR: Failed to store message %s: %v", doc.ID, err)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
stored++
|
stored++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cancel()
|
cancel()
|
||||||
|
if skipped > 0 {
|
||||||
|
fmt.Printf(" Stored %d/%d messages from %s (%d skipped as duplicates)\n", stored, len(messages), mailbox, skipped)
|
||||||
|
} else {
|
||||||
fmt.Printf(" Stored %d/%d messages from %s\n", stored, len(messages), mailbox)
|
fmt.Printf(" Stored %d/%d messages from %s\n", stored, len(messages), mailbox)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
stored = len(messages) // In dry-run, assume all would be stored
|
stored = len(messages) // In dry-run, assume all would be stored
|
||||||
fmt.Printf(" DRY-RUN: Would store %d messages from %s\n", len(messages), mailbox)
|
fmt.Printf(" DRY-RUN: Would store %d messages from %s\n", len(messages), mailbox)
|
||||||
|
|
|
||||||
|
|
@ -705,21 +705,14 @@ impl ImapClient {
|
||||||
// Generate a filename for the attachment
|
// Generate a filename for the attachment
|
||||||
let filename = self.get_attachment_filename(part, index);
|
let filename = self.get_attachment_filename(part, index);
|
||||||
|
|
||||||
// Get the content data (try different methods based on content type)
|
// Get the binary content data using the proper mail-parser API
|
||||||
let body_data = if let Some(text_content) = part.get_text_contents() {
|
// This works for both text and binary attachments (images, PDFs, etc.)
|
||||||
// Text-based attachments
|
let body_data = part.get_contents().to_vec();
|
||||||
log::debug!("Found text attachment content: {} bytes", text_content.len());
|
log::debug!("Found attachment content: {} bytes (content-type: {})", body_data.len(), content_type.c_type);
|
||||||
text_content.as_bytes().to_vec()
|
|
||||||
} else {
|
|
||||||
// For now, skip attachments without text content
|
|
||||||
// TODO: Implement binary attachment support with proper mail-parser API
|
|
||||||
log::debug!("Skipping non-text attachment for part {} (content-type: {})", index, content_type.c_type);
|
|
||||||
vec![]
|
|
||||||
};
|
|
||||||
|
|
||||||
let content_type_str = content_type.c_type.to_string();
|
let content_type_str = content_type.c_type.to_string();
|
||||||
|
|
||||||
// Only create attachment stub if we have actual data
|
// Create attachment stub - get_contents() always returns the full data
|
||||||
if !body_data.is_empty() {
|
if !body_data.is_empty() {
|
||||||
let attachment_stub = AttachmentStub {
|
let attachment_stub = AttachmentStub {
|
||||||
content_type: content_type_str.clone(),
|
content_type: content_type_str.clone(),
|
||||||
|
|
|
||||||
|
|
@ -36,7 +36,7 @@ build_implementations() {
|
||||||
# Build Go implementation
|
# Build Go implementation
|
||||||
echo -e "${BLUE} Building Go implementation...${NC}"
|
echo -e "${BLUE} Building Go implementation...${NC}"
|
||||||
cd go
|
cd go
|
||||||
go build -o mail2couch .
|
go build -o mail2couch-go .
|
||||||
cd ..
|
cd ..
|
||||||
echo -e "${GREEN} ✅ Go implementation built${NC}"
|
echo -e "${GREEN} ✅ Go implementation built${NC}"
|
||||||
|
|
||||||
|
|
@ -53,7 +53,7 @@ run_go() {
|
||||||
echo -e "${BLUE}🦬 Running Go implementation...${NC}"
|
echo -e "${BLUE}🦬 Running Go implementation...${NC}"
|
||||||
cd go
|
cd go
|
||||||
echo -e "${BLUE} Using config: config-test-go.json${NC}"
|
echo -e "${BLUE} Using config: config-test-go.json${NC}"
|
||||||
./mail2couch -c config-test-go.json
|
./mail2couch-go -c config-test-go.json
|
||||||
cd ..
|
cd ..
|
||||||
echo -e "${GREEN}✅ Go implementation completed${NC}"
|
echo -e "${GREEN}✅ Go implementation completed${NC}"
|
||||||
}
|
}
|
||||||
|
|
@ -63,7 +63,7 @@ run_rust() {
|
||||||
echo -e "${BLUE}🦀 Running Rust implementation...${NC}"
|
echo -e "${BLUE}🦀 Running Rust implementation...${NC}"
|
||||||
cd rust
|
cd rust
|
||||||
echo -e "${BLUE} Using config: config-test-rust.json${NC}"
|
echo -e "${BLUE} Using config: config-test-rust.json${NC}"
|
||||||
./target/release/mail2couch -c config-test-rust.json
|
./target/release/mail2couch-rs -c config-test-rust.json
|
||||||
cd ..
|
cd ..
|
||||||
echo -e "${GREEN}✅ Rust implementation completed${NC}"
|
echo -e "${GREEN}✅ Rust implementation completed${NC}"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
24
test-config-go.json
Normal file
24
test-config-go.json
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"couchDb": {
|
||||||
|
"url": "http://localhost:5984",
|
||||||
|
"user": "admin",
|
||||||
|
"password": "password"
|
||||||
|
},
|
||||||
|
"mailSources": [
|
||||||
|
{
|
||||||
|
"name": "go_test_account",
|
||||||
|
"enabled": true,
|
||||||
|
"protocol": "imap",
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 3143,
|
||||||
|
"user": "testuser1",
|
||||||
|
"password": "password123",
|
||||||
|
"mode": "archive",
|
||||||
|
"folderFilter": {
|
||||||
|
"include": ["INBOX"],
|
||||||
|
"exclude": []
|
||||||
|
},
|
||||||
|
"messageFilter": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
24
test-config-rust.json
Normal file
24
test-config-rust.json
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"couchDb": {
|
||||||
|
"url": "http://localhost:5984",
|
||||||
|
"user": "admin",
|
||||||
|
"password": "password"
|
||||||
|
},
|
||||||
|
"mailSources": [
|
||||||
|
{
|
||||||
|
"name": "rust_test_account",
|
||||||
|
"enabled": true,
|
||||||
|
"protocol": "imap",
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 3143,
|
||||||
|
"user": "testuser1",
|
||||||
|
"password": "password123",
|
||||||
|
"mode": "archive",
|
||||||
|
"folderFilter": {
|
||||||
|
"include": ["INBOX"],
|
||||||
|
"exclude": []
|
||||||
|
},
|
||||||
|
"messageFilter": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
24
test-config-shared.json
Normal file
24
test-config-shared.json
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"couchDb": {
|
||||||
|
"url": "http://localhost:5984",
|
||||||
|
"user": "admin",
|
||||||
|
"password": "password"
|
||||||
|
},
|
||||||
|
"mailSources": [
|
||||||
|
{
|
||||||
|
"name": "comparison_test_account",
|
||||||
|
"enabled": true,
|
||||||
|
"protocol": "imap",
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 3143,
|
||||||
|
"user": "testuser1",
|
||||||
|
"password": "password123",
|
||||||
|
"mode": "archive",
|
||||||
|
"folderFilter": {
|
||||||
|
"include": ["INBOX"],
|
||||||
|
"exclude": []
|
||||||
|
},
|
||||||
|
"messageFilter": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
24
test-incremental-config.json
Normal file
24
test-incremental-config.json
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
{
|
||||||
|
"couchDb": {
|
||||||
|
"url": "http://localhost:5984",
|
||||||
|
"user": "admin",
|
||||||
|
"password": "password"
|
||||||
|
},
|
||||||
|
"mailSources": [
|
||||||
|
{
|
||||||
|
"name": "incremental_test",
|
||||||
|
"enabled": true,
|
||||||
|
"protocol": "imap",
|
||||||
|
"host": "localhost",
|
||||||
|
"port": 3143,
|
||||||
|
"user": "testuser1",
|
||||||
|
"password": "password123",
|
||||||
|
"mode": "archive",
|
||||||
|
"folderFilter": {
|
||||||
|
"include": ["INBOX"],
|
||||||
|
"exclude": []
|
||||||
|
},
|
||||||
|
"messageFilter": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue