upload.rs 812 Bytes
use actix_web::{Error, HttpResponse, web};
use anyhow::Result;
use futures::stream::StreamExt;
use async_std::{fs::OpenOptions, io::WriteExt};
use uuid::Uuid;

use crate::AppData;

pub async fn upload( app_data: web::Data<AppData>
                   , mut body: web::Payload) -> Result<HttpResponse, Error>
{
    let worker = app_data.tx_upload_worker.clone();

    let upload_filename = format!("/tmp/upload_{}", Uuid::new_v4());
    let mut output = OpenOptions::new();
    output . create(true)
           . write(true);
    let mut output = output.open(&upload_filename).await?;

    while let Some(item) = body.next().await {
        output.write_all(&item?).await?;
    }

    // TODO handle this as error response...
    worker.send(upload_filename).await.unwrap();

    Ok(HttpResponse::Ok().finish())
}