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", "serde_json",
"shrinkwraprs 0.3.0", "shrinkwraprs 0.3.0",
"tantivy", "tantivy",
"tokio 0.2.20",
"url 2.1.1", "url 2.1.1",
"walkdir", "walkdir",
"webfinger", "webfinger",

View File

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

View File

@ -81,10 +81,10 @@ impl Follow {
/// from -> The one sending the follow request /// from -> The one sending the follow request
/// target -> The target of the request, responding with Accept /// 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, conn: &Connection,
from: &B, from: &B,
target: &A, target: &'static A,
follow: FollowAct, follow: FollowAct,
from_id: i32, from_id: i32,
target_id: i32, target_id: i32,

View File

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

View File

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