oximedia-batch 0.1.8

Comprehensive batch processing engine for OxiMedia
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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
//! File operation implementations

use crate::error::{BatchError, Result};
use crate::job::BatchJob;
use crate::operations::OperationExecutor;
use async_trait::async_trait;
use oxiarc_archive::zip::{ZipCompressionLevel, ZipWriter};
use oxiarc_archive::TarWriter;
use oxiarc_deflate::GzipStreamEncoder;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::fs;
use std::io::Read;
use std::path::{Path, PathBuf};
use walkdir::WalkDir;

/// File operation types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FileOperation {
    /// Copy files
    Copy {
        /// Overwrite existing files
        overwrite: bool,
    },
    /// Move files
    Move {
        /// Overwrite existing files
        overwrite: bool,
    },
    /// Rename files
    Rename {
        /// Name template
        template: String,
    },
    /// Delete files
    Delete {
        /// Confirm deletion
        confirm: bool,
    },
    /// Create archive
    Archive {
        /// Archive format
        format: ArchiveFormat,
        /// Compression level (0-9)
        compression: u32,
    },
    /// Extract archive
    Extract {
        /// Destination directory
        destination: PathBuf,
    },
    /// Verify file integrity
    Verify {
        /// Verification method
        method: VerifyMethod,
    },
    /// Calculate checksum
    Checksum {
        /// Hash algorithm
        algorithm: HashAlgorithm,
    },
}

/// Archive formats
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ArchiveFormat {
    /// ZIP format
    Zip,
    /// TAR format
    Tar,
    /// TAR.GZ format
    TarGz,
}

/// Verification methods
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum VerifyMethod {
    /// Checksum verification
    Checksum {
        /// Expected checksum
        expected: String,
        /// Hash algorithm
        algorithm: HashAlgorithm,
    },
    /// File size verification
    Size {
        /// Expected size in bytes
        expected: u64,
    },
}

/// Hash algorithms
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum HashAlgorithm {
    /// SHA-256
    Sha256,
    /// MD5
    Md5,
}

/// File operation executor
pub struct FileOperationExecutor;

impl FileOperationExecutor {
    /// Create a new file operation executor
    #[must_use]
    pub const fn new() -> Self {
        Self
    }

    fn copy_file(src: &Path, dst: &Path, overwrite: bool) -> Result<()> {
        if dst.exists() && !overwrite {
            return Err(BatchError::FileOperationError(format!(
                "Destination file already exists: {}",
                dst.display()
            )));
        }

        if let Some(parent) = dst.parent() {
            fs::create_dir_all(parent)?;
        }

        fs::copy(src, dst)?;
        Ok(())
    }

    fn move_file(src: &Path, dst: &Path, overwrite: bool) -> Result<()> {
        if dst.exists() && !overwrite {
            return Err(BatchError::FileOperationError(format!(
                "Destination file already exists: {}",
                dst.display()
            )));
        }

        if let Some(parent) = dst.parent() {
            fs::create_dir_all(parent)?;
        }

        fs::rename(src, dst)?;
        Ok(())
    }

    fn delete_file(path: &Path) -> Result<()> {
        if path.is_file() {
            fs::remove_file(path)?;
        } else if path.is_dir() {
            fs::remove_dir_all(path)?;
        }
        Ok(())
    }

    fn create_zip_archive(files: &[PathBuf], output: &Path, compression: u32) -> Result<()> {
        let file = fs::File::create(output)?;
        let mut zip = ZipWriter::new(file);

        let level = match compression {
            0 => ZipCompressionLevel::Store,
            1..=3 => ZipCompressionLevel::Fast,
            4..=7 => ZipCompressionLevel::Normal,
            _ => ZipCompressionLevel::Best,
        };
        zip.set_compression(level);

        for input_path in files {
            if input_path.is_file() {
                let file_name = input_path
                    .file_name()
                    .ok_or_else(|| BatchError::FileOperationError("Invalid file name".to_string()))?
                    .to_str()
                    .ok_or_else(|| {
                        BatchError::FileOperationError("Non-UTF8 file name".to_string())
                    })?;

                let mut f = fs::File::open(input_path)?;
                let mut buffer = Vec::new();
                f.read_to_end(&mut buffer)?;
                zip.add_file(file_name, &buffer)
                    .map_err(|e| BatchError::FileOperationError(format!("Zip add error: {e}")))?;
            } else if input_path.is_dir() {
                for entry in WalkDir::new(input_path) {
                    let entry = entry
                        .map_err(|e| BatchError::FileOperationError(format!("Walk error: {e}")))?;
                    let path = entry.path();
                    if path.is_file() {
                        let name = path
                            .strip_prefix(input_path)
                            .map_err(|e| {
                                BatchError::FileOperationError(format!("Path error: {e}"))
                            })?
                            .to_str()
                            .ok_or_else(|| {
                                BatchError::FileOperationError("Non-UTF8 path".to_string())
                            })?;
                        let mut f = fs::File::open(path)?;
                        let mut buffer = Vec::new();
                        f.read_to_end(&mut buffer)?;
                        zip.add_file(name, &buffer).map_err(|e| {
                            BatchError::FileOperationError(format!("Zip add error: {e}"))
                        })?;
                    }
                }
            }
        }

        zip.finish()
            .map_err(|e| BatchError::FileOperationError(format!("Zip finish error: {e}")))?;
        Ok(())
    }

    fn create_tar_archive(files: &[PathBuf], output: &Path, gzip: bool) -> Result<()> {
        let file = fs::File::create(output)?;

        if gzip {
            let enc = GzipStreamEncoder::new(file, 6);
            let mut tar_writer = TarWriter::new(enc);
            Self::add_paths_to_tar(&mut tar_writer, files)?;
            let gzip_encoder = tar_writer.into_inner().map_err(|e| {
                BatchError::FileOperationError(format!("Failed to finish tar: {e}"))
            })?;
            gzip_encoder.finish().map_err(|e| {
                BatchError::FileOperationError(format!("Failed to finish gzip: {e}"))
            })?;
        } else {
            let mut tar_writer = TarWriter::new(file);
            Self::add_paths_to_tar(&mut tar_writer, files)?;
            tar_writer.finish().map_err(|e| {
                BatchError::FileOperationError(format!("Failed to finish tar: {e}"))
            })?;
        }

        Ok(())
    }

    fn add_paths_to_tar<W: std::io::Write>(
        tar_writer: &mut TarWriter<W>,
        files: &[PathBuf],
    ) -> Result<()> {
        for input_path in files {
            if input_path.is_file() {
                let name = input_path
                    .file_name()
                    .ok_or_else(|| BatchError::FileOperationError("Invalid file name".to_string()))?
                    .to_str()
                    .ok_or_else(|| {
                        BatchError::FileOperationError("Non-UTF8 file name".to_string())
                    })?;
                let mut f = fs::File::open(input_path)?;
                let mut buffer = Vec::new();
                f.read_to_end(&mut buffer)?;
                tar_writer
                    .add_file(name, &buffer)
                    .map_err(|e| BatchError::FileOperationError(format!("Tar add error: {e}")))?;
            } else if input_path.is_dir() {
                let dir_name = input_path
                    .file_name()
                    .ok_or_else(|| BatchError::FileOperationError("Invalid dir name".to_string()))?
                    .to_str()
                    .ok_or_else(|| {
                        BatchError::FileOperationError("Non-UTF8 dir name".to_string())
                    })?;
                for entry in WalkDir::new(input_path) {
                    let entry = entry
                        .map_err(|e| BatchError::FileOperationError(format!("Walk error: {e}")))?;
                    let path = entry.path();
                    if path.is_file() {
                        let rel = path.strip_prefix(input_path).map_err(|e| {
                            BatchError::FileOperationError(format!("Path error: {e}"))
                        })?;
                        let archive_name = format!(
                            "{}/{}",
                            dir_name,
                            rel.to_str().ok_or_else(|| {
                                BatchError::FileOperationError("Non-UTF8 path".to_string())
                            })?
                        );
                        let mut f = fs::File::open(path)?;
                        let mut buffer = Vec::new();
                        f.read_to_end(&mut buffer)?;
                        tar_writer.add_file(&archive_name, &buffer).map_err(|e| {
                            BatchError::FileOperationError(format!("Tar add error: {e}"))
                        })?;
                    } else if path.is_dir() && path != input_path {
                        let rel = path.strip_prefix(input_path).map_err(|e| {
                            BatchError::FileOperationError(format!("Path error: {e}"))
                        })?;
                        let archive_name = format!(
                            "{}/{}",
                            dir_name,
                            rel.to_str().ok_or_else(|| {
                                BatchError::FileOperationError("Non-UTF8 path".to_string())
                            })?
                        );
                        tar_writer.add_directory(&archive_name).map_err(|e| {
                            BatchError::FileOperationError(format!("Tar add dir error: {e}"))
                        })?;
                    }
                }
            }
        }
        Ok(())
    }

    fn calculate_sha256(path: &Path) -> Result<String> {
        let mut file = fs::File::open(path)?;
        let mut hasher = Sha256::new();
        let mut buffer = vec![0; 8192];

        loop {
            let n = file.read(&mut buffer)?;
            if n == 0 {
                break;
            }
            hasher.update(&buffer[..n]);
        }

        Ok(hex::encode(hasher.finalize()))
    }
}

impl Default for FileOperationExecutor {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl OperationExecutor for FileOperationExecutor {
    async fn execute(&self, job: &BatchJob, input_files: &[PathBuf]) -> Result<Vec<PathBuf>> {
        let start = std::time::Instant::now();
        let mut output_files = Vec::new();

        match &job.operation {
            crate::job::BatchOperation::FileOp { operation } => match operation {
                FileOperation::Copy { overwrite } => {
                    for input_file in input_files {
                        for output_spec in &job.outputs {
                            let output_path = PathBuf::from(&output_spec.template);
                            Self::copy_file(input_file, &output_path, *overwrite)?;
                            output_files.push(output_path);
                        }
                    }
                }
                FileOperation::Move { overwrite } => {
                    for input_file in input_files {
                        for output_spec in &job.outputs {
                            let output_path = PathBuf::from(&output_spec.template);
                            Self::move_file(input_file, &output_path, *overwrite)?;
                            output_files.push(output_path);
                        }
                    }
                }
                FileOperation::Delete { confirm: _ } => {
                    for input_file in input_files {
                        Self::delete_file(input_file)?;
                    }
                }
                FileOperation::Archive {
                    format,
                    compression,
                } => {
                    if let Some(output_spec) = job.outputs.first() {
                        let output_path = PathBuf::from(&output_spec.template);
                        match format {
                            ArchiveFormat::Zip => {
                                Self::create_zip_archive(input_files, &output_path, *compression)?;
                            }
                            ArchiveFormat::Tar => {
                                Self::create_tar_archive(input_files, &output_path, false)?;
                            }
                            ArchiveFormat::TarGz => {
                                Self::create_tar_archive(input_files, &output_path, true)?;
                            }
                        }
                        output_files.push(output_path);
                    }
                }
                FileOperation::Checksum { algorithm: _ } => {
                    for input_file in input_files {
                        let checksum = Self::calculate_sha256(input_file)?;
                        let checksum_file = input_file.with_extension("sha256");
                        fs::write(&checksum_file, checksum)?;
                        output_files.push(checksum_file);
                    }
                }
                _ => {
                    return Err(BatchError::FileOperationError(
                        "Unsupported operation".to_string(),
                    ));
                }
            },
            _ => {
                return Err(BatchError::FileOperationError(
                    "Not a file operation".to_string(),
                ));
            }
        }

        tracing::info!("File operation completed in {:?}", start.elapsed());

        Ok(output_files)
    }

    fn validate(&self, job: &BatchJob) -> Result<()> {
        if !matches!(job.operation, crate::job::BatchOperation::FileOp { .. }) {
            return Err(BatchError::ValidationError(
                "Not a file operation".to_string(),
            ));
        }
        Ok(())
    }

    fn estimate_duration(&self, _job: &BatchJob, _input_files: &[PathBuf]) -> Option<u64> {
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    #[test]
    fn test_copy_file() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let src = temp_dir.path().join("source.txt");
        let dst = temp_dir.path().join("dest.txt");

        fs::write(&src, b"test content").expect("operation should succeed");

        let result = FileOperationExecutor::copy_file(&src, &dst, false);
        assert!(result.is_ok());
        assert!(dst.exists());

        let content = fs::read_to_string(&dst).expect("formatting should succeed");
        assert_eq!(content, "test content");
    }

    #[test]
    fn test_copy_file_no_overwrite() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let src = temp_dir.path().join("source.txt");
        let dst = temp_dir.path().join("dest.txt");

        fs::write(&src, b"test content").expect("operation should succeed");
        fs::write(&dst, b"existing content").expect("operation should succeed");

        let result = FileOperationExecutor::copy_file(&src, &dst, false);
        assert!(result.is_err());
    }

    #[test]
    fn test_move_file() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let src = temp_dir.path().join("source.txt");
        let dst = temp_dir.path().join("dest.txt");

        fs::write(&src, b"test content").expect("operation should succeed");

        let result = FileOperationExecutor::move_file(&src, &dst, false);
        assert!(result.is_ok());
        assert!(!src.exists());
        assert!(dst.exists());
    }

    #[test]
    fn test_delete_file() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let file = temp_dir.path().join("test.txt");

        fs::write(&file, b"test content").expect("operation should succeed");

        let result = FileOperationExecutor::delete_file(&file);
        assert!(result.is_ok());
        assert!(!file.exists());
    }

    #[test]
    fn test_calculate_sha256() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let file = temp_dir.path().join("test.txt");

        fs::write(&file, b"test content").expect("operation should succeed");

        let result = FileOperationExecutor::calculate_sha256(&file);
        assert!(result.is_ok());

        let hash = result.expect("result should be valid");
        assert_eq!(hash.len(), 64); // SHA-256 is 64 hex characters
    }

    #[test]
    fn test_create_zip_archive() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let file1 = temp_dir.path().join("file1.txt");
        let file2 = temp_dir.path().join("file2.txt");
        let archive = temp_dir.path().join("archive.zip");

        fs::write(&file1, b"content 1").expect("operation should succeed");
        fs::write(&file2, b"content 2").expect("operation should succeed");

        let result = FileOperationExecutor::create_zip_archive(&[file1, file2], &archive, 6);
        assert!(result.is_ok());
        assert!(archive.exists());
    }
}