Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Support can be enabled with the new `serde` feature (not enabled by default)
- **Probe**: `Probe::read_bound()` ([PR](https://github.com/Serial-ATA/lofty-rs/pull/557))
- Same as `Probe::read()`, but returns a [`BoundTaggedFile`](https://docs.rs/lofty/latest/lofty/file/struct.BoundTaggedFile.html)
- **Other**: `EXTENSIONS` list containing common file extensions for all supported audio file types ([issue](https://github.com/Serial-ATA/lofty-rs/issues/509)) ([PR](https://github.com/Serial-ATA/lofty-rs/pull/558))
- This is useful for filtering files when scanning directories. If your app uses extension filtering, **please consider switching to this**, as to not
miss any supported files.

### Changed
- **ID3v2**: Check `TXXX:ALBUMARTIST` and `TXXX:ALBUM ARTIST` for `ItemKey::AlbumArtist` conversions
Expand Down
39 changes: 39 additions & 0 deletions lofty/src/file/file_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,44 @@ use crate::tag::TagType;
use std::ffi::OsStr;
use std::path::Path;

/// List of common audio extensions
///
/// This contains a bunch of common extensions for all supported [`FileType`]s, and can be used a filter
/// when scanning directories.
///
/// NOTE: This is **not** an exhaustive list, but it should work fine in most cases.
///
/// # Examples
///
/// ```rust,no_run
/// use lofty::file::EXTENSIONS;
/// use std::fs;
///
/// # fn main() -> lofty::error::Result<()> {
/// for entry in fs::read_dir(".")? {
/// let entry = entry?;
///
/// let path = entry.path();
/// let Some(extension) = path.extension() else {
/// continue;
/// };
///
/// // Skip any non-audio file extensions
/// if !EXTENSIONS.iter().any(|e| *e == extension) {
/// continue;
/// }
///
/// // `entry` is *most likely* a supported file at this point
/// let parsed = lofty::read_from_path(path)?;
/// }
/// # Ok(()) }
/// ```
pub const EXTENSIONS: &[&str] = &[
// Also update `FileType::from_ext()` below
"aac", "ape", "aiff", "aif", "afc", "aifc", "mp3", "mp2", "mp1", "wav", "wv", "opus", "flac",
"ogg", "mp4", "m4a", "m4b", "m4p", "m4r", "m4v", "3gp", "mpc", "mp+", "mpp", "spx",
];

/// The type of file read
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
Expand Down Expand Up @@ -129,6 +167,7 @@ impl FileType {
}
}

// Also update `EXTENSIONS` above
match ext.as_str() {
"aac" => Some(Self::Aac),
"ape" => Some(Self::Ape),
Expand Down
2 changes: 1 addition & 1 deletion lofty/src/file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod file_type;
mod tagged_file;

pub use audio_file::AudioFile;
pub use file_type::FileType;
pub use file_type::{EXTENSIONS, FileType};
pub use tagged_file::{BoundTaggedFile, TaggedFile, TaggedFileExt};

pub(crate) use file_type::FileTypeGuessResult;