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
//! Validation utilities for batch processing

use crate::error::{BatchError, Result};
use crate::job::BatchJob;
use std::path::Path;

/// Validate job configuration
///
/// # Arguments
///
/// * `job` - Job to validate
///
/// # Errors
///
/// Returns an error if validation fails
pub fn validate_job(job: &BatchJob) -> Result<()> {
    // Validate job name
    if job.name.is_empty() {
        return Err(BatchError::ValidationError(
            "Job name cannot be empty".to_string(),
        ));
    }

    // Validate inputs
    if job.inputs.is_empty() {
        return Err(BatchError::ValidationError(
            "Job must have at least one input".to_string(),
        ));
    }

    // Validate input patterns
    for input in &job.inputs {
        validate_pattern(&input.pattern)?;
    }

    // Validate outputs
    for output in &job.outputs {
        validate_output_template(&output.template)?;
    }

    // Validate dependencies don't form cycles
    validate_dependencies(job)?;

    Ok(())
}

/// Validate file pattern
///
/// # Arguments
///
/// * `pattern` - File pattern to validate
///
/// # Errors
///
/// Returns an error if pattern is invalid
pub fn validate_pattern(pattern: &str) -> Result<()> {
    if pattern.is_empty() {
        return Err(BatchError::ValidationError(
            "Pattern cannot be empty".to_string(),
        ));
    }

    // Check for valid glob pattern
    glob::Pattern::new(pattern)?;

    Ok(())
}

/// Validate output template
///
/// # Arguments
///
/// * `template` - Output template to validate
///
/// # Errors
///
/// Returns an error if template is invalid
pub fn validate_output_template(template: &str) -> Result<()> {
    if template.is_empty() {
        return Err(BatchError::ValidationError(
            "Output template cannot be empty".to_string(),
        ));
    }

    // Check for balanced braces in template variables
    let mut brace_count = 0;
    for c in template.chars() {
        match c {
            '{' => brace_count += 1,
            '}' => {
                brace_count -= 1;
                if brace_count < 0 {
                    return Err(BatchError::ValidationError(
                        "Unbalanced braces in template".to_string(),
                    ));
                }
            }
            _ => {}
        }
    }

    if brace_count != 0 {
        return Err(BatchError::ValidationError(
            "Unbalanced braces in template".to_string(),
        ));
    }

    Ok(())
}

/// Validate job dependencies
///
/// # Arguments
///
/// * `job` - Job to validate
///
/// # Errors
///
/// Returns an error if dependencies form a cycle
fn validate_dependencies(job: &BatchJob) -> Result<()> {
    // Check that the job does not depend on itself
    if job.dependencies.contains(&job.id) {
        return Err(BatchError::ValidationError(format!(
            "Job '{}' depends on itself",
            job.id
        )));
    }

    // Check for duplicate dependency entries
    let mut seen = std::collections::HashSet::new();
    for dep in &job.dependencies {
        if !seen.insert(dep) {
            return Err(BatchError::ValidationError(format!(
                "Duplicate dependency '{}' in job '{}'",
                dep, job.id
            )));
        }
    }

    Ok(())
}

/// Validate file path
///
/// # Arguments
///
/// * `path` - Path to validate
///
/// # Errors
///
/// Returns an error if path is invalid
pub fn validate_path(path: &Path) -> Result<()> {
    // Check for null bytes
    let path_str = path
        .to_str()
        .ok_or_else(|| BatchError::ValidationError("Invalid path encoding".to_string()))?;

    if path_str.contains('\0') {
        return Err(BatchError::ValidationError(
            "Path contains null byte".to_string(),
        ));
    }

    // Check for relative path traversal
    if path_str.contains("..") {
        return Err(BatchError::ValidationError(
            "Path contains relative traversal".to_string(),
        ));
    }

    Ok(())
}

/// Validate bitrate value
///
/// # Arguments
///
/// * `bitrate` - Bitrate in bps
///
/// # Errors
///
/// Returns an error if bitrate is out of range
pub fn validate_bitrate(bitrate: u64) -> Result<()> {
    const MIN_BITRATE: u64 = 64_000; // 64 kbps
    const MAX_BITRATE: u64 = 500_000_000; // 500 Mbps

    if bitrate < MIN_BITRATE {
        return Err(BatchError::ValidationError(format!(
            "Bitrate too low: {bitrate} (minimum: {MIN_BITRATE})"
        )));
    }

    if bitrate > MAX_BITRATE {
        return Err(BatchError::ValidationError(format!(
            "Bitrate too high: {bitrate} (maximum: {MAX_BITRATE})"
        )));
    }

    Ok(())
}

/// Validate resolution
///
/// # Arguments
///
/// * `width` - Width in pixels
/// * `height` - Height in pixels
///
/// # Errors
///
/// Returns an error if resolution is invalid
pub fn validate_resolution(width: u32, height: u32) -> Result<()> {
    const MIN_DIMENSION: u32 = 16;
    const MAX_DIMENSION: u32 = 8192;

    if width < MIN_DIMENSION || height < MIN_DIMENSION {
        return Err(BatchError::ValidationError(format!(
            "Resolution too small: {width}x{height} (minimum: {MIN_DIMENSION}x{MIN_DIMENSION})"
        )));
    }

    if width > MAX_DIMENSION || height > MAX_DIMENSION {
        return Err(BatchError::ValidationError(format!(
            "Resolution too large: {width}x{height} (maximum: {MAX_DIMENSION}x{MAX_DIMENSION})"
        )));
    }

    // Check if dimensions are even (required for many codecs)
    if width % 2 != 0 || height % 2 != 0 {
        return Err(BatchError::ValidationError(format!(
            "Resolution dimensions must be even: {width}x{height}"
        )));
    }

    Ok(())
}

/// Validate framerate
///
/// # Arguments
///
/// * `fps` - Frames per second
///
/// # Errors
///
/// Returns an error if framerate is invalid
pub fn validate_framerate(fps: f64) -> Result<()> {
    const MIN_FPS: f64 = 1.0;
    const MAX_FPS: f64 = 240.0;

    if fps < MIN_FPS {
        return Err(BatchError::ValidationError(format!(
            "Framerate too low: {fps} (minimum: {MIN_FPS})"
        )));
    }

    if fps > MAX_FPS {
        return Err(BatchError::ValidationError(format!(
            "Framerate too high: {fps} (maximum: {MAX_FPS})"
        )));
    }

    Ok(())
}

/// Validate email address
///
/// # Arguments
///
/// * `email` - Email address to validate
///
/// # Errors
///
/// Returns an error if email is invalid
pub fn validate_email(email: &str) -> Result<()> {
    if !email.contains('@') {
        return Err(BatchError::ValidationError(
            "Invalid email format".to_string(),
        ));
    }

    let parts: Vec<&str> = email.split('@').collect();
    if parts.len() != 2 {
        return Err(BatchError::ValidationError(
            "Invalid email format".to_string(),
        ));
    }

    if parts[0].is_empty() || parts[1].is_empty() {
        return Err(BatchError::ValidationError(
            "Invalid email format".to_string(),
        ));
    }

    Ok(())
}

/// Validate URL
///
/// # Arguments
///
/// * `url` - URL to validate
///
/// # Errors
///
/// Returns an error if URL is invalid
pub fn validate_url(url: &str) -> Result<()> {
    if !url.starts_with("http://") && !url.starts_with("https://") {
        return Err(BatchError::ValidationError(
            "URL must start with http:// or https://".to_string(),
        ));
    }

    if url.len() < 10 {
        return Err(BatchError::ValidationError("URL too short".to_string()));
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::operations::FileOperation;

    #[test]
    fn test_validate_pattern() {
        assert!(validate_pattern("*.mp4").is_ok());
        assert!(validate_pattern("**/*.mp4").is_ok());
        assert!(validate_pattern("").is_err());
    }

    #[test]
    fn test_validate_output_template() {
        assert!(validate_output_template("{filename}.mp4").is_ok());
        assert!(validate_output_template("output_{date}.mp4").is_ok());
        assert!(validate_output_template("{unbalanced").is_err());
        assert!(validate_output_template("unbalanced}").is_err());
    }

    #[test]
    fn test_validate_path() {
        assert!(validate_path(&std::env::temp_dir().join("oximedia-batch-file.mp4")).is_ok());
        assert!(validate_path(Path::new("/tmp/../etc/passwd")).is_err());
    }

    #[test]
    fn test_validate_bitrate() {
        assert!(validate_bitrate(5_000_000).is_ok());
        assert!(validate_bitrate(100).is_err());
        assert!(validate_bitrate(1_000_000_000).is_err());
    }

    #[test]
    fn test_validate_resolution() {
        assert!(validate_resolution(1920, 1080).is_ok());
        assert!(validate_resolution(1280, 720).is_ok());
        assert!(validate_resolution(10, 10).is_err());
        assert!(validate_resolution(10000, 10000).is_err());
        assert!(validate_resolution(1921, 1080).is_err()); // Odd width
    }

    #[test]
    fn test_validate_framerate() {
        assert!(validate_framerate(30.0).is_ok());
        assert!(validate_framerate(29.97).is_ok());
        assert!(validate_framerate(0.5).is_err());
        assert!(validate_framerate(300.0).is_err());
    }

    #[test]
    fn test_validate_email() {
        assert!(validate_email("user@example.com").is_ok());
        assert!(validate_email("invalid").is_err());
        assert!(validate_email("@example.com").is_err());
        assert!(validate_email("user@").is_err());
    }

    #[test]
    fn test_validate_url() {
        assert!(validate_url("https://example.com").is_ok());
        assert!(validate_url("http://example.com").is_ok());
        assert!(validate_url("example.com").is_err());
        assert!(validate_url("ftp://example.com").is_err());
    }

    #[test]
    fn test_validate_job() {
        let mut job = BatchJob::new(
            "test".to_string(),
            crate::job::BatchOperation::FileOp {
                operation: FileOperation::Copy { overwrite: false },
            },
        );

        // No inputs - should fail
        assert!(validate_job(&job).is_err());

        // Add input - should pass
        job.add_input(crate::job::InputSpec::new("*.mp4".to_string()));
        assert!(validate_job(&job).is_ok());
    }
}