@@ -3,7 +3,7 @@ use file_format::{FileFormat, Kind};
33use lofty:: { Accessor , AudioFile , Probe , Tag , TagType , TaggedFile , TaggedFileExt } ;
44use log:: { error, info} ;
55use rodio:: Decoder ;
6- use std:: path:: PathBuf ;
6+ use std:: path:: { Path , PathBuf } ;
77use std:: time:: Instant ;
88use std:: { fs:: File , io:: BufReader , time:: Duration } ;
99use tauri:: AppHandle ;
@@ -140,7 +140,7 @@ impl std::cmp::PartialEq for _Audio {
140140 }
141141}
142142
143- fn gen_tag ( path : & str ) -> TaggedFile {
143+ fn gen_tag ( path : & PathBuf ) -> TaggedFile {
144144 let tagged_file = Probe :: open ( path) ;
145145 match tagged_file {
146146 Ok ( tagged_file) => match tagged_file. read ( ) {
@@ -160,8 +160,8 @@ fn gen_tag(path: &str) -> TaggedFile {
160160 }
161161}
162162
163- pub fn create_audio ( path : & str , format : FileFormat ) -> _Audio {
164- let tagged_file = gen_tag ( path) ;
163+ pub fn create_audio ( path : PathBuf , format : FileFormat ) -> _Audio {
164+ let tagged_file = gen_tag ( & path) ;
165165 let properties = tagged_file. properties ( ) ;
166166 let duration = properties. duration ( ) ;
167167 let time = parse ( & ( duration. as_secs ( ) . to_string ( ) + "s" ) ) . unwrap ( ) ;
@@ -178,7 +178,7 @@ pub fn create_audio(path: &str, format: FileFormat) -> _Audio {
178178 None => Vec :: new ( ) ,
179179 } ;
180180 _Audio {
181- path : path. to_string ( ) ,
181+ path : path. as_os_str ( ) . to_str ( ) . unwrap ( ) . to_string ( ) ,
182182 duration : time,
183183 format : match format. short_name ( ) {
184184 Some ( name) => name. to_string ( ) ,
@@ -189,7 +189,7 @@ pub fn create_audio(path: &str, format: FileFormat) -> _Audio {
189189 title : tag
190190 . title ( )
191191 . as_deref ( )
192- . unwrap_or ( path. split ( '/' ) . last ( ) . unwrap ( ) )
192+ . unwrap_or ( path. file_stem ( ) . unwrap ( ) . to_str ( ) . unwrap_or ( "Unknown" ) )
193193 . to_string ( ) ,
194194 artist : tag. artist ( ) . as_deref ( ) . unwrap_or ( "Unknown" ) . to_string ( ) ,
195195 album : tag. album ( ) . as_deref ( ) . unwrap_or ( "Unknown" ) . to_string ( ) ,
@@ -201,19 +201,20 @@ pub fn create_audio(path: &str, format: FileFormat) -> _Audio {
201201
202202fn check_audio_format (
203203 format : FileFormat ,
204- p : & str ,
204+ p : & Path ,
205205 audios : & mut Vec < _Audio > ,
206206 app_handle : & AppHandle ,
207207 count : & mut usize ,
208208) -> bool {
209209 match format. kind ( ) {
210210 Kind :: Audio => {
211- match app_handle. db ( |db| database:: is_audio_in_db ( db, p) ) {
211+ match app_handle. db ( |db| database:: is_audio_in_db ( db, p. as_os_str ( ) . to_str ( ) . unwrap ( ) ) )
212+ {
212213 Ok ( true ) => {
213214 info ! ( "Audio already in db" ) ;
214215 }
215216 Ok ( false ) => {
216- let audio = create_audio ( p, format) ;
217+ let audio = create_audio ( p. to_path_buf ( ) , format) ;
217218 app_handle. db ( |db| database:: add_audio ( & audio, db) ) . unwrap ( ) ;
218219 audios. push ( audio) ;
219220 * count += 1 ;
@@ -229,9 +230,9 @@ fn check_audio_format(
229230 }
230231}
231232
232- pub fn get_audios ( audios : & mut Vec < _Audio > , path : & str , app_handle : & AppHandle ) -> usize {
233+ pub fn get_audios ( audios : & mut Vec < _Audio > , pathbuf : PathBuf , app_handle : & AppHandle ) -> usize {
233234 let mut count = 0 ;
234- let paths = std:: fs:: read_dir ( PathBuf :: from ( path ) ) ;
235+ let paths = std:: fs:: read_dir ( pathbuf ) ;
235236 info ! ( "Init dir read" ) ;
236237 match paths {
237238 Ok ( paths) => {
@@ -242,10 +243,10 @@ pub fn get_audios(audios: &mut Vec<_Audio>, path: &str, app_handle: &AppHandle)
242243 let p = & path. path ( ) . into_os_string ( ) . into_string ( ) ;
243244 match p {
244245 Ok ( p) => {
245- // if p.contains(".ini") {
246- // // see https://github.com/mmalecot/file-format/issues/36
247- // continue;
248- // }
246+ if p. contains ( ".ini" ) {
247+ // see https://github.com/mmalecot/file-format/issues/36
248+ continue ;
249+ }
249250 if audios. iter ( ) . any ( |audio| audio. path == * p) {
250251 info ! ( "Audio already in list" ) ;
251252 continue ;
@@ -259,7 +260,13 @@ pub fn get_audios(audios: &mut Vec<_Audio>, path: &str, app_handle: &AppHandle)
259260 continue ;
260261 }
261262 } ;
262- check_audio_format ( format, p, audios, app_handle, & mut count) ;
263+ check_audio_format (
264+ format,
265+ & PathBuf :: from ( p) ,
266+ audios,
267+ app_handle,
268+ & mut count,
269+ ) ;
263270 }
264271 Err ( e) => {
265272 error ! ( "{:?}" , e) ;
0 commit comments