Skip to content

Commit b4a73d7

Browse files
committed
Support multi-image digital shares
1 parent c87b6d4 commit b4a73d7

File tree

5 files changed

+262
-74
lines changed

5 files changed

+262
-74
lines changed

report-listener/database/digital_share.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ import (
88
"time"
99
)
1010

11+
type DigitalShareAttachment struct {
12+
Ordinal int
13+
Filename string
14+
MIMEType string
15+
SHA256 string
16+
Bytes []byte
17+
}
18+
1119
func (d *Database) UpsertDigitalShareMetadata(
1220
ctx context.Context,
1321
reportSeq int,
@@ -19,6 +27,7 @@ func (d *Database) UpsertDigitalShareMetadata(
1927
clientSubmissionID string,
2028
normalizedSourceKey string,
2129
sharedText string,
30+
attachments []DigitalShareAttachment,
2231
) error {
2332
if reportSeq <= 0 {
2433
return nil
@@ -94,6 +103,30 @@ func (d *Database) UpsertDigitalShareMetadata(
94103
return fmt.Errorf("upsert digital_share_reports: %w", err)
95104
}
96105

106+
if _, err := tx.ExecContext(ctx, `DELETE FROM digital_share_attachments WHERE report_seq = ?`, reportSeq); err != nil {
107+
return fmt.Errorf("clear digital_share_attachments: %w", err)
108+
}
109+
110+
for _, attachment := range attachments {
111+
if len(attachment.Bytes) == 0 {
112+
continue
113+
}
114+
if _, err := tx.ExecContext(
115+
ctx,
116+
`INSERT INTO digital_share_attachments (
117+
report_seq, ordinal, filename, mime_type, sha256, image
118+
) VALUES (?, ?, ?, ?, ?, ?)`,
119+
reportSeq,
120+
attachment.Ordinal,
121+
nullIfBlank(attachment.Filename),
122+
nullIfBlank(attachment.MIMEType),
123+
nullIfBlank(attachment.SHA256),
124+
attachment.Bytes,
125+
); err != nil {
126+
return fmt.Errorf("insert digital_share_attachment[%d]: %w", attachment.Ordinal, err)
127+
}
128+
}
129+
97130
if err := tx.Commit(); err != nil {
98131
return fmt.Errorf("commit digital share tx: %w", err)
99132
}

report-listener/database/migrate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func RunMigrations(ctx context.Context, db *sql.DB) error {
3131
{ID: "0019_notify_quality_tuning", Description: "seed jurisdiction-aware authority rules and execution quality defaults", Up: func(ctx context.Context, db *sql.DB) error { return ensureNotifyQualityTuning(ctx, db) }},
3232
{ID: "0020_mobile_push_delivery", Description: "create mobile push device registry and report delivery event tables", Up: func(ctx context.Context, db *sql.DB) error { return ensureMobilePushDeliveryTables(ctx, db) }},
3333
{ID: "0021_digital_share_reports", Description: "create digital share report metadata table", Up: func(ctx context.Context, db *sql.DB) error { return ensureDigitalShareReportsTable(ctx, db) }},
34+
{ID: "0022_digital_share_attachments", Description: "create digital share attachment table", Up: func(ctx context.Context, db *sql.DB) error { return ensureDigitalShareAttachmentsTable(ctx, db) }},
3435
})
3536
}
3637

report-listener/database/migration_helpers.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,6 +1231,26 @@ func ensureDigitalShareReportsTable(ctx context.Context, db *sql.DB) error {
12311231
return nil
12321232
}
12331233

1234+
func ensureDigitalShareAttachmentsTable(ctx context.Context, db *sql.DB) error {
1235+
stmt := `CREATE TABLE IF NOT EXISTS digital_share_attachments (
1236+
report_seq INT NOT NULL,
1237+
ordinal INT NOT NULL,
1238+
filename VARCHAR(255) NULL,
1239+
mime_type VARCHAR(128) NULL,
1240+
sha256 CHAR(64) NULL,
1241+
image LONGBLOB NOT NULL,
1242+
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
1243+
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
1244+
PRIMARY KEY (report_seq, ordinal),
1245+
KEY idx_digital_share_attachments_sha256 (sha256),
1246+
CONSTRAINT fk_digital_share_attachments_report FOREIGN KEY (report_seq) REFERENCES reports(seq) ON DELETE CASCADE
1247+
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci`
1248+
if _, err := db.ExecContext(ctx, stmt); err != nil {
1249+
return fmt.Errorf("failed to ensure digital_share_attachments table: %w", err)
1250+
}
1251+
return nil
1252+
}
1253+
12341254
func ensureUnifiedDefectRoutingTables(ctx context.Context, db *sql.DB) error {
12351255
stmts := []string{
12361256
`CREATE TABLE IF NOT EXISTS subject_routing_profiles (

0 commit comments

Comments
 (0)