From 87ce3a7b518afa09dc18ad86e9b2f2e43e7056fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Igor=20Gali=C4=87?= Date: Wed, 29 Jan 2020 13:40:03 +0100 Subject: [PATCH] asyncify plume-models: media upload is now async including the use of tokio! --- Cargo.lock | 1 + plume-models/Cargo.toml | 1 + plume-models/src/follows.rs | 4 ++-- plume-models/src/medias.rs | 14 ++++++++------ plume-models/src/users.rs | 8 ++++---- 5 files changed, 16 insertions(+), 12 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b8384f47..3760d7f5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2718,6 +2718,7 @@ dependencies = [ "serde_json", "shrinkwraprs 0.3.0", "tantivy", + "tokio 0.2.20", "url 2.1.1", "walkdir", "webfinger", diff --git a/plume-models/Cargo.toml b/plume-models/Cargo.toml index 94b5c1a1..5d7f6ef5 100644 --- a/plume-models/Cargo.toml +++ b/plume-models/Cargo.toml @@ -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" diff --git a/plume-models/src/follows.rs b/plume-models/src/follows.rs index d371e04e..4dd0c58e 100644 --- a/plume-models/src/follows.rs +++ b/plume-models/src/follows.rs @@ -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 + IntoId, T>( + pub fn accept_follow + IntoId, T>( conn: &Connection, from: &B, - target: &A, + target: &'static A, follow: FollowAct, from_id: i32, target_id: i32, diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs index 7cae530a..6c401366 100644 --- a/plume-models/src/medias.rs +++ b/plume-models/src/medias.rs @@ -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 { + pub async fn from_activity(c: &PlumeRocket, image: &Image) -> Result { 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, diff --git a/plume-models/src/users.rs b/plume-models/src/users.rs index 5d1dd5fa..b6309acc 100644 --- a/plume-models/src/users.rs +++ b/plume-models/src/users.rs @@ -223,7 +223,7 @@ impl User { fn fetch(url: &str) -> Result { 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(&self, url: &str) -> Result<(Vec, Option)> { 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(&self) -> Result> { 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> { 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(