zebflow 0.8.3

Reactive web automation platform — pipelines, templates, and real-time collaboration in a single binary.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
//! n.fs.compress — archive one project file or folder into a compressed bundle.
//!
//! First slice supports only `tar.gz`.

use std::path::Path;
use std::process::Command;
use std::sync::Arc;

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::json;

use super::file_ref::file_ref_to_rel_path_or_string;
use super::util::{metadata_scope, resolve_path};
use crate::pipeline::{
    NodeDefinition, PipelineError,
    model::{DslFlag, DslFlagKind, LayoutItem, NodeFieldDef, NodeFieldType, SelectOptionDef},
    nodes::{NodeExecutionInput, NodeExecutionOutput, NodeHandler},
};
use crate::platform::services::PlatformService;

pub const NODE_KIND: &str = "n.fs.compress";
const INPUT_PIN_IN: &str = "in";
const OUTPUT_PIN_OUT: &str = "out";

fn default_source_key() -> String {
    "saved.path".to_string()
}

fn default_format() -> String {
    "tar.gz".to_string()
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Config {
    #[serde(default = "default_source_key")]
    pub source_key: String,
    #[serde(default)]
    pub extra_source_keys: Vec<String>,
    #[serde(default)]
    pub output_path: String,
    #[serde(default = "default_format")]
    pub format: String,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            source_key: default_source_key(),
            extra_source_keys: Vec::new(),
            output_path: String::new(),
            format: default_format(),
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompressOutput {
    pub source_path: String,
    pub source_paths: Vec<String>,
    pub archive_path: String,
    pub archive_url: String,
    pub format: String,
    pub size: u64,
}

pub fn definition() -> NodeDefinition {
    NodeDefinition {
        kind: NODE_KIND.to_string(),
        title: "File Compress".to_string(),
        description: "Archive one project file or folder into a compressed bundle. \
            Reads the source path from `input.saved.path` by default. \
            First slice supports only tar.gz."
            .to_string(),
        input_schema: json!({
            "type": "object",
            "description": "Payload must contain a project-relative file or folder path at the configured source_key."
        }),
        output_schema: json!({
            "type": "object",
            "properties": {
                "compressed": {
                    "type": "object",
                    "properties": {
                        "source_path": { "type": "string" },
                        "source_paths": {
                            "type": "array",
                            "items": { "type": "string" }
                        },
                        "archive_path": { "type": "string" },
                        "archive_url": { "type": "string" },
                        "format": { "type": "string" },
                        "size": { "type": "integer" }
                    }
                }
            }
        }),
        input_pins: vec![INPUT_PIN_IN.to_string()],
        output_pins: vec![OUTPUT_PIN_OUT.to_string()],
        script_available: false,
        script_bridge: None,
        config_schema: Default::default(),
        dsl_flags: vec![
            DslFlag {
                flag: "--source-key".to_string(),
                config_key: "source_key".to_string(),
                description: "Dot-path to the source file or folder path in payload (default: saved.path)".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
            DslFlag {
                flag: "--extra-source-keys".to_string(),
                config_key: "extra_source_keys".to_string(),
                description: "Comma-separated extra payload keys to include in the same archive".to_string(),
                kind: DslFlagKind::CommaSeparatedList,
                required: false,
            },
            DslFlag {
                flag: "--output-path".to_string(),
                config_key: "output_path".to_string(),
                description: "Destination ZebFS object path. Defaults to archives/<source>.tar.gz".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
            DslFlag {
                flag: "--format".to_string(),
                config_key: "format".to_string(),
                description: "Archive format. First slice supports only tar.gz".to_string(),
                kind: DslFlagKind::Scalar,
                required: false,
            },
        ],
        fields: vec![
            NodeFieldDef {
                name: "source_key".to_string(),
                label: "Source Key".to_string(),
                field_type: NodeFieldType::Text,
                help: Some("Dot-path to the project-relative source path in the input payload.".to_string()),
                default_value: Some(json!("saved.path")),
                ..Default::default()
            },
            NodeFieldDef {
                name: "extra_source_keys".to_string(),
                label: "Extra Source Keys".to_string(),
                field_type: NodeFieldType::Text,
                help: Some(
                    "Comma-separated extra payload keys whose file/folder paths should be included in the same archive."
                        .to_string(),
                ),
                ..Default::default()
            },
            NodeFieldDef {
                name: "output_path".to_string(),
                label: "Output Path".to_string(),
                field_type: NodeFieldType::Text,
                help: Some("Destination ZebFS object path. Leave blank to use archives/<source>.tar.gz.".to_string()),
                ..Default::default()
            },
            NodeFieldDef {
                name: "format".to_string(),
                label: "Format".to_string(),
                field_type: NodeFieldType::Select,
                default_value: Some(json!("tar.gz")),
                options: vec![SelectOptionDef {
                    label: "tar.gz".to_string(),
                    value: "tar.gz".to_string(),
                }],
                help: Some("Archive format to write. Current runtime supports tar.gz.".to_string()),
                ..Default::default()
            },
        ],
        layout: vec![LayoutItem::Col {
            col: vec![
                LayoutItem::Field("source_key".to_string()),
                LayoutItem::Field("extra_source_keys".to_string()),
                LayoutItem::Field("output_path".to_string()),
                LayoutItem::Field("format".to_string()),
            ],
        }],
        ai_tool: Default::default(),
        ..Default::default()
    }
}

pub struct Node {
    config: Config,
    platform: Arc<PlatformService>,
}

impl Node {
    pub fn new(config: Config, platform: Arc<PlatformService>) -> Result<Self, PipelineError> {
        Ok(Self { config, platform })
    }
}

#[async_trait]
impl NodeHandler for Node {
    fn kind(&self) -> &'static str {
        NODE_KIND
    }

    fn input_pins(&self) -> &'static [&'static str] {
        &[INPUT_PIN_IN]
    }

    fn output_pins(&self) -> &'static [&'static str] {
        &[OUTPUT_PIN_OUT]
    }

    async fn execute_async(
        &self,
        input: NodeExecutionInput,
    ) -> Result<NodeExecutionOutput, PipelineError> {
        let (owner, project, ..) = metadata_scope(&input.metadata)?;
        validate_format(&self.config.format)?;

        let source_key = if self.config.source_key.trim().is_empty() {
            "saved.path"
        } else {
            self.config.source_key.trim()
        };

        let primary_source_rel = resolve_path(&input.payload, source_key)
            .and_then(file_ref_to_rel_path_or_string)
            .ok_or_else(|| {
                PipelineError::new(
                    "FW_NODE_FILE_COMPRESS",
                    format!(
                        "source path not found at payload key '{source_key}' — chain after n.fs.save or set --source-key"
                    ),
                )
            })?;

        let primary_source_rel = sanitize_rel_path(&primary_source_rel);
        if primary_source_rel.is_empty() {
            return Err(PipelineError::new(
                "FW_NODE_FILE_COMPRESS",
                "resolved source path is empty after sanitization",
            ));
        }

        let mut source_paths = vec![primary_source_rel.clone()];
        for extra_key in &self.config.extra_source_keys {
            let extra_key = extra_key.trim();
            if extra_key.is_empty() {
                continue;
            }
            let extra_rel = resolve_path(&input.payload, extra_key)
                .and_then(file_ref_to_rel_path_or_string)
                .ok_or_else(|| {
                    PipelineError::new(
                        "FW_NODE_FILE_COMPRESS",
                        format!("extra source path not found at payload key '{extra_key}'"),
                    )
                })?;
            let extra_rel = sanitize_rel_path(&extra_rel);
            if extra_rel.is_empty() {
                return Err(PipelineError::new(
                    "FW_NODE_FILE_COMPRESS",
                    format!(
                        "resolved extra source path is empty after sanitization for key '{extra_key}'"
                    ),
                ));
            }
            if !source_paths.iter().any(|existing| existing == &extra_rel) {
                source_paths.push(extra_rel);
            }
        }

        let layout = self
            .platform
            .file
            .ensure_project_layout(owner, project)
            .map_err(|err| PipelineError::new("FW_NODE_FILE_COMPRESS", err.to_string()))?;

        let mut source_rels_for_task = Vec::with_capacity(source_paths.len());
        for source_rel in &source_paths {
            let source_abs = layout.files_dir.join(source_rel);
            if !source_abs.exists() {
                return Err(PipelineError::new(
                    "FW_NODE_FILE_COMPRESS",
                    format!("source path not found: {source_rel}"),
                ));
            }
            source_rels_for_task.push(source_rel.clone());
        }

        let archive_rel = resolve_archive_leaf(&self.config.output_path, &primary_source_rel);
        let archive_abs = layout.files_dir.join(&archive_rel);
        if let Some(parent) = archive_abs.parent() {
            std::fs::create_dir_all(parent).map_err(|err| {
                PipelineError::new(
                    "FW_NODE_FILE_COMPRESS",
                    format!("create archive parent dir: {err}"),
                )
            })?;
        }

        let archive_abs_for_task = archive_abs.clone();
        let files_root_for_task = layout.files_dir.clone();
        tokio::task::spawn_blocking(move || {
            compress_tar_gz(
                &files_root_for_task,
                &source_rels_for_task,
                &archive_abs_for_task,
            )
        })
        .await
        .map_err(|err| {
            PipelineError::new(
                "FW_NODE_FILE_COMPRESS",
                format!("archive task failed: {err}"),
            )
        })??;

        let size = std::fs::metadata(&archive_abs)
            .map_err(|err| {
                PipelineError::new(
                    "FW_NODE_FILE_COMPRESS",
                    format!("read archive metadata: {err}"),
                )
            })?
            .len();

        let output = CompressOutput {
            source_path: primary_source_rel.clone(),
            source_paths: source_paths.clone(),
            archive_path: archive_rel.clone(),
            archive_url: format!("/fs/{owner}/{project}/{archive_rel}"),
            format: "tar.gz".to_string(),
            size,
        };

        Ok(NodeExecutionOutput {
            output_pins: vec![OUTPUT_PIN_OUT.to_string()],
            payload: json!({ "compressed": output }),
            trace: vec![format!(
                "node_kind={NODE_KIND} srcs={} archive={} size={}",
                source_paths.join(","),
                archive_rel,
                size
            )],
        })
    }
}

fn compress_tar_gz(
    files_root: &Path,
    source_rels: &[String],
    archive_abs: &Path,
) -> Result<(), PipelineError> {
    if source_rels.is_empty() {
        return Err(PipelineError::new(
            "FW_NODE_FILE_COMPRESS",
            "no source paths provided to archive",
        ));
    }
    let mut command = Command::new("tar");
    command
        .arg("-czf")
        .arg(archive_abs)
        .arg("-C")
        .arg(files_root);
    for source_rel in source_rels {
        command.arg(source_rel);
    }
    let output = command.output().map_err(|err| {
        PipelineError::new(
            "FW_NODE_FILE_COMPRESS",
            format!("failed running tar: {err}"),
        )
    })?;
    if !output.status.success() {
        let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
        return Err(PipelineError::new(
            "FW_NODE_FILE_COMPRESS",
            format!("tar failed: {stderr}"),
        ));
    }
    Ok(())
}

fn validate_format(format: &str) -> Result<(), PipelineError> {
    if format.trim().eq_ignore_ascii_case("tar.gz") {
        Ok(())
    } else {
        Err(PipelineError::new(
            "FW_NODE_FILE_COMPRESS",
            format!(
                "unsupported archive format '{}'; first slice supports only tar.gz",
                format
            ),
        ))
    }
}

fn sanitize_rel_path(path: &str) -> String {
    path.split('/')
        .filter(|segment| !segment.is_empty() && *segment != "." && *segment != "..")
        .collect::<Vec<_>>()
        .join("/")
}

fn sanitize_filename_stem(name: &str) -> String {
    let stem = Path::new(name)
        .file_stem()
        .and_then(|value| value.to_str())
        .unwrap_or(name);
    let sanitized: String = stem
        .chars()
        .map(|ch| {
            if ch.is_alphanumeric() || ch == '-' || ch == '_' {
                ch.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect();
    sanitized
        .split('-')
        .filter(|segment| !segment.is_empty())
        .collect::<Vec<_>>()
        .join("-")
}

fn resolve_archive_leaf(configured: &str, source_rel_path: &str) -> String {
    let configured = sanitize_rel_path(configured);
    if !configured.is_empty() {
        if configured.ends_with(".tar.gz") {
            return configured;
        }
        return format!("{configured}.tar.gz");
    }
    let source_leaf = Path::new(source_rel_path)
        .file_name()
        .and_then(|value| value.to_str())
        .unwrap_or("bundle");
    let stem = sanitize_filename_stem(source_leaf);
    format!(
        "archives/{}.tar.gz",
        if stem.is_empty() { "bundle" } else { &stem }
    )
}

#[cfg(test)]
mod tests {
    use super::{resolve_archive_leaf, sanitize_rel_path};

    #[test]
    fn sanitizes_source_relative_paths() {
        assert_eq!(sanitize_rel_path("../pdf/./paper"), "pdf/paper");
    }

    #[test]
    fn derives_default_archive_leaf() {
        assert_eq!(
            resolve_archive_leaf("", "pdf/My Paper"),
            "archives/my-paper.tar.gz"
        );
    }
}