use std::path::Path;
use crate::format::Origin;
pub(crate) fn check(origin: Origin, folder_path: Option<&str>) -> Option<bool> {
match origin {
Origin::Local => Some(Path::new(folder_path?).exists()),
Origin::Cloud | Origin::Stream => None,
}
}
pub(crate) fn online_only(path: &Path) -> bool {
let Ok(meta) = std::fs::metadata(path) else {
return false;
};
#[cfg(target_os = "macos")]
{
use std::os::macos::fs::MetadataExt;
const SF_DATALESS: u32 = 0x4000_0000;
meta.st_flags() & SF_DATALESS != 0
}
#[cfg(target_os = "windows")]
{
use std::os::windows::fs::MetadataExt;
const FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS: u32 = 0x0040_0000;
meta.file_attributes() & FILE_ATTRIBUTE_RECALL_ON_DATA_ACCESS != 0
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn only_a_local_row_gets_an_answer() {
assert_eq!(check(Origin::Cloud, Some("/contents_1/a.flac")), None);
assert_eq!(check(Origin::Stream, Some("soundcloud:tracks:1")), None);
assert_eq!(check(Origin::Local, None), None);
}
#[test]
fn a_local_path_is_answered_from_the_filesystem() {
let here = concat!(env!("CARGO_MANIFEST_DIR"), "/src/presence.rs");
assert_eq!(check(Origin::Local, Some(here)), Some(true));
assert_eq!(
check(Origin::Local, Some("/nope/definitely/not/here.flac")),
Some(false)
);
}
#[test]
fn another_machines_path_is_missing_not_an_error() {
assert_eq!(
check(Origin::Local, Some("C:/users/someone/Music/a.mp3")),
Some(false)
);
}
}