Skip to main content

bitwarden_uniffi/vault/
attachments.rs

1use std::path::Path;
2
3use bitwarden_vault::{
4    Attachment, AttachmentEncryptResult, AttachmentView, Cipher, CipherId, CipherView,
5    CreateAttachmentRequest, CreatedAttachment,
6};
7
8use crate::Result;
9
10#[derive(uniffi::Object)]
11pub struct AttachmentsClient(pub(crate) bitwarden_vault::AttachmentsClient);
12
13#[uniffi::export(async_runtime = "tokio")]
14impl AttachmentsClient {
15    /// Encrypt an attachment file in memory
16    pub fn encrypt_buffer(
17        &self,
18        cipher: Cipher,
19        attachment: AttachmentView,
20        buffer: Vec<u8>,
21    ) -> Result<AttachmentEncryptResult> {
22        Ok(self.0.encrypt_buffer(cipher, attachment, &buffer)?)
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.0.encrypt_file(
34            cipher,
35            attachment,
36            Path::new(&decrypted_file_path),
37            Path::new(&encrypted_file_path),
38        )?)
39    }
40    /// Decrypt an attachment file in memory
41    pub fn decrypt_buffer(
42        &self,
43        cipher: Cipher,
44        attachment: AttachmentView,
45        buffer: Vec<u8>,
46    ) -> Result<Vec<u8>> {
47        Ok(self.0.decrypt_buffer(cipher, attachment, &buffer)?)
48    }
49
50    /// Decrypt an attachment file located in the file system
51    pub fn decrypt_file(
52        &self,
53        cipher: Cipher,
54        attachment: AttachmentView,
55        encrypted_file_path: String,
56        decrypted_file_path: String,
57    ) -> Result<()> {
58        Ok(self.0.decrypt_file(
59            cipher,
60            attachment,
61            Path::new(&encrypted_file_path),
62            Path::new(&decrypted_file_path),
63        )?)
64    }
65
66    /// Create an attachment on a cipher, returning the URL the encrypted file should be uploaded
67    /// to and the updated cipher
68    pub async fn create_attachment(
69        &self,
70        cipher_id: CipherId,
71        request: CreateAttachmentRequest,
72    ) -> Result<CreatedAttachment> {
73        Ok(self.0.create_attachment(cipher_id, request).await?)
74    }
75
76    /// Delete an attachment from a cipher, returning the updated cipher
77    pub async fn delete_attachment(
78        &self,
79        cipher_id: CipherId,
80        attachment_id: String,
81    ) -> Result<Cipher> {
82        Ok(self.0.delete_attachment(cipher_id, attachment_id).await?)
83    }
84
85    /// Get a renewed upload URL for an attachment. Does not modify the attachment slot.
86    pub async fn renew_file_upload_url(
87        &self,
88        cipher_id: CipherId,
89        attachment_id: String,
90    ) -> Result<String> {
91        Ok(self
92            .0
93            .renew_file_upload_url(cipher_id, attachment_id)
94            .await?)
95    }
96
97    /// Re-encrypt a legacy attachment that has no attachment key, returning the updated cipher
98    pub async fn upgrade_attachment(
99        &self,
100        cipher_id: CipherId,
101        attachment_id: String,
102    ) -> Result<CipherView> {
103        Ok(self.0.upgrade_attachment(cipher_id, attachment_id).await?)
104    }
105
106    /// Get the download URL for an attachment. Pass `emergency_access_id` when downloading
107    /// through emergency access.
108    pub async fn get_attachment_download_url(
109        &self,
110        cipher_id: CipherId,
111        attachment_id: String,
112        emergency_access_id: Option<String>,
113    ) -> Result<String> {
114        Ok(self
115            .0
116            .get_attachment_download_url(cipher_id, attachment_id, emergency_access_id)
117            .await?)
118    }
119}