@@ -9,24 +9,40 @@ fn main() -> Result<()> {
99
1010 log:: info!( "=== Media Library Demo ===" ) ;
1111
12- // Step 1: Create a media library
13- log:: info!( "Step 1: Creating media library..." ) ;
14- let mut library = MediaLibrary :: new ( ) ;
15-
16- // Step 2: Create a media cache for thumbnails and metadata
17- log:: info!( "Step 2: Creating media cache..." ) ;
18- let cache_dir = PathBuf :: from ( "tmp" ) . join ( ".cache" ) ;
19- let mut cache = video_editor:: media:: MediaCache :: new ( cache_dir) ?
20- . with_thumbnail_size ( 320 , 180 )
21- . with_max_age ( std:: time:: Duration :: from_secs ( 86400 ) ) ;
22-
23- // Load existing cache index
24- let cache_index_path = PathBuf :: from ( "tmp" ) . join ( ".cache" ) . join ( "index.json" ) ;
25- if let Err ( e) = cache. load_cache_index ( & cache_index_path) {
26- log:: debug!( "No existing cache index found (this is normal for first run). error: {e}" ) ;
12+ // Step 1: Create a media library with integrated cache
13+ log:: info!( "Step 1: Creating media library with cache..." ) ;
14+ let cache_dir = PathBuf :: from ( "tmp" ) . join ( "cache" ) ;
15+ let mut library = MediaLibrary :: new ( ) . with_cache_configured (
16+ cache_dir. clone ( ) ,
17+ 320 ,
18+ 180 ,
19+ std:: time:: Duration :: from_secs ( 86400 ) ,
20+ ) ?;
21+ log:: info!( " Cache directory: {:?}" , cache_dir) ;
22+
23+ // Step 2: Demonstrate add_file (auto-detect media type and generate thumbnail)
24+ log:: info!( "Step 2: Adding files with auto-detection..." ) ;
25+ let test_files = vec ! [ "data/video1.mp4" , "data/video2.mp4" , "data/song1.mp3" ] ;
26+
27+ for file_path in & test_files {
28+ match library. add_file ( PathBuf :: from ( file_path) ) {
29+ Ok ( id) => {
30+ if let Some ( item) = library. get_item ( & id) {
31+ log:: info!( " ✓ Added: {} (ID: {})" , item. name, id) ;
32+ log:: info!( " Type: {}" , item. media_type. as_str( ) ) ;
33+ log:: info!( " Duration: {}" , item. format_duration( ) ) ;
34+ if let Some ( ref thumbnail) = item. thumbnail_path {
35+ log:: info!( " Thumbnail: {}" , thumbnail. display( ) ) ;
36+ }
37+ }
38+ }
39+ Err ( e) => {
40+ log:: warn!( " ✗ Failed to add {}: {}" , file_path, e) ;
41+ }
42+ }
2743 }
2844
29- // Step 3: Configure import options
45+ // Step 3: Configure import options for bulk import
3046 log:: info!( "Step 3: Configuring import options..." ) ;
3147 let import_options = ImportOptions :: new ( )
3248 . with_import_thumbnails ( true )
@@ -37,25 +53,25 @@ fn main() -> Result<()> {
3753 log:: info!( "Step 4: Importing media files from data/ directory..." ) ;
3854 let data_dir = PathBuf :: from ( "data" ) ;
3955
40- if !data_dir. exists ( ) {
56+ if data_dir. exists ( ) {
57+ let mut importer = MediaImporter :: new ( import_options) ;
58+ // Note: importer doesn't need separate cache anymore, library has it
59+ let import_results = importer. import_directory ( & data_dir, & mut library) ?;
60+
61+ // Display import results
62+ log:: info!( "Import Results:" ) ;
63+ log:: info!( " Total files processed: {}" , import_results. len( ) ) ;
64+ let successful = import_results. iter ( ) . filter ( |r| r. success ) . count ( ) ;
65+ let failed = import_results. iter ( ) . filter ( |r| !r. success ) . count ( ) ;
66+ log:: info!( " Successful: {}" , successful) ;
67+ log:: info!( " Failed: {}" , failed) ;
68+ } else {
4169 log:: warn!( "Data directory does not exist: {}" , data_dir. display( ) ) ;
4270 log:: info!(
4371 "Please add some media files (mp4, mkv, mp3, wav, jpg, png, srt) to the data/ directory"
4472 ) ;
45- return Ok ( ( ) ) ;
4673 }
4774
48- let mut importer = MediaImporter :: new ( import_options) ;
49- let import_results = importer. import_directory ( & data_dir, & mut library, & mut cache) ?;
50-
51- // Display import results
52- log:: info!( "Import Results:" ) ;
53- log:: info!( " Total files processed: {}" , import_results. len( ) ) ;
54- let successful = import_results. iter ( ) . filter ( |r| r. success ) . count ( ) ;
55- let failed = import_results. iter ( ) . filter ( |r| !r. success ) . count ( ) ;
56- log:: info!( " Successful: {}" , successful) ;
57- log:: info!( " Failed: {}" , failed) ;
58-
5975 // Step 5: Display library statistics
6076 log:: info!( "=== Library Statistics ===" ) ;
6177 log:: info!( "Total items: {}" , library. item_count( ) ) ;
@@ -182,14 +198,42 @@ fn main() -> Result<()> {
182198 log:: info!( " - {} ({} bytes)" , item. name, item. file_size) ;
183199 }
184200
185- // Step 12: Save cache index
201+ // Step 12: Cache management (now integrated into MediaLibrary)
186202 log:: info!( "=== Cache Management Demo ===" ) ;
187- log:: info!( "Cache size: {} entries" , cache. cache_size( ) ) ;
188- let cache_index_path = PathBuf :: from ( "tmp" ) . join ( ".cache" ) . join ( "index.json" ) ;
189- if let Err ( e) = cache. save_cache_index ( & cache_index_path) {
190- log:: warn!( "Failed to save cache index: {}" , e) ;
191- } else {
192- log:: info!( "Cache index saved to: {}" , cache_index_path. display( ) ) ;
203+ log:: info!( "Cache size: {} entries" , library. cache_size( ) ) ;
204+
205+ // Cleanup expired cache entries
206+ library. cleanup_cache ( ) ?;
207+ log:: info!( "Cache cleanup complete" ) ;
208+
209+ // Step 13: Serialization and deserialization test
210+ log:: info!( "=== Serialization/Deserialization Demo ===" ) ;
211+ let json = library. to_json ( true ) ?;
212+ log:: info!( " Serialized library to JSON ({} bytes)" , json. len( ) ) ;
213+
214+ // Test deserialization and cache rebuilding
215+ match MediaLibrary :: from_json ( & json) {
216+ Ok ( restored_library) => {
217+ log:: info!( " Successfully restored library from JSON" ) ;
218+ log:: info!(
219+ " Restored library has {} items" ,
220+ restored_library. item_count( )
221+ ) ;
222+ log:: info!( " Cache is configured: {}" , restored_library. has_cache( ) ) ;
223+
224+ // Verify thumbnails are preserved
225+ for item in restored_library. all_items ( ) . iter ( ) . take ( 3 ) {
226+ let thumb_status = if item. thumbnail_path . is_some ( ) {
227+ "has thumbnail" . to_string ( )
228+ } else {
229+ "no thumbnail" . to_string ( )
230+ } ;
231+ log:: info!( " {} - {}" , item. name, thumb_status) ;
232+ }
233+ }
234+ Err ( e) => {
235+ log:: warn!( " Failed to restore library: {}" , e) ;
236+ }
193237 }
194238
195239 log:: info!( "=== Demo Complete ===" ) ;
0 commit comments