send.rs

 1// Copyright (c) 2023 xmpp-rs contributors.
 2//
 3// This Source Code Form is subject to the terms of the Mozilla Public
 4// License, v. 2.0. If a copy of the MPL was not distributed with this
 5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
 6
 7use std::path::Path;
 8use tokio::fs::File;
 9use tokio_xmpp::connect::ServerConnector;
10use tokio_xmpp::{
11    parsers::{http_upload::SlotRequest, iq::Iq},
12    Jid,
13};
14
15use crate::Agent;
16
17pub async fn upload_file_with<C: ServerConnector>(
18    agent: &mut Agent<C>,
19    service: &str,
20    path: &Path,
21) {
22    let name = path.file_name().unwrap().to_str().unwrap().to_string();
23    let file = File::open(path).await.unwrap();
24    let size = file.metadata().await.unwrap().len();
25    let slot_request = SlotRequest {
26        filename: name,
27        size: size,
28        content_type: None,
29    };
30    let to = service.parse::<Jid>().unwrap();
31    let request = Iq::from_get("upload1", slot_request).with_to(to.clone());
32    agent
33        .uploads
34        .push((String::from("upload1"), to, path.to_path_buf()));
35    agent.client.send_stanza(request.into()).await.unwrap();
36}