mirror of
https://github.com/Drop-OSS/droplet.git
synced 2026-06-22 04:11:40 +10:00
added progress & log callback functions
This commit is contained in:
+2
-1
@@ -197,4 +197,5 @@ Cargo.lock
|
|||||||
*.node
|
*.node
|
||||||
index.js
|
index.js
|
||||||
index.d.ts
|
index.d.ts
|
||||||
test.mjs
|
test.mjs
|
||||||
|
manifest.json
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@drop/droplet",
|
"name": "@drop/droplet",
|
||||||
"version": "0.4.0",
|
"version": "0.4.1",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"types": "index.d.ts",
|
"types": "index.d.ts",
|
||||||
"napi": {
|
"napi": {
|
||||||
|
|||||||
+18
-4
@@ -8,7 +8,7 @@ use std::{
|
|||||||
use std::os::unix::fs::PermissionsExt;
|
use std::os::unix::fs::PermissionsExt;
|
||||||
|
|
||||||
use gxhash::gxhash128;
|
use gxhash::gxhash128;
|
||||||
use napi::Error;
|
use napi::{Error, JsFunction, JsNumber};
|
||||||
use serde::Serialize;
|
use serde::Serialize;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
@@ -27,12 +27,18 @@ struct Chunk {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[napi]
|
#[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 base_dir = Path::new(&dir);
|
||||||
let files = list_files(base_dir);
|
let files = list_files(base_dir);
|
||||||
|
|
||||||
let mut chunks: Vec<Chunk> = Vec::new();
|
let mut chunks: Vec<Chunk> = Vec::new();
|
||||||
|
|
||||||
|
let total: i32 = files.len() as i32;
|
||||||
|
let mut i: i32 = 0;
|
||||||
for file_path in files {
|
for file_path in files {
|
||||||
let file = File::open(file_path.clone()).unwrap();
|
let file = File::open(file_path.clone()).unwrap();
|
||||||
let relative = file_path.strip_prefix(base_dir).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);
|
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);
|
reader.consume(length);
|
||||||
chunk_index += 1;
|
chunk_index += 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
i += 1;
|
||||||
|
progress.call1::<i32, ()>(i * 100 / total).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(json!(chunks).to_string())
|
Ok(json!(chunks).to_string())
|
||||||
|
|||||||
Reference in New Issue
Block a user