mirror of
https://gitlab.com/xmpp-rs/xmpp-rs.git
synced 2024-07-12 22:21:53 +00:00
Move handle_upload_result to upload module
This commit is contained in:
parent
75b864a5f9
commit
b4caea19d4
3 changed files with 69 additions and 54 deletions
|
@ -13,7 +13,7 @@ use tokio_xmpp::parsers::{
|
|||
stanza_error::{DefinedCondition, ErrorType, StanzaError},
|
||||
};
|
||||
|
||||
use crate::{handle_upload_result, pubsub, Agent, Event};
|
||||
use crate::{pubsub, upload, Agent, Event};
|
||||
|
||||
pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
|
||||
let mut events = vec![];
|
||||
|
@ -71,7 +71,7 @@ pub async fn handle_iq(agent: &mut Agent, iq: Iq) -> Vec<Event> {
|
|||
let new_events = pubsub::handle_iq_result(&from, payload);
|
||||
events.extend(new_events);
|
||||
} else if payload.is("slot", ns::HTTP_UPLOAD) {
|
||||
let new_events = handle_upload_result(&from, iq.id, payload, agent).await;
|
||||
let new_events = upload::handle_upload_result(&from, iq.id, payload, agent).await;
|
||||
events.extend(new_events);
|
||||
} else if payload.is("query", ns::PRIVATE) {
|
||||
match PrivateXMLQuery::try_from(payload) {
|
||||
|
|
|
@ -7,20 +7,16 @@
|
|||
#![deny(bare_trait_objects)]
|
||||
|
||||
use futures::stream::StreamExt;
|
||||
use reqwest::{
|
||||
header::HeaderMap as ReqwestHeaderMap, Body as ReqwestBody, Client as ReqwestClient,
|
||||
};
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::{Arc, RwLock};
|
||||
use tokio::fs::File;
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
pub use tokio_xmpp::parsers;
|
||||
use tokio_xmpp::parsers::{
|
||||
bookmarks, bookmarks2,
|
||||
caps::{compute_disco, hash_caps, Caps},
|
||||
disco::{DiscoInfoQuery, DiscoInfoResult, Feature, Identity},
|
||||
hashes::Algo,
|
||||
http_upload::{Header as HttpUploadHeader, SlotRequest, SlotResult},
|
||||
http_upload::SlotRequest,
|
||||
iq::Iq,
|
||||
message::{Body, Message, MessageType},
|
||||
muc::{user::MucUser, Muc},
|
||||
|
@ -40,6 +36,7 @@ pub mod iq;
|
|||
pub mod message;
|
||||
pub mod presence;
|
||||
pub mod pubsub;
|
||||
pub mod upload;
|
||||
|
||||
pub type Error = tokio_xmpp::Error;
|
||||
|
||||
|
@ -485,53 +482,6 @@ impl Agent {
|
|||
}
|
||||
}
|
||||
|
||||
async fn handle_upload_result(
|
||||
from: &Jid,
|
||||
iqid: String,
|
||||
elem: Element,
|
||||
agent: &mut Agent,
|
||||
) -> impl IntoIterator<Item = Event> {
|
||||
let mut res: Option<(usize, PathBuf)> = None;
|
||||
|
||||
for (i, (id, to, filepath)) in agent.uploads.iter().enumerate() {
|
||||
if to == from && id == &iqid {
|
||||
res = Some((i, filepath.to_path_buf()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((index, file)) = res {
|
||||
agent.uploads.remove(index);
|
||||
let slot = SlotResult::try_from(elem).unwrap();
|
||||
|
||||
let mut headers = ReqwestHeaderMap::new();
|
||||
for header in slot.put.headers {
|
||||
let (attr, val) = match header {
|
||||
HttpUploadHeader::Authorization(val) => ("Authorization", val),
|
||||
HttpUploadHeader::Cookie(val) => ("Cookie", val),
|
||||
HttpUploadHeader::Expires(val) => ("Expires", val),
|
||||
};
|
||||
headers.insert(attr, val.parse().unwrap());
|
||||
}
|
||||
|
||||
let web = ReqwestClient::new();
|
||||
let stream = FramedRead::new(File::open(file).await.unwrap(), BytesCodec::new());
|
||||
let body = ReqwestBody::wrap_stream(stream);
|
||||
let res = web
|
||||
.put(slot.put.url.as_str())
|
||||
.headers(headers)
|
||||
.body(body)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
if res.status() == 201 {
|
||||
return vec![Event::HttpUploadedFile(slot.get.url)];
|
||||
}
|
||||
}
|
||||
|
||||
return vec![];
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Agent, BareJid, ClientBuilder, ClientFeature, ClientType, Event};
|
||||
|
|
65
xmpp/src/upload/mod.rs
Normal file
65
xmpp/src/upload/mod.rs
Normal file
|
@ -0,0 +1,65 @@
|
|||
// Copyright (c) 2023 xmpp-rs contributors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
use reqwest::{
|
||||
header::HeaderMap as ReqwestHeaderMap, Body as ReqwestBody, Client as ReqwestClient,
|
||||
};
|
||||
use std::path::PathBuf;
|
||||
use tokio::fs::File;
|
||||
use tokio_util::codec::{BytesCodec, FramedRead};
|
||||
use tokio_xmpp::{
|
||||
parsers::http_upload::{Header as HttpUploadHeader, SlotResult},
|
||||
Element, Jid,
|
||||
};
|
||||
|
||||
use crate::{Agent, Event};
|
||||
|
||||
pub async fn handle_upload_result(
|
||||
from: &Jid,
|
||||
iqid: String,
|
||||
elem: Element,
|
||||
agent: &mut Agent,
|
||||
) -> impl IntoIterator<Item = Event> {
|
||||
let mut res: Option<(usize, PathBuf)> = None;
|
||||
|
||||
for (i, (id, to, filepath)) in agent.uploads.iter().enumerate() {
|
||||
if to == from && id == &iqid {
|
||||
res = Some((i, filepath.to_path_buf()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some((index, file)) = res {
|
||||
agent.uploads.remove(index);
|
||||
let slot = SlotResult::try_from(elem).unwrap();
|
||||
|
||||
let mut headers = ReqwestHeaderMap::new();
|
||||
for header in slot.put.headers {
|
||||
let (attr, val) = match header {
|
||||
HttpUploadHeader::Authorization(val) => ("Authorization", val),
|
||||
HttpUploadHeader::Cookie(val) => ("Cookie", val),
|
||||
HttpUploadHeader::Expires(val) => ("Expires", val),
|
||||
};
|
||||
headers.insert(attr, val.parse().unwrap());
|
||||
}
|
||||
|
||||
let web = ReqwestClient::new();
|
||||
let stream = FramedRead::new(File::open(file).await.unwrap(), BytesCodec::new());
|
||||
let body = ReqwestBody::wrap_stream(stream);
|
||||
let res = web
|
||||
.put(slot.put.url.as_str())
|
||||
.headers(headers)
|
||||
.body(body)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
if res.status() == 201 {
|
||||
return vec![Event::HttpUploadedFile(slot.get.url)];
|
||||
}
|
||||
}
|
||||
|
||||
return vec![];
|
||||
}
|
Loading…
Reference in a new issue