added progress & log callback functions

This commit is contained in:
DecDuck
2024-10-10 16:35:45 +11:00
parent c370aba5b4
commit 174e48667f
3 changed files with 21 additions and 6 deletions

3
.gitignore vendored
View File

@ -197,4 +197,5 @@ Cargo.lock
*.node
index.js
index.d.ts
test.mjs
test.mjs
manifest.json

View File

@ -1,6 +1,6 @@
{
"name": "@drop/droplet",
"version": "0.4.0",
"version": "0.4.1",
"main": "index.js",
"types": "index.d.ts",
"napi": {

View File

@ -8,7 +8,7 @@ use std::{
use std::os::unix::fs::PermissionsExt;
use gxhash::gxhash128;
use napi::Error;
use napi::{Error, JsFunction, JsNumber};
use serde::Serialize;
use serde_json::json;
use uuid::Uuid;
@ -27,12 +27,18 @@ struct Chunk {
}
#[napi]
pub fn generate_manifest(dir: String) -> Result<String, Error> {
pub fn generate_manifest(
dir: String,
progress: JsFunction,
log: JsFunction,
) -> Result<String, Error> {
let base_dir = Path::new(&dir);
let files = list_files(base_dir);
let mut chunks: Vec<Chunk> = Vec::new();
let total: i32 = files.len() as i32;
let mut i: i32 = 0;
for file_path in files {
let file = File::open(file_path.clone()).unwrap();
let relative = file_path.strip_prefix(base_dir).unwrap();
@ -72,11 +78,19 @@ pub fn generate_manifest(dir: String) -> Result<String, Error> {
chunks.push(chunk);
println!("Processed chunk {} for {}", chunk_index, relative.to_str().unwrap());
log
.call1::<String, ()>(format!(
"Processed chunk {} for {}",
chunk_index,
relative.to_str().unwrap()
))
.unwrap();
reader.consume(length);
chunk_index += 1;
}
i += 1;
progress.call1::<i32, ()>(i * 100 / total).unwrap();
}
Ok(json!(chunks).to_string())