vidsage-core 0.1.0

Core functionality for VidSage video processing and AI commentary generation
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
//! Storage manager implementations

use super::{StorageError, StorageManager};
use crate::{CoreError, Result};
use flate2::{read::GzDecoder, write::GzEncoder, Compression};
use serde::{Deserialize, Serialize};
use std::fs::{self, File};
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use uuid::Uuid;

/// Storage configuration structure
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StorageConfig {
    /// Base directory for storage
    pub base_dir: PathBuf,

    /// Maximum file size in bytes
    pub max_file_size: u64,

    /// Enable compression for stored files
    pub enable_compression: bool,

    /// Compression level (0-9)
    pub compression_level: u8,
}

impl Default for StorageConfig {
    fn default() -> Self {
        Self {
            base_dir: PathBuf::from("./storage"),
            max_file_size: 100 * 1024 * 1024, // 100MB
            enable_compression: false,
            compression_level: 5,
        }
    }
}

/// File-based storage implementation
pub struct FileStorage {
    config: StorageConfig,
}

impl FileStorage {
    /// Create a new FileStorage instance
    pub fn new(config: StorageConfig) -> Result<Self> {
        // Create base directories if they don't exist
        fs::create_dir_all(&config.base_dir).map_err(|e| CoreError::IoError(e.to_string()))?;
        fs::create_dir_all(config.base_dir.join("videos"))
            .map_err(|e| CoreError::IoError(e.to_string()))?;
        fs::create_dir_all(config.base_dir.join("commentaries"))
            .map_err(|e| CoreError::IoError(e.to_string()))?;
        fs::create_dir_all(config.base_dir.join("metadata"))
            .map_err(|e| CoreError::IoError(e.to_string()))?;

        Ok(Self { config })
    }

    /// Get the path for a video file
    fn get_video_path(&self, id: &str) -> PathBuf {
        let base_path = self.config.base_dir.join("videos").join(id);
        if self.config.enable_compression {
            base_path.with_extension("gz")
        } else {
            base_path
        }
    }

    /// Get the path for metadata
    fn get_metadata_path(&self, id: &str) -> PathBuf {
        let base_path = self
            .config
            .base_dir
            .join("metadata")
            .join(format!("{}.json", id));
        if self.config.enable_compression {
            base_path.with_extension("json.gz")
        } else {
            base_path
        }
    }

    /// Get the path for a commentary file
    fn get_commentary_path(&self, id: &str) -> PathBuf {
        let base_path = self
            .config
            .base_dir
            .join("commentaries")
            .join(format!("{}.json", id));
        if self.config.enable_compression {
            base_path.with_extension("json.gz")
        } else {
            base_path
        }
    }

    /// Write data to file with optional compression
    fn write_file_with_compression(&self, path: &Path, data: &[u8]) -> Result<()> {
        if self.config.enable_compression {
            let file = File::create(path).map_err(|e| CoreError::IoError(e.to_string()))?;
            let compression_level = Compression::new(self.config.compression_level as u32);
            let mut encoder = GzEncoder::new(file, compression_level);
            encoder
                .write_all(data)
                .map_err(|e| CoreError::IoError(e.to_string()))?;
            encoder
                .finish()
                .map_err(|e| CoreError::IoError(e.to_string()))?;
        } else {
            let mut file = File::create(path).map_err(|e| CoreError::IoError(e.to_string()))?;
            file.write_all(data)
                .map_err(|e| CoreError::IoError(e.to_string()))?;
        }
        Ok(())
    }

    /// Read data from file with optional decompression
    fn read_file_with_decompression(&self, path: &Path) -> Result<Vec<u8>> {
        if self.config.enable_compression {
            let file = File::open(path).map_err(|e| CoreError::IoError(e.to_string()))?;
            let mut decoder = GzDecoder::new(file);
            let mut data = Vec::new();
            decoder
                .read_to_end(&mut data)
                .map_err(|e| CoreError::IoError(e.to_string()))?;
            Ok(data)
        } else {
            fs::read(path).map_err(|e| CoreError::IoError(e.to_string()))
        }
    }
}

#[async_trait::async_trait]
impl StorageManager for FileStorage {
    async fn store_video(&self, path: &Path, metadata: &serde_json::Value) -> Result<String> {
        // Check file size
        let file_metadata = fs::metadata(path).map_err(|e| CoreError::IoError(e.to_string()))?;
        if file_metadata.len() > self.config.max_file_size {
            return Err(CoreError::StorageError(StorageError::FileTooLarge(
                file_metadata.len(),
            )));
        }

        // Generate unique ID
        let id = Uuid::new_v4().to_string();

        // Read video file content
        let video_content = fs::read(path).map_err(|e| CoreError::IoError(e.to_string()))?;

        // Store video with optional compression
        let video_path = self.get_video_path(&id);
        self.write_file_with_compression(&video_path, &video_content)?;

        // Store metadata with optional compression
        let metadata_path = self.get_metadata_path(&id);
        let metadata_json = serde_json::to_string_pretty(metadata)
            .map_err(|e| CoreError::JsonError(e.to_string()))?;
        self.write_file_with_compression(&metadata_path, metadata_json.as_bytes())?;

        Ok(id)
    }

    async fn retrieve_video(&self, id: &str) -> Result<Vec<u8>> {
        let path = self.get_video_path(id);
        if !path.exists() {
            return Err(CoreError::StorageError(StorageError::FileNotFound(
                id.to_string(),
            )));
        }

        self.read_file_with_decompression(&path)
    }

    async fn get_video_metadata(&self, id: &str) -> Result<serde_json::Value> {
        let path = self.get_metadata_path(id);
        if !path.exists() {
            return Err(CoreError::StorageError(StorageError::FileNotFound(
                id.to_string(),
            )));
        }

        let content_bytes = self.read_file_with_decompression(&path)?;
        let content =
            String::from_utf8(content_bytes).map_err(|e| CoreError::IoError(e.to_string()))?;
        Ok(serde_json::from_str(&content).map_err(|e| CoreError::JsonError(e.to_string()))?)
    }

    async fn delete_video(&self, id: &str) -> Result<bool> {
        let video_path = self.get_video_path(id);
        let metadata_path = self.get_metadata_path(id);

        // Delete video file if it exists
        if video_path.exists() {
            fs::remove_file(video_path).map_err(|e| CoreError::IoError(e.to_string()))?;
        }

        // Delete metadata if it exists
        if metadata_path.exists() {
            fs::remove_file(metadata_path).map_err(|e| CoreError::IoError(e.to_string()))?;
        }

        Ok(true)
    }

    async fn store_commentary(&self, commentary: &serde_json::Value) -> Result<String> {
        // Generate unique ID
        let id = Uuid::new_v4().to_string();

        // Store commentary with optional compression
        let path = self.get_commentary_path(&id);
        let content = serde_json::to_string_pretty(commentary)
            .map_err(|e| CoreError::JsonError(e.to_string()))?;
        self.write_file_with_compression(&path, content.as_bytes())?;

        Ok(id)
    }

    async fn retrieve_commentary(&self, id: &str) -> Result<serde_json::Value> {
        let path = self.get_commentary_path(id);
        if !path.exists() {
            return Err(CoreError::StorageError(StorageError::FileNotFound(
                id.to_string(),
            )));
        }

        let content_bytes = self.read_file_with_decompression(&path)?;
        let content =
            String::from_utf8(content_bytes).map_err(|e| CoreError::IoError(e.to_string()))?;
        Ok(serde_json::from_str(&content).map_err(|e| CoreError::JsonError(e.to_string()))?)
    }

    async fn update_commentary(&self, id: &str, commentary: &serde_json::Value) -> Result<bool> {
        let path = self.get_commentary_path(id);
        if !path.exists() {
            return Err(CoreError::StorageError(StorageError::FileNotFound(
                id.to_string(),
            )));
        }

        // Update commentary with optional compression
        let content = serde_json::to_string_pretty(commentary)
            .map_err(|e| CoreError::JsonError(e.to_string()))?;
        self.write_file_with_compression(&path, content.as_bytes())?;

        Ok(true)
    }

    async fn delete_commentary(&self, id: &str) -> Result<bool> {
        let path = self.get_commentary_path(id);
        if !path.exists() {
            return Err(CoreError::StorageError(StorageError::FileNotFound(
                id.to_string(),
            )));
        }

        fs::remove_file(path).map_err(|e| CoreError::IoError(e.to_string()))?;
        Ok(true)
    }

    async fn list_videos(
        &self,
        page: u32,
        page_size: u32,
    ) -> Result<Vec<(String, serde_json::Value)>> {
        let videos_dir = self.config.base_dir.join("videos");

        // Read all video files
        let mut videos = Vec::new();
        for entry in fs::read_dir(videos_dir).map_err(|e| CoreError::IoError(e.to_string()))? {
            let entry = entry.map_err(|e| CoreError::IoError(e.to_string()))?;
            let file_name = entry.file_name().to_string_lossy().to_string();

            // Extract ID from file name (remove .gz extension if present)
            let id = if file_name.ends_with(".gz") {
                file_name[0..file_name.len() - 3].to_string()
            } else {
                file_name
            };

            // Get metadata path with proper extension
            let metadata_path = self.get_metadata_path(&id);
            if metadata_path.exists() {
                // Read metadata with optional decompression
                let content_bytes = self.read_file_with_decompression(&metadata_path)?;
                let content = String::from_utf8(content_bytes)
                    .map_err(|e| CoreError::IoError(e.to_string()))?;
                let metadata = serde_json::from_str(&content)
                    .map_err(|e| CoreError::JsonError(e.to_string()))?;
                videos.push((id, metadata));
            }
        }

        // Apply pagination
        let start = (page - 1) as usize * page_size as usize;
        let end = start + page_size as usize;
        let paginated = videos.into_iter().skip(start).take(end).collect();

        Ok(paginated)
    }

    async fn list_commentaries(&self, video_id: &str) -> Result<Vec<(String, serde_json::Value)>> {
        let commentaries_dir = self.config.base_dir.join("commentaries");

        // Read all commentary files
        let mut commentaries = Vec::new();
        for entry in
            fs::read_dir(commentaries_dir).map_err(|e| CoreError::IoError(e.to_string()))?
        {
            let entry = entry.map_err(|e| CoreError::IoError(e.to_string()))?;
            let path = entry.path();

            // Read commentary with optional decompression
            let content_bytes = self.read_file_with_decompression(&path)?;
            let content =
                String::from_utf8(content_bytes).map_err(|e| CoreError::IoError(e.to_string()))?;
            let commentary: serde_json::Value =
                serde_json::from_str(&content).map_err(|e| CoreError::JsonError(e.to_string()))?;

            // Check if this commentary belongs to the requested video
            if let Some(vid) = commentary.get("video_id").and_then(|v| v.as_str()) {
                if vid == video_id {
                    let file_name = entry.file_name().to_string_lossy().to_string();
                    // Extract ID from file name (remove .json.gz or .json extension)
                    let id = if file_name.ends_with(".json.gz") {
                        file_name[0..file_name.len() - 8].to_string()
                    } else if file_name.ends_with(".json") {
                        file_name[0..file_name.len() - 5].to_string()
                    } else {
                        file_name
                    };
                    commentaries.push((id, commentary));
                }
            }
        }

        Ok(commentaries)
    }
}

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

    #[tokio::test]
    async fn test_file_storage_new() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();
        assert_eq!(storage.config.base_dir, temp_dir.path());
    }

    #[tokio::test]
    async fn test_store_retrieve_video() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Create a test video file
        let test_video_path = temp_dir.path().join("test_video.mp4");
        std::fs::write(&test_video_path, "test video content").unwrap();

        // Store video
        let metadata = json!({"title": "Test Video", "duration": 60});
        let video_id = storage
            .store_video(&test_video_path, &metadata)
            .await
            .unwrap();

        // Retrieve video
        let retrieved_video = storage.retrieve_video(&video_id).await.unwrap();
        assert_eq!(retrieved_video, b"test video content");

        // Get video metadata
        let retrieved_metadata = storage.get_video_metadata(&video_id).await.unwrap();
        assert_eq!(retrieved_metadata["title"], "Test Video");
        assert_eq!(retrieved_metadata["duration"], 60);
    }

    #[tokio::test]
    async fn test_delete_video() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Create a test video file
        let test_video_path = temp_dir.path().join("test_video.mp4");
        std::fs::write(&test_video_path, "test video content").unwrap();

        // Store video
        let metadata = json!({"title": "Test Video", "duration": 60});
        let video_id = storage
            .store_video(&test_video_path, &metadata)
            .await
            .unwrap();

        // Delete video
        let deleted = storage.delete_video(&video_id).await.unwrap();
        assert!(deleted);

        // Try to retrieve deleted video
        let result = storage.retrieve_video(&video_id).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_store_retrieve_commentary() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Store commentary
        let commentary = json!({
            "video_id": "test-video-id",
            "content": "Test commentary",
            "style": "professional",
            "language": "en"
        });
        let commentary_id = storage.store_commentary(&commentary).await.unwrap();

        // Retrieve commentary
        let retrieved_commentary = storage.retrieve_commentary(&commentary_id).await.unwrap();
        assert_eq!(retrieved_commentary["video_id"], "test-video-id");
        assert_eq!(retrieved_commentary["content"], "Test commentary");
    }

    #[tokio::test]
    async fn test_update_commentary() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Store commentary
        let commentary = json!({
            "video_id": "test-video-id",
            "content": "Test commentary",
            "style": "professional",
            "language": "en"
        });
        let commentary_id = storage.store_commentary(&commentary).await.unwrap();

        // Update commentary
        let updated_commentary = json!({
            "video_id": "test-video-id",
            "content": "Updated commentary",
            "style": "professional",
            "language": "en"
        });
        let updated = storage
            .update_commentary(&commentary_id, &updated_commentary)
            .await
            .unwrap();
        assert!(updated);

        // Retrieve updated commentary
        let retrieved_commentary = storage.retrieve_commentary(&commentary_id).await.unwrap();
        assert_eq!(retrieved_commentary["content"], "Updated commentary");
    }

    #[tokio::test]
    async fn test_delete_commentary() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Store commentary
        let commentary = json!({
            "video_id": "test-video-id",
            "content": "Test commentary",
            "style": "professional",
            "language": "en"
        });
        let commentary_id = storage.store_commentary(&commentary).await.unwrap();

        // Delete commentary
        let deleted = storage.delete_commentary(&commentary_id).await.unwrap();
        assert!(deleted);

        // Try to retrieve deleted commentary
        let result = storage.retrieve_commentary(&commentary_id).await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_list_videos() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Create test video files
        for i in 0..3 {
            let test_video_path = temp_dir.path().join(format!("test_video_{}.mp4", i));
            std::fs::write(&test_video_path, format!("test video content {}", i)).unwrap();

            let metadata = json!({
                "title": format!("Test Video {}", i),
                "duration": 60
            });
            storage
                .store_video(&test_video_path, &metadata)
                .await
                .unwrap();
        }

        // List videos
        let videos = storage.list_videos(1, 10).await.unwrap();
        assert_eq!(videos.len(), 3);

        // Test pagination
        let videos_page_1 = storage.list_videos(1, 2).await.unwrap();
        assert_eq!(videos_page_1.len(), 2);

        let videos_page_2 = storage.list_videos(2, 2).await.unwrap();
        assert_eq!(videos_page_2.len(), 1);
    }

    #[tokio::test]
    async fn test_list_commentaries() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Store commentaries for different videos
        for i in 0..3 {
            let commentary = json!({
                "video_id": "test-video-id",
                "content": format!("Test commentary {}", i),
                "style": "professional",
                "language": "en"
            });
            storage.store_commentary(&commentary).await.unwrap();
        }

        // Store a commentary for a different video
        let other_commentary = json!({
            "video_id": "other-video-id",
            "content": "Other video commentary",
            "style": "professional",
            "language": "en"
        });
        storage.store_commentary(&other_commentary).await.unwrap();

        // List commentaries for test-video-id
        let commentaries = storage.list_commentaries("test-video-id").await.unwrap();
        assert_eq!(commentaries.len(), 3);

        // List commentaries for other-video-id
        let other_commentaries = storage.list_commentaries("other-video-id").await.unwrap();
        assert_eq!(other_commentaries.len(), 1);
    }

    #[tokio::test]
    async fn test_file_storage_with_compression() {
        let temp_dir = tempdir().unwrap();
        let config = StorageConfig {
            base_dir: temp_dir.path().to_path_buf(),
            enable_compression: true,
            ..Default::default()
        };

        let storage = FileStorage::new(config).unwrap();

        // Create a test video file
        let test_video_path = temp_dir.path().join("test_video.mp4");
        std::fs::write(&test_video_path, "test video content with compression").unwrap();

        // Store video with compression
        let metadata = json!({"title": "Test Video", "duration": 60});
        let video_id = storage
            .store_video(&test_video_path, &metadata)
            .await
            .unwrap();

        // Retrieve video with decompression
        let retrieved_video = storage.retrieve_video(&video_id).await.unwrap();
        assert_eq!(retrieved_video, b"test video content with compression");
    }
}