Skip to content

Commit

Permalink
_
Browse files Browse the repository at this point in the history
  • Loading branch information
CaviarChen committed Oct 21, 2024
1 parent 591f82f commit eb2fa25
Showing 1 changed file with 102 additions and 86 deletions.
188 changes: 102 additions & 86 deletions server/src/misc_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,106 @@ fn get_download_item(
download_times.get(token).cloned()
}

async fn download_snapshot(
conn: Connection<'_, Db>,
server_state: &rocket::State<ServerState>,
snapshot_id: i64,
) -> Result<FileResponse, InternalError> {
let db = conn.into_inner();
let snapshot = snapshot::Entity::find()
.filter(snapshot::Column::Id.eq(snapshot_id))
.one(db)
.await?;
match snapshot {
None => Ok(FileResponse::Forbidden),
Some(snapshot) => {
let user = User {
uid: snapshot.user_id,
};
let mut buf = Vec::new();
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(&mut buf));
let options = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
zip.add_directory("Sync/", options)?;

let entity::snapshot::SyncFiles(sync_files) = snapshot.sync_files;
for (file_id, sha256) in &sync_files {
let sync_file = data_fetcher::SyncFile::create_from_id(*file_id, sha256)?;
zip.start_file(format!("Sync/{}", sync_file.filename()), options)?;
let mut file = server_state.file_storage.open_file(&user, sha256)?;
// TODO: async?
std::io::copy(&mut file, &mut zip)?;
}
zip.finish()?;

Ok(FileResponse::Ok {
filename: String::from("Sync.zip"),
content: buf,
})
}
}
}

async fn download_memolanes_archive(
conn: Connection<'_, Db>,
server_state: &rocket::State<ServerState>,
uid: i64,
) -> Result<FileResponse, InternalError> {
let db = conn.into_inner();
let snapshots = snapshot::Entity::find()
.filter(snapshot::Column::UserId.eq(uid))
.order_by_desc(snapshot::Column::Timestamp)
.all(db)
.await?;

let user = User { uid };
let temp_dir = TempDir::new()?;
let mut main_db = memolanes_core::main_db::MainDb::open(temp_dir.path().to_str().unwrap());
for snapshot in snapshots {
let zip_file_path = temp_dir.path().join("temp_sync.zip");
let mut zip_file = fs::File::create(&zip_file_path)?;
let mut zip = zip::ZipWriter::new(&mut zip_file);
let options = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
zip.add_directory("Sync/", options)?;

let entity::snapshot::SyncFiles(sync_files) = snapshot.sync_files;
for (file_id, sha256) in &sync_files {
let sync_file = data_fetcher::SyncFile::create_from_id(*file_id, sha256)?;
zip.start_file(format!("Sync/{}", sync_file.filename()), options)?;
let mut file = server_state.file_storage.open_file(&user, sha256)?;
std::io::copy(&mut file, &mut zip)?;
}
zip.finish()?;

let journey_bitmap =
memolanes_core::import_data::load_fow_sync_data(zip_file_path.to_str().unwrap())?;
let journey_data = memolanes_core::journey_data::JourneyData::Bitmap(journey_bitmap.0);

// TODO: generate these details
main_db.with_txn(|txn| {
txn.create_and_insert_journey(
NaiveDate::default(),
None,
None,
None,
JourneyKind::DefaultKind,
None,
journey_data,
)
})?;
}

let mut buf = Vec::new();
let mut writer = Cursor::new(&mut buf);
memolanes_core::archive::archive_all_as_zip(&mut main_db, &mut writer)?;

Ok(FileResponse::Ok {
filename: String::from("export.mldx"),
content: buf,
})
}

#[get("/download?<token>")]
async fn download<'r>(
conn: Connection<'_, Db>,
Expand All @@ -84,94 +184,10 @@ async fn download<'r>(
match get_download_item(server_state, token) {
None => Ok(FileResponse::Forbidden),
Some(DownloadItem::Snapshot { snapshot_id }) => {
let db = conn.into_inner();
let snapshot = snapshot::Entity::find()
.filter(snapshot::Column::Id.eq(snapshot_id))
.one(db)
.await?;
match snapshot {
None => Ok(FileResponse::Forbidden),
Some(snapshot) => {
let user = User {
uid: snapshot.user_id,
};
let mut buf = Vec::new();
let mut zip = zip::ZipWriter::new(std::io::Cursor::new(&mut buf));
let options = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
zip.add_directory("Sync/", options)?;

let entity::snapshot::SyncFiles(sync_files) = snapshot.sync_files;
for (file_id, sha256) in &sync_files {
let sync_file = data_fetcher::SyncFile::create_from_id(*file_id, sha256)?;
zip.start_file(format!("Sync/{}", sync_file.filename()), options)?;
let mut file = server_state.file_storage.open_file(&user, sha256)?;
// TODO: async?
std::io::copy(&mut file, &mut zip)?;
}
zip.finish()?;

Ok(FileResponse::Ok {
filename: String::from("Sync.zip"),
content: buf,
})
}
}
download_snapshot(conn, server_state, snapshot_id).await
}
Some(DownloadItem::MemolanesArchive { uid }) => {
let db = conn.into_inner();
let snapshots = snapshot::Entity::find()
.filter(snapshot::Column::UserId.eq(uid))
.order_by_desc(snapshot::Column::Timestamp)
.all(db)
.await?;

let user = User { uid };
let temp_dir = TempDir::new()?;
let mut main_db =
memolanes_core::main_db::MainDb::open(temp_dir.path().to_str().unwrap());
for snapshot in snapshots {
let zip_file_path = temp_dir.path().join("temp_sync.zip");
let mut zip_file = fs::File::create(zip_file_path.clone())?;
let mut zip = zip::ZipWriter::new(&mut zip_file);
let options = zip::write::SimpleFileOptions::default()
.compression_method(zip::CompressionMethod::Stored);
zip.add_directory("Sync/", options)?;

let entity::snapshot::SyncFiles(sync_files) = snapshot.sync_files;
for (file_id, sha256) in &sync_files {
let sync_file = data_fetcher::SyncFile::create_from_id(*file_id, sha256)?;
zip.start_file(format!("Sync/{}", sync_file.filename()), options)?;
let mut file = server_state.file_storage.open_file(&user, sha256)?;
std::io::copy(&mut file, &mut zip)?;
}
zip.finish()?;
let journey_bitmap = memolanes_core::import_data::load_fow_sync_data(
zip_file_path.to_str().unwrap(),
)?;
let journey_data =
memolanes_core::journey_data::JourneyData::Bitmap(journey_bitmap.0);
main_db.with_txn(|txn| {
txn.create_and_insert_journey(
NaiveDate::default(),
None,
None,
None,
JourneyKind::DefaultKind,
None,
journey_data,
)
})?;
}

let mut buf = Vec::new();
let mut writer = Cursor::new(&mut buf);
memolanes_core::archive::archive_all_as_zip(&mut main_db, &mut writer)?;

Ok(FileResponse::Ok {
filename: String::from("Archive.zip"),
content: buf,
})
download_memolanes_archive(conn, server_state, uid).await
}
}
}
Expand Down

0 comments on commit eb2fa25

Please sign in to comment.