BlueGit

ict rusty-teams

Files

rusty-teams / crates / store / src / attachment_images.rs

attachment_images.rs

attachment_images.rs 1.9 KiB Download Raw
1use rusqlite::{Connection, params};
2
3use crate::error::Result;
4use crate::models::AttachmentImage;
5
6pub(crate) const OUTBOX_OWNER: &str = "outbox";
7pub(crate) const DRAFT_OWNER: &str = "draft";
8
9pub(crate) fn replace_images(
10 connection: &Connection,
11 owner_kind: &str,
12 owner_id: &str,
13 images: &[AttachmentImage],
14) -> Result<()> {
15 delete_images(connection, owner_kind, owner_id)?;
16 let mut insert = connection.prepare_cached(
17 "INSERT INTO attachment_images (owner_kind, owner_id, position, name, format, bytes, width, height)
18 VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8)",
19 )?;
20 for (position, image) in images.iter().enumerate() {
21 insert.execute(params![
22 owner_kind,
23 owner_id,
24 position as i64,
25 image.name,
26 image.format,
27 image.bytes,
28 image.width,
29 image.height,
30 ])?;
31 }
32 Ok(())
33}
34
35pub(crate) fn load_images(
36 connection: &Connection,
37 owner_kind: &str,
38 owner_id: &str,
39) -> Result<Vec<AttachmentImage>> {
40 let mut statement = connection.prepare_cached(
41 "SELECT name, format, bytes, width, height FROM attachment_images
42 WHERE owner_kind = ?1 AND owner_id = ?2 ORDER BY position",
43 )?;
44 let images = statement
45 .query_map(params![owner_kind, owner_id], |row| {
46 Ok(AttachmentImage {
47 name: row.get(0)?,
48 format: row.get(1)?,
49 bytes: row.get(2)?,
50 width: row.get(3)?,
51 height: row.get(4)?,
52 })
53 })?
54 .collect::<rusqlite::Result<Vec<_>>>()?;
55 Ok(images)
56}
57
58pub(crate) fn delete_images(
59 connection: &Connection,
60 owner_kind: &str,
61 owner_id: &str,
62) -> Result<()> {
63 connection.execute(
64 "DELETE FROM attachment_images WHERE owner_kind = ?1 AND owner_id = ?2",
65 params![owner_kind, owner_id],
66 )?;
67 Ok(())
68}