use actix::{Actor, Addr}; use actix_web::web::BytesMut; use config::SharedAppConfig; use database_manager::{query_db, Database}; use fs_manager::query_fs; use tokio::io::AsyncReadExt; use crate::{Result, SharedState}; async fn create_photo( db: Addr, seed: SharedState, fs: Addr, file: &'static str, ) -> crate::Result<()> { let (tx, rx) = tokio::sync::mpsc::unbounded_channel(); let read = async move { let mut file = tokio::fs::File::open(std::path::Path::new("./assets/examples/images").join(file)) .await .unwrap(); let mut buffer = BytesMut::with_capacity(1024); while let Ok(len) = file.read_buf(&mut buffer).await { if len == 0 { break; } tx.send(actix_web::web::Bytes::from(buffer)).unwrap(); buffer = BytesMut::with_capacity(1024); } }; let write = async { let fs_manager::WriteResult { unique_name, local_path, file_name, } = query_fs!( fs, fs_manager::WriteFile { file_name: file.to_string(), stream: rx, }, panic ); let photo: model::Photo = query_db!( db, database_manager::CreatePhoto { local_path, file_name, unique_name }, crate::Error::WritePhoto(file.into()) ); seed.lock().unwrap().photos.push(photo); Ok(()) }; let (_, res) = tokio::join!(read, write); tracing::debug!("Photo {:?} done", file); res } pub(crate) async fn create_photos( db: Addr, seed: SharedState, config: SharedAppConfig, ) -> Result<()> { tracing::debug!("Creating photos"); let photos: Vec = query_db!(db, database_manager::AllPhotos, default vec![]); if photos.len() >= 10 { seed.lock().unwrap().photos = photos; return Ok(()); } let fs = fs_manager::FsManager::build(config.clone()) .await .unwrap() .start(); let results = tokio::join!( create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-alex-azabache-3907507.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-binoid-cbd-3612182.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-caio-1279107.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-eprism-studio-335257.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-gabriel-freytez-341523.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-jess-bailey-designs-913135.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-luis-quintero-1738641.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-math-90946.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-mike-380954.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-pixabay-279906.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-Venus-HD-Make-up-and-perfume-2587370.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-agnese-lunecka-10322857.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-agnese-lunecka-11179383.webp" ), create_photo( db.clone(), seed.clone(), fs.clone(), "pexels-agnese-lunecka-11328773.webp" ), ); results.0.unwrap(); results.1.unwrap(); results.2.unwrap(); results.3.unwrap(); results.4.unwrap(); results.5.unwrap(); results.6.unwrap(); results.7.unwrap(); results.8.unwrap(); results.9.unwrap(); results.10.unwrap(); results.11.unwrap(); Ok(()) }