bazzar/crates/db-seed/src/photos.rs

180 lines
4.6 KiB
Rust
Raw Normal View History

2022-05-08 09:47:05 +02:00
use actix::{Actor, Addr};
use actix_web::web::BytesMut;
2022-05-08 09:47:05 +02:00
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<Database>,
seed: SharedState,
fs: Addr<fs_manager::FsManager>,
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);
2022-05-08 09:47:05 +02:00
}
};
let write = async {
let fs_manager::WriteResult {
unique_name,
2022-05-08 09:47:05 +02:00
local_path,
file_name,
2022-05-08 09:47:05 +02:00
} = 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
2022-05-08 09:47:05 +02:00
},
crate::Error::WritePhoto(file.into())
);
seed.lock().unwrap().photos.push(photo);
Ok(())
};
let (_, res) = tokio::join!(read, write);
tracing::debug!("Photo {:?} done", file);
2022-05-08 09:47:05 +02:00
res
}
pub(crate) async fn create_photos(
db: Addr<Database>,
seed: SharedState,
config: SharedAppConfig,
) -> Result<()> {
tracing::debug!("Creating photos");
2022-05-08 09:47:05 +02:00
let photos: Vec<model::Photo> = 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"
),
2022-05-08 09:47:05 +02:00
);
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();
2022-05-08 09:47:05 +02:00
Ok(())
}