asyncify plume-models: media upload is now async

including the use of tokio!
This commit is contained in:
Igor Galić 2020-01-29 13:40:03 +01:00
parent 3472a58299
commit 87ce3a7b51
No known key found for this signature in database
GPG Key ID: ACFEFF7F6A123A86
5 changed files with 16 additions and 12 deletions

1
Cargo.lock generated
View File

@ -2718,6 +2718,7 @@ dependencies = [
"serde_json",
"shrinkwraprs 0.3.0",
"tantivy",
"tokio 0.2.20",
"url 2.1.1",
"walkdir",
"webfinger",

View File

@ -21,6 +21,7 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
tantivy = "0.10.1"
tokio = "0.2"
url = "2.1"
walkdir = "2.2"
webfinger = "0.5"

View File

@ -81,10 +81,10 @@ impl Follow {
/// from -> The one sending the follow request
/// target -> The target of the request, responding with Accept
pub fn accept_follow<A: Signer + IntoId + Clone, B: Clone + AsActor<T> + IntoId, T>(
pub fn accept_follow<A: Signer + IntoId + Clone + Sync, B: Clone + AsActor<T> + IntoId, T>(
conn: &Connection,
from: &B,
target: &A,
target: &'static A,
follow: FollowAct,
from_id: i32,
target_id: i32,

View File

@ -12,6 +12,7 @@ use plume_common::{
};
use reqwest;
use std::{fs, path::Path};
use tokio::prelude::*;
#[derive(Clone, Identifiable, Queryable)]
pub struct Media {
@ -197,7 +198,7 @@ impl Media {
}
// TODO: merge with save_remote?
pub fn from_activity(c: &PlumeRocket, image: &Image) -> Result<Media> {
pub async fn from_activity(c: &PlumeRocket, image: &Image) -> Result<Media> {
let conn = &*c.conn;
let remote_url = image.object_props.url_string().ok()?;
let ext = remote_url
@ -211,11 +212,12 @@ impl Media {
ext
));
let mut dest = fs::File::create(path.clone()).ok()?;
reqwest::get(remote_url.as_str())
.ok()?
.copy_to(&mut dest)
.ok()?;
let mut dest = tokio::fs::File::create(path.clone()).await?;
let contents = reqwest::get(remote_url.as_str())
.await?
.bytes()
.await?;
dest.write_all(&contents).await?;
Media::insert(
conn,

View File

@ -223,7 +223,7 @@ impl User {
fn fetch(url: &str) -> Result<CustomPerson> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(url)
.header(
@ -358,7 +358,7 @@ impl User {
}
fn fetch_outbox_page<T: Activity>(&self, url: &str) -> Result<(Vec<T>, Option<String>)> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(url)
.header(
@ -388,7 +388,7 @@ impl User {
}
pub fn fetch_outbox<T: Activity>(&self) -> Result<Vec<T>> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(&self.outbox_url[..])
.header(
@ -433,7 +433,7 @@ impl User {
pub fn fetch_followers_ids(&self) -> Result<Vec<String>> {
let mut res = ClientBuilder::new()
.connect_timeout(Some(std::time::Duration::from_secs(5)))
.connect_timeout(std::time::Duration::from_secs(5))
.build()?
.get(&self.followers_endpoint[..])
.header(