Skip to content
Snippets Groups Projects
Commit 4686cfa7 authored by Stephen D's avatar Stephen D
Browse files

Merge branch 'worker_improvements' into 'master'

Don't block async when processing images

See merge request !5
parents 36f5c81b 6ed9bc82
No related branches found
No related tags found
1 merge request!5Don't block async when processing images
......@@ -10,6 +10,7 @@ use std::fs::File;
use std::io::prelude::*;
use std::io::Cursor;
use std::path::Path;
use tokio::task;
async fn download_file(url: &str) -> Option<Bytes> {
let resp = reqwest::get(url).await.ok()?;
......@@ -27,35 +28,40 @@ fn write_file(path: &Path, bytes: &[u8]) -> Option<()> {
async fn process_file(
url: &str,
output_directory: &str,
output_id: &str,
formats: &[Format],
output_directory: String,
output_id: String,
formats: Vec<Format>,
) -> Option<String> {
let content = download_file(url).await?;
let reader = ImageReader::new(Cursor::new(content))
.with_guessed_format()
.ok()?;
let img_format = reader.format()?;
let extension = img_format.extensions_str()[0];
let img = reader.decode().ok()?;
for format in formats {
let img = img.resize(
format.width.unwrap_or_else(|| img.width()),
format.height.unwrap_or_else(|| img.height()),
FilterType::Triangle,
);
let mut buffer = Vec::new();
img.write_to(&mut buffer, img_format).ok()?;
write_file(
&Path::new(output_directory)
.join(&format!("{}_{}.{}", output_id, format.name, extension)),
&buffer,
)?;
}
let extension = task::spawn_blocking(move || {
let reader = ImageReader::new(Cursor::new(content))
.with_guessed_format()
.ok()?;
let img_format = reader.format()?;
let extension = img_format.extensions_str()[0];
let img = reader.decode().ok()?;
for format in formats {
let img = img.resize(
format.width.unwrap_or_else(|| img.width()),
format.height.unwrap_or_else(|| img.height()),
FilterType::Triangle,
);
let mut buffer = Vec::new();
img.write_to(&mut buffer, img_format).ok()?;
write_file(
&Path::new(&output_directory)
.join(&format!("{}_{}.{}", output_id, format.name, extension)),
&buffer,
)?;
}
Some(extension)
})
.await
.unwrap()?; // Propagate any panics
Some(extension.to_string())
}
......@@ -92,9 +98,9 @@ async fn main() {
|(row, pool)| async move {
let extension = match process_file(
&row.original_url,
image_directory,
&row.id.to_string(),
&formats,
image_directory.to_string(),
row.id.to_string(),
formats.clone(),
)
.await
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment