Skip to content

Commit df141db

Browse files
authored
Merge pull request #45 from Siirko/ini-skip-file
fix hardcoded linux path handling
2 parents 768123b + 4510749 commit df141db

File tree

4 files changed

+32
-23
lines changed

4 files changed

+32
-23
lines changed

src-tauri/src/api/player.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::{
22
fs::File,
3+
path::PathBuf,
34
sync::{Arc, Mutex},
45
};
56

@@ -20,7 +21,8 @@ pub fn import_from_folders(
2021
let mut player = player.lock().unwrap();
2122
let mut total_imported = 0;
2223
for folder in folders {
23-
total_imported += player.import_from_folders(&folder, &app_handle);
24+
let folder_path = PathBuf::from(&folder);
25+
total_imported += player.import_from_folders(folder_path, &app_handle);
2426
}
2527
player.write_to_db(app_handle);
2628
drop(player);

src-tauri/src/db/database.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
};
1414
use log::info;
1515
use rusqlite::{named_params, Connection};
16-
use std::fs;
16+
use std::{fs, path::Path};
1717
use tauri::AppHandle;
1818

1919
const CURRENT_DB_VERSION: u32 = 1;
@@ -73,7 +73,7 @@ pub fn add_audio(audio: &_Audio, db: &Connection) -> Result<(), rusqlite::Error>
7373
"@album": audio.tag.album,
7474
"@genre": audio.tag.genre,
7575
})?;
76-
let folder = audio.path.rsplit_once('/').unwrap().0;
76+
let folder = Path::new(&audio.path).parent().unwrap().to_str().unwrap();
7777
let mut folder_statement = db.prepare(insert::folder::FOLDER_INSERT)?;
7878
folder_statement.execute(named_params! { "@path": folder })?;
7979

src-tauri/src/music/audio.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use file_format::{FileFormat, Kind};
33
use lofty::{Accessor, AudioFile, Probe, Tag, TagType, TaggedFile, TaggedFileExt};
44
use log::{error, info};
55
use rodio::Decoder;
6-
use std::path::PathBuf;
6+
use std::path::{Path, PathBuf};
77
use std::time::Instant;
88
use std::{fs::File, io::BufReader, time::Duration};
99
use 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

202202
fn 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);

src-tauri/src/music/player.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use log::info;
22
use rodio::Sink;
3-
use std::{collections::HashMap, time::Duration};
3+
use std::{collections::HashMap, path::PathBuf, time::Duration};
44

55
use crate::{database, db::state::DbAccess, music::audio::*, update_status};
66
use rand::prelude::*;
@@ -20,7 +20,7 @@ pub struct MusicPlayer {
2020
pub trait Player {
2121
fn new(stream_handler: rodio::OutputStreamHandle) -> Self;
2222
fn add_audio(&mut self, audio: _Audio) -> bool;
23-
fn import_from_folders(&mut self, path: &str, app_handle: &AppHandle) -> usize;
23+
fn import_from_folders(&mut self, path: PathBuf, app_handle: &AppHandle) -> usize;
2424
fn import_from_db(&mut self, app_handle: &AppHandle) -> Result<usize, rusqlite::Error>;
2525
fn set_index(&mut self, index: usize);
2626
fn update_total_time(&mut self);
@@ -89,8 +89,8 @@ impl Player for MusicPlayer {
8989
true
9090
}
9191

92-
fn import_from_folders(&mut self, path: &str, app_handle: &AppHandle) -> usize {
93-
info!("Importing from {}", path);
92+
fn import_from_folders(&mut self, path: PathBuf, app_handle: &AppHandle) -> usize {
93+
info!("Importing from {}", path.display());
9494
let value = get_audios(self.audios.as_mut(), path, app_handle);
9595
self.audios.sort_by(|a, b| a.path.cmp(&b.path));
9696
self.playlists

0 commit comments

Comments
 (0)