summaryrefslogtreecommitdiff
path: root/crates/jmap/src/blob/copy.rs
blob: 160eb28f2e21a94e923737b5ef2a02a410b418c5 (plain)
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
/*
 * SPDX-FileCopyrightText: 2020 Stalwart Labs Ltd <hello@stalw.art>
 *
 * SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-SEL
 */

use jmap_proto::{
    error::set::{SetError, SetErrorType},
    method::copy::{CopyBlobRequest, CopyBlobResponse},
    types::blob::BlobId,
};

use store::{
    write::{now, BatchBuilder, BlobOp},
    BlobClass, Serialize,
};
use utils::map::vec_map::VecMap;

use crate::{auth::AccessToken, JMAP};

impl JMAP {
    pub async fn blob_copy(
        &self,
        request: CopyBlobRequest,
        access_token: &AccessToken,
    ) -> trc::Result<CopyBlobResponse> {
        let mut response = CopyBlobResponse {
            from_account_id: request.from_account_id,
            account_id: request.account_id,
            copied: VecMap::with_capacity(request.blob_ids.len()),
            not_copied: VecMap::new(),
        };
        let account_id = request.account_id.document_id();

        for blob_id in request.blob_ids {
            if self.has_access_blob(&blob_id, access_token).await? {
                let mut batch = BatchBuilder::new();
                let until = now() + self.core.jmap.upload_tmp_ttl;
                batch.with_account_id(account_id).set(
                    BlobOp::Reserve {
                        until,
                        hash: blob_id.hash.clone(),
                    },
                    0u32.serialize(),
                );
                self.write_batch(batch).await?;
                let dest_blob_id = BlobId {
                    hash: blob_id.hash.clone(),
                    class: BlobClass::Reserved {
                        account_id,
                        expires: until,
                    },
                    section: blob_id.section.clone(),
                };

                response.copied.append(blob_id, dest_blob_id);
            } else {
                response.not_copied.append(
                    blob_id,
                    SetError::new(SetErrorType::BlobNotFound).with_description(
                        "blobId does not exist or not enough permissions to access it.",
                    ),
                );
            }
        }

        Ok(response)
    }
}