1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
//! Attachment storage operations.
use super::{Database, now_ms};
use crate::types::{Attachment, AttachmentMeta};
use anyhow::{Result, anyhow};
use rusqlite::params;
impl Database {
/// Add an attachment to a task with auto-increment sequence per type.
/// Returns the sequence number of the new attachment.
/// If file_path is provided, content should be empty (stored externally).
pub fn add_attachment(
&self,
task_id: &str,
attachment_type: String,
name: String,
content: String,
mime_type: Option<String>,
file_path: Option<String>,
) -> Result<i32> {
let now = now_ms();
let mime_type = mime_type.unwrap_or_else(|| "text/plain".to_string());
self.with_conn_mut(|conn| {
let tx = conn.transaction()?;
// Verify task exists
let exists: bool = tx
.query_row(
"SELECT 1 FROM tasks WHERE id = ?1",
params![task_id],
|_| Ok(true),
)
.unwrap_or(false);
if !exists {
return Err(anyhow!("Task not found"));
}
// Get next sequence for this (task_id, attachment_type)
let max_seq: Option<i32> = tx.query_row(
"SELECT MAX(sequence) FROM attachments WHERE task_id = ?1 AND attachment_type = ?2",
params![task_id, attachment_type],
|row| row.get(0),
)?;
let sequence = max_seq.unwrap_or(-1) + 1;
tx.execute(
"INSERT INTO attachments (task_id, attachment_type, sequence, name, mime_type, content, file_path, created_at)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
params![
task_id,
attachment_type,
sequence,
name,
mime_type,
content,
file_path,
now,
],
)?;
tx.commit()?;
Ok(sequence)
})
}
/// Get attachments for a task, optionally including content.
/// Note: For file-based attachments, content is NOT loaded here - use get_attachment for that.
pub fn get_attachments_full(
&self,
task_id: &str,
include_content: bool,
) -> Result<Vec<Attachment>> {
self.with_conn(|conn| {
let mut stmt = conn.prepare(
"SELECT task_id, attachment_type, sequence, name, mime_type, content, file_path, created_at
FROM attachments WHERE task_id = ?1 ORDER BY attachment_type, sequence",
)?;
let attachments = stmt
.query_map(params![task_id], |row| {
let task_id: String = row.get(0)?;
let attachment_type: String = row.get(1)?;
let sequence: i32 = row.get(2)?;
let name: String = row.get(3)?;
let mime_type: String = row.get(4)?;
let content: String = row.get(5)?;
let file_path: Option<String> = row.get(6)?;
let created_at: i64 = row.get(7)?;
Ok(Attachment {
task_id,
attachment_type,
sequence,
name,
mime_type,
content: if include_content {
content
} else {
String::new()
},
file_path,
created_at,
})
})?
.filter_map(|r| r.ok())
.collect();
Ok(attachments)
})
}
/// Get attachments for a task (metadata only).
pub fn get_attachments(&self, task_id: &str) -> Result<Vec<AttachmentMeta>> {
self.get_attachments_filtered(task_id, None, None)
}
/// Get attachments for a task with optional filtering (metadata only).
/// - type_pattern: Optional glob pattern (with * wildcard) to filter by attachment_type
/// - mime_pattern: Optional prefix to filter by MIME type (e.g., "image/" matches "image/png")
pub fn get_attachments_filtered(
&self,
task_id: &str,
type_pattern: Option<&str>,
mime_pattern: Option<&str>,
) -> Result<Vec<AttachmentMeta>> {
self.with_conn(|conn| {
// Build query with optional filters
let mut sql = String::from(
"SELECT task_id, attachment_type, sequence, name, mime_type, file_path, created_at
FROM attachments WHERE task_id = ?1",
);
// For type pattern, convert glob to SQL LIKE pattern
let type_like = type_pattern.map(|p| {
// Convert glob wildcards to SQL LIKE: * -> %, ? -> _
p.replace('*', "%").replace('?', "_")
});
if type_like.is_some() {
sql.push_str(" AND attachment_type LIKE ?2 ESCAPE '\\'");
}
if mime_pattern.is_some() {
let idx = if type_like.is_some() { 3 } else { 2 };
sql.push_str(&format!(" AND mime_type LIKE ?{} ESCAPE '\\'", idx));
}
sql.push_str(" ORDER BY attachment_type, sequence");
let mut stmt = conn.prepare(&sql)?;
// Bind parameters based on which filters are present
let attachments: Vec<AttachmentMeta> = match (&type_like, mime_pattern) {
(Some(type_pat), Some(mime)) => {
let mime_like = format!("{}%", mime);
stmt.query_map(params![task_id, type_pat, mime_like], |row| {
Self::map_attachment_meta(row)
})?
.filter_map(|r| r.ok())
.collect()
}
(Some(type_pat), None) => stmt
.query_map(params![task_id, type_pat], Self::map_attachment_meta)?
.filter_map(|r| r.ok())
.collect(),
(None, Some(mime)) => {
let mime_like = format!("{}%", mime);
stmt.query_map(params![task_id, mime_like], |row| {
Self::map_attachment_meta(row)
})?
.filter_map(|r| r.ok())
.collect()
}
(None, None) => stmt
.query_map(params![task_id], Self::map_attachment_meta)?
.filter_map(|r| r.ok())
.collect(),
};
Ok(attachments)
})
}
/// Helper to map a row to AttachmentMeta.
fn map_attachment_meta(row: &rusqlite::Row) -> rusqlite::Result<AttachmentMeta> {
Ok(AttachmentMeta {
task_id: row.get(0)?,
attachment_type: row.get(1)?,
sequence: row.get(2)?,
name: row.get(3)?,
mime_type: row.get(4)?,
file_path: row.get(5)?,
created_at: row.get(6)?,
})
}
/// Get a full attachment by (task_id, attachment_type, sequence).
/// Note: For file-based attachments, content field contains the DB content (empty).
/// The caller should read from file_path if set.
pub fn get_attachment(
&self,
task_id: &str,
attachment_type: &str,
sequence: i32,
) -> Result<Option<Attachment>> {
self.with_conn(|conn| {
let mut stmt = conn.prepare(
"SELECT task_id, attachment_type, sequence, name, mime_type, content, file_path, created_at
FROM attachments WHERE task_id = ?1 AND attachment_type = ?2 AND sequence = ?3",
)?;
let result = stmt.query_row(params![task_id, attachment_type, sequence], |row| {
let task_id: String = row.get(0)?;
let attachment_type: String = row.get(1)?;
let sequence: i32 = row.get(2)?;
let name: String = row.get(3)?;
let mime_type: String = row.get(4)?;
let content: String = row.get(5)?;
let file_path: Option<String> = row.get(6)?;
let created_at: i64 = row.get(7)?;
Ok(Attachment {
task_id,
attachment_type,
sequence,
name,
mime_type,
content,
file_path,
created_at,
})
});
match result {
Ok(attachment) => Ok(Some(attachment)),
Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
Err(e) => Err(e.into()),
}
})
}
/// Get file_paths for all attachments of a given type (useful before deletion).
pub fn get_attachment_file_paths_by_type(
&self,
task_id: &str,
attachment_type: &str,
) -> Result<Vec<String>> {
self.with_conn(|conn| {
let mut stmt = conn.prepare(
"SELECT file_path FROM attachments WHERE task_id = ?1 AND attachment_type = ?2 AND file_path IS NOT NULL",
)?;
let paths: Vec<String> = stmt
.query_map(params![task_id, attachment_type], |row| row.get(0))?
.filter_map(|r| r.ok())
.collect();
Ok(paths)
})
}
/// Delete an attachment by (task_id, attachment_type, sequence).
pub fn delete_attachment(
&self,
task_id: &str,
attachment_type: &str,
sequence: i32,
) -> Result<bool> {
self.with_conn(|conn| {
let deleted = conn.execute(
"DELETE FROM attachments WHERE task_id = ?1 AND attachment_type = ?2 AND sequence = ?3",
params![task_id, attachment_type, sequence],
)?;
Ok(deleted > 0)
})
}
/// Delete all attachments of a given type (for replace behavior).
/// Returns the file_paths of deleted attachments (for cleanup).
pub fn delete_attachments_by_type(
&self,
task_id: &str,
attachment_type: &str,
) -> Result<Vec<String>> {
self.with_conn(|conn| {
// First get all file_paths
let file_paths = {
let mut stmt = conn.prepare(
"SELECT file_path FROM attachments WHERE task_id = ?1 AND attachment_type = ?2 AND file_path IS NOT NULL",
)?;
stmt.query_map(params![task_id, attachment_type], |row| row.get(0))?
.filter_map(|r| r.ok())
.collect::<Vec<String>>()
};
// Delete all attachments of this type
conn.execute(
"DELETE FROM attachments WHERE task_id = ?1 AND attachment_type = ?2",
params![task_id, attachment_type],
)?;
Ok(file_paths)
})
}
/// Delete all attachments of a given type and return count plus file_paths.
/// Returns (deleted_count, file_paths).
pub fn delete_attachments_by_type_ex(
&self,
task_id: &str,
attachment_type: &str,
) -> Result<(usize, Vec<String>)> {
self.with_conn(|conn| {
// First get all file_paths
let file_paths = {
let mut stmt = conn.prepare(
"SELECT file_path FROM attachments WHERE task_id = ?1 AND attachment_type = ?2 AND file_path IS NOT NULL",
)?;
stmt.query_map(params![task_id, attachment_type], |row| row.get(0))?
.filter_map(|r| r.ok())
.collect::<Vec<String>>()
};
// Delete all attachments of this type
let deleted = conn.execute(
"DELETE FROM attachments WHERE task_id = ?1 AND attachment_type = ?2",
params![task_id, attachment_type],
)?;
Ok((deleted, file_paths))
})
}
}