|
|
|
@ -5,10 +5,12 @@ use crate::controllers::members_controller::MemberRequest;
|
|
|
|
|
use crate::controllers::HttpResult;
|
|
|
|
|
use crate::extractors::family_extractor::{FamilyInPath, FamilyInPathWithAdminMembership};
|
|
|
|
|
use crate::models::{CoupleID, MemberID, PhotoID};
|
|
|
|
|
use crate::services::photos_service::UploadedFile;
|
|
|
|
|
use crate::services::{couples_service, members_service, photos_service};
|
|
|
|
|
use actix_multipart::form::tempfile::TempFile;
|
|
|
|
|
use actix_multipart::form::MultipartForm;
|
|
|
|
|
use actix_web::HttpResponse;
|
|
|
|
|
use mime_guess::Mime;
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::io;
|
|
|
|
|
use std::io::{Cursor, Read, Write};
|
|
|
|
@ -17,6 +19,7 @@ use zip::{CompressionMethod, ZipArchive};
|
|
|
|
|
|
|
|
|
|
const MEMBERS_FILE: &str = "members.json";
|
|
|
|
|
const COUPLES_FILE: &str = "couples.json";
|
|
|
|
|
const PHOTOS_DIR: &str = "photos/";
|
|
|
|
|
|
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
|
struct ImportMemberRequest {
|
|
|
|
@ -28,7 +31,6 @@ struct ImportMemberRequest {
|
|
|
|
|
|
|
|
|
|
#[derive(serde::Deserialize)]
|
|
|
|
|
struct ImportCoupleRequest {
|
|
|
|
|
id: CoupleID,
|
|
|
|
|
photo_id: Option<PhotoID>,
|
|
|
|
|
#[serde(flatten)]
|
|
|
|
|
data: CoupleRequest,
|
|
|
|
@ -70,7 +72,7 @@ pub async fn export_family(f: FamilyInPath) -> HttpResult {
|
|
|
|
|
let ext = photo.mime_extension().unwrap_or("bad");
|
|
|
|
|
let file = s3_connection::get_file(&photo.photo_path()).await?;
|
|
|
|
|
|
|
|
|
|
zip_file.start_file(format!("photos/{}.{ext}", id.0), files_opt)?;
|
|
|
|
|
zip_file.start_file(format!("{PHOTOS_DIR}{}.{ext}", id.0), files_opt)?;
|
|
|
|
|
zip_file.write_all(&file)?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -87,6 +89,21 @@ pub struct UploadFamilyDataForm {
|
|
|
|
|
archive: TempFile,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct Photo {
|
|
|
|
|
path: String,
|
|
|
|
|
mimetype: Mime,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
enum PhotoTarget {
|
|
|
|
|
Member(MemberID),
|
|
|
|
|
Couple(CoupleID),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct PhotoToProcess {
|
|
|
|
|
id: PhotoID,
|
|
|
|
|
target: PhotoTarget,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Import whole family data
|
|
|
|
|
pub async fn import_family(
|
|
|
|
|
f: FamilyInPathWithAdminMembership,
|
|
|
|
@ -94,6 +111,23 @@ pub async fn import_family(
|
|
|
|
|
) -> HttpResult {
|
|
|
|
|
let mut zip = ZipArchive::new(form.archive.file)?;
|
|
|
|
|
|
|
|
|
|
// Pre-process photos list
|
|
|
|
|
let mut photos = HashMap::new();
|
|
|
|
|
for file in zip.file_names() {
|
|
|
|
|
let (id, ext) = match file.strip_prefix(PHOTOS_DIR).map(|f| f.split_once('.')) {
|
|
|
|
|
Some(Some((id, ext))) => (id, ext),
|
|
|
|
|
_ => continue,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
photos.insert(
|
|
|
|
|
PhotoID(id.parse()?),
|
|
|
|
|
Photo {
|
|
|
|
|
path: file.to_string(),
|
|
|
|
|
mimetype: mime_guess::from_ext(ext).first_or_octet_stream(),
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Parse general information
|
|
|
|
|
let members_list = serde_json::from_slice::<Vec<ImportMemberRequest>>(&read_zip_file(
|
|
|
|
|
&mut zip,
|
|
|
|
@ -115,6 +149,8 @@ pub async fn import_family(
|
|
|
|
|
let mut rev_members_id_mapping = HashMap::new();
|
|
|
|
|
let mut new_members = Vec::with_capacity(members_list.len());
|
|
|
|
|
|
|
|
|
|
let mut photos_to_insert = Vec::with_capacity(photos.len());
|
|
|
|
|
|
|
|
|
|
for req_m in members_list {
|
|
|
|
|
// Create member entry in database
|
|
|
|
|
let new_m = members_service::create(f.family_id()).await?;
|
|
|
|
@ -146,7 +182,14 @@ pub async fn import_family(
|
|
|
|
|
req_member_data.mother = members_id_mapping.get(&i).copied();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req_member_data.to_member(member, true).await?;
|
|
|
|
|
req_member_data.to_member(member).await?;
|
|
|
|
|
|
|
|
|
|
if let Some(id) = req_member.photo_id {
|
|
|
|
|
photos_to_insert.push(PhotoToProcess {
|
|
|
|
|
id,
|
|
|
|
|
target: PhotoTarget::Member(db_id),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for loops
|
|
|
|
@ -161,29 +204,72 @@ pub async fn import_family(
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extract and insert couples information
|
|
|
|
|
let mut couple_mapping = HashMap::new();
|
|
|
|
|
for c in &mut couples_list {
|
|
|
|
|
let mut new_couples = HashMap::new();
|
|
|
|
|
for req_couple in &mut couples_list {
|
|
|
|
|
// Map wife and husband
|
|
|
|
|
if let Some(i) = c.data.wife {
|
|
|
|
|
c.data.wife = members_id_mapping.get(&i).copied();
|
|
|
|
|
if let Some(i) = req_couple.data.wife {
|
|
|
|
|
req_couple.data.wife = members_id_mapping.get(&i).copied();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some(i) = c.data.husband {
|
|
|
|
|
c.data.husband = members_id_mapping.get(&i).copied();
|
|
|
|
|
if let Some(i) = req_couple.data.husband {
|
|
|
|
|
req_couple.data.husband = members_id_mapping.get(&i).copied();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut db_couple = couples_service::create(f.family_id()).await?;
|
|
|
|
|
couple_mapping.insert(c.id, db_couple.id());
|
|
|
|
|
|
|
|
|
|
c.data.clone().to_couple(&mut db_couple, true).await?;
|
|
|
|
|
req_couple.data.clone().to_couple(&mut db_couple).await?;
|
|
|
|
|
|
|
|
|
|
couples_service::update(&mut db_couple).await?;
|
|
|
|
|
|
|
|
|
|
if let Some(id) = req_couple.photo_id {
|
|
|
|
|
photos_to_insert.push(PhotoToProcess {
|
|
|
|
|
id,
|
|
|
|
|
target: PhotoTarget::Couple(db_couple.id()),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
new_couples.insert(db_couple.id(), db_couple);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert photos
|
|
|
|
|
// TODO
|
|
|
|
|
// Insert member photos
|
|
|
|
|
for photo_to_process in photos_to_insert {
|
|
|
|
|
let photo = match photos.get(&photo_to_process.id) {
|
|
|
|
|
None => continue,
|
|
|
|
|
Some(photo) => photo,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().body("go on"))
|
|
|
|
|
let mut file = zip.by_name(&photo.path)?;
|
|
|
|
|
if file.size() > constants::PHOTOS_MAX_SIZE as u64 {
|
|
|
|
|
return Ok(HttpResponse::BadRequest().body("File is too large!"));
|
|
|
|
|
}
|
|
|
|
|
let mut buff = Vec::with_capacity(file.size() as usize);
|
|
|
|
|
file.read_to_end(&mut buff)?;
|
|
|
|
|
|
|
|
|
|
let photo = photos_service::finalize_upload(UploadedFile::from_memory(
|
|
|
|
|
&buff,
|
|
|
|
|
Some(photo.mimetype.clone()),
|
|
|
|
|
)?)
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
// Update appropriate database entry
|
|
|
|
|
match photo_to_process.target {
|
|
|
|
|
PhotoTarget::Member(member_id) => {
|
|
|
|
|
let member = new_members
|
|
|
|
|
.iter_mut()
|
|
|
|
|
.find(|m| m.id().eq(&member_id))
|
|
|
|
|
.unwrap();
|
|
|
|
|
member.set_photo_id(Some(photo.id()));
|
|
|
|
|
members_service::update(member).await?;
|
|
|
|
|
}
|
|
|
|
|
PhotoTarget::Couple(couple_id) => {
|
|
|
|
|
let couple = new_couples.get_mut(&couple_id).unwrap();
|
|
|
|
|
couple.set_photo_id(Some(photo.id()));
|
|
|
|
|
couples_service::update(couple).await?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(HttpResponse::Accepted().finish())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn read_zip_file<R: Read + io::Seek>(
|
|
|
|
|