|
| 1 | +use std::{fs::OpenOptions, io::Write}; |
| 2 | + |
| 3 | +use varuint::WriteVarint; |
| 4 | +use ytpapi2::YoutubeMusicVideoRef; |
| 5 | + |
| 6 | +use crate::YTLocalDatabase; |
| 7 | + |
| 8 | +impl YTLocalDatabase { |
| 9 | + pub fn write(&self) { |
| 10 | + let db = self.references.read().unwrap(); |
| 11 | + let mut file = OpenOptions::new() |
| 12 | + .write(true) |
| 13 | + .append(false) |
| 14 | + .create(true) |
| 15 | + .truncate(true) |
| 16 | + .open(self.cache_dir.join("db.bin")) |
| 17 | + .unwrap(); |
| 18 | + for video in db.iter() { |
| 19 | + write_video(&mut file, video) |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | +impl YTLocalDatabase { |
| 24 | + pub fn fix_db(&self) { |
| 25 | + let mut db = self.references.write().unwrap(); |
| 26 | + db.clear(); |
| 27 | + let cache_folder = self.cache_dir.join("downloads"); |
| 28 | + if !cache_folder.is_dir() { |
| 29 | + println!( |
| 30 | + "[WARN] The download folder in the cache wasn't found ({:?})", |
| 31 | + cache_folder |
| 32 | + ); |
| 33 | + return; |
| 34 | + } |
| 35 | + for entry in std::fs::read_dir(&cache_folder).unwrap() { |
| 36 | + let entry = entry.unwrap(); |
| 37 | + let path = entry.path(); |
| 38 | + // Check if the file is a json file (+ sloppy check if there is any files or directory) |
| 39 | + if path.extension().unwrap_or_default() != "json" { |
| 40 | + continue; |
| 41 | + } |
| 42 | + // Read the file if not readable do not add it to the database |
| 43 | + let content = match std::fs::read_to_string(&path) { |
| 44 | + Ok(content) => content, |
| 45 | + Err(e) => { |
| 46 | + match std::fs::remove_file(&path) { |
| 47 | + Ok(_) => println!( |
| 48 | + "[INFO] Removing file {:?} because the file is not readable: {e:?}", |
| 49 | + path.file_name() |
| 50 | + ), |
| 51 | + Err(ef) => println!( |
| 52 | + "[ERROR] file {:?} is not readable: {e:?}, but could not be deleted: {ef:?}", |
| 53 | + path.file_name() |
| 54 | + ), |
| 55 | + } |
| 56 | + continue; |
| 57 | + } |
| 58 | + }; |
| 59 | + // Check if the file is a valid json file |
| 60 | + let video = match serde_json::from_str::<YoutubeMusicVideoRef>(&content) { |
| 61 | + Ok(parsed) => parsed, |
| 62 | + Err(e) => { |
| 63 | + match std::fs::remove_file(&path) { |
| 64 | + Ok(_) => println!( |
| 65 | + "[INFO] Removing file {:?} because the file is not a valid json file: {e:?}", |
| 66 | + path.file_name() |
| 67 | + ), |
| 68 | + Err(ef) => println!( |
| 69 | + "[ERROR] file {:?} is not a valid json file: {e:?}, but could not be deleted: {ef:?}", |
| 70 | + path.file_name() |
| 71 | + ), |
| 72 | + } |
| 73 | + continue; |
| 74 | + } |
| 75 | + }; |
| 76 | + // Check if the video file exists |
| 77 | + let video_file = cache_folder.join(format!("{}.mp4", video.video_id)); |
| 78 | + if !video_file.exists() { |
| 79 | + match std::fs::remove_file(&path) { |
| 80 | + Ok(_) => println!( |
| 81 | + "[INFO] Removing file {:?} because the video file does not exist", |
| 82 | + path.file_name() |
| 83 | + ), |
| 84 | + Err(ef) => println!( |
| 85 | + "[ERROR] video assocated to file {:?} does not exist, but the file could not be deleted: {ef:?}", |
| 86 | + path.file_name() |
| 87 | + ), |
| 88 | + } |
| 89 | + continue; |
| 90 | + } |
| 91 | + // Read the video file |
| 92 | + let video_file = match std::fs::read(&video_file) { |
| 93 | + Ok(video_file) => video_file, |
| 94 | + Err(e) => { |
| 95 | + match std::fs::remove_file(&path) { |
| 96 | + Ok(_) => println!( |
| 97 | + "[INFO] Removing file {:?} because the video file is not readable: {e:?}", |
| 98 | + path.file_name() |
| 99 | + ), |
| 100 | + Err(ef) => println!( |
| 101 | + "[ERROR] video associated to file {:?} is not readable: {e:?}, but the file could not be deleted: {ef:?}", |
| 102 | + path.file_name() |
| 103 | + ), |
| 104 | + } |
| 105 | + continue; |
| 106 | + } |
| 107 | + }; |
| 108 | + // Check if the video file contains the header |
| 109 | + if !video_file.starts_with(&[ |
| 110 | + 0, 0, 0, 24, 102, 116, 121, 112, 100, 97, 115, 104, 0, 0, 0, 0, |
| 111 | + ]) { |
| 112 | + match std::fs::remove_file(&path) { |
| 113 | + Ok(_) => println!( |
| 114 | + "[INFO] Removing file {:?} because the video file does not contain the header", |
| 115 | + path.file_name() |
| 116 | + ), |
| 117 | + Err(ef) => println!( |
| 118 | + "[ERROR] video associated to file {:?} does not contain the header, but the file could not be deleted: {ef:?}", |
| 119 | + path.file_name() |
| 120 | + ), |
| 121 | + } |
| 122 | + continue; |
| 123 | + } |
| 124 | + db.push(video); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +/// Writes a video to a file |
| 130 | +pub fn write_video(buffer: &mut impl Write, video: &YoutubeMusicVideoRef) { |
| 131 | + write_str(buffer, &video.title); |
| 132 | + write_str(buffer, &video.author); |
| 133 | + write_str(buffer, &video.album); |
| 134 | + write_str(buffer, &video.video_id); |
| 135 | + write_str(buffer, &video.duration); |
| 136 | +} |
| 137 | + |
| 138 | +/// Writes a string from the cursor |
| 139 | +fn write_str(cursor: &mut impl Write, value: &str) { |
| 140 | + write_u32(cursor, value.len() as u32); |
| 141 | + cursor.write_all(value.as_bytes()).unwrap(); |
| 142 | +} |
| 143 | + |
| 144 | +/// Writes a u32 from the cursor |
| 145 | +fn write_u32(cursor: &mut impl Write, value: u32) { |
| 146 | + cursor.write_varint(value).unwrap(); |
| 147 | +} |
0 commit comments