bitwarden_uniffi/vault/
attachments.rs

1use std::path::Path;
2
3use bitwarden_vault::{Attachment, AttachmentEncryptResult, AttachmentView, Cipher};
4
5use crate::{error::Error, Result};
6
7#[derive(uniffi::Object)]
8pub struct AttachmentsClient(pub(crate) bitwarden_vault::AttachmentsClient);
9
10#[uniffi::export]
11impl AttachmentsClient {
12    /// Encrypt an attachment file in memory
13    pub fn encrypt_buffer(
14        &self,
15        cipher: Cipher,
16        attachment: AttachmentView,
17        buffer: Vec<u8>,
18    ) -> Result<AttachmentEncryptResult> {
19        Ok(self
20            .0
21            .encrypt_buffer(cipher, attachment, &buffer)
22            .map_err(Error::Encrypt)?)
23    }
24
25    /// Encrypt an attachment file located in the file system
26    pub fn encrypt_file(
27        &self,
28        cipher: Cipher,
29        attachment: AttachmentView,
30        decrypted_file_path: String,
31        encrypted_file_path: String,
32    ) -> Result<Attachment> {
33        Ok(self
34            .0
35            .encrypt_file(
36                cipher,
37                attachment,
38                Path::new(&decrypted_file_path),
39                Path::new(&encrypted_file_path),
40            )
41            .map_err(Error::EncryptFile)?)
42    }
43    /// Decrypt an attachment file in memory
44    pub fn decrypt_buffer(
45        &self,
46        cipher: Cipher,
47        attachment: AttachmentView,
48        buffer: Vec<u8>,
49    ) -> Result<Vec<u8>> {
50        Ok(self
51            .0
52            .decrypt_buffer(cipher, attachment, &buffer)
53            .map_err(Error::Decrypt)?)
54    }
55
56    /// Decrypt an attachment file located in the file system
57    pub fn decrypt_file(
58        &self,
59        cipher: Cipher,
60        attachment: AttachmentView,
61        encrypted_file_path: String,
62        decrypted_file_path: String,
63    ) -> Result<()> {
64        Ok(self
65            .0
66            .decrypt_file(
67                cipher,
68                attachment,
69                Path::new(&encrypted_file_path),
70                Path::new(&decrypted_file_path),
71            )
72            .map_err(Error::DecryptFile)?)
73    }
74}