Skip to content

Commit

Permalink
feat(workspace): detail apply mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Yesterday17 committed Mar 23, 2024
1 parent 761155c commit e767cc3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 25 deletions.
44 changes: 30 additions & 14 deletions anni-repo/src/models/album.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,14 @@ impl Album {
/// Apply album metadata to a directory formatted with strict album format.
///
/// This function applies both metadata and cover.
///
/// The argument `detailed` determines whether to write metadata(such as title and artist) and cover to flac files.
#[cfg(feature = "apply")]
pub fn apply_strict<P>(&self, directory: P) -> Result<(), crate::error::AlbumApplyError>
pub fn apply_strict<P>(
&self,
directory: P,
detailed: bool,
) -> Result<(), crate::error::AlbumApplyError>
where
P: AsRef<std::path::Path>,
{
Expand Down Expand Up @@ -319,29 +325,39 @@ impl Album {

// let mut modified = false;
// no comment block exist, or comments is not correct
// TODO: the comparison is not accurate if `detailed` = false
if comments.is_none() || comments.unwrap().to_string() != meta {
let comments = flac.comments_mut();
comments.clear();
comments.push(UserComment::title(track.title()));
comments.push(UserComment::album(disc.title()));
comments.push(UserComment::artist(track.artist()));
comments.push(UserComment::date(self.release_date()));

if detailed {
comments.push(UserComment::title(track.title()));
comments.push(UserComment::album(disc.title()));
comments.push(UserComment::artist(track.artist()));
comments.push(UserComment::date(self.release_date()));
}
comments.push(UserComment::track_number(track_num));
comments.push(UserComment::track_total(track_total));
comments.push(UserComment::disc_number(disc_num));
comments.push(UserComment::disc_total(disc_total));
// modified = true;
}

// TODO: do not modify flac file if embed cover is the same as the one in folder
let cover_path = file.with_file_name("cover.jpg");
let picture =
BlockPicture::new(cover_path, PictureType::CoverFront, String::new())?;
flac.blocks
.retain(|block| !matches!(block.data, MetadataBlockData::Picture(_)));
flac.blocks
.push(MetadataBlock::new(MetadataBlockData::Picture(picture)));
// modified = true;
if detailed {
// TODO: do not modify flac file if embed cover is the same as the one in folder
let cover_path = file.with_file_name("cover.jpg");
let picture =
BlockPicture::new(cover_path, PictureType::CoverFront, String::new())?;
flac.blocks
.retain(|block| !matches!(block.data, MetadataBlockData::Picture(_)));
flac.blocks
.push(MetadataBlock::new(MetadataBlockData::Picture(picture)));
// modified = true;
} else {
// remove cover block
flac.blocks
.retain(|block| !matches!(block.data, MetadataBlockData::Picture(_)));
}

// if modified {
flac.save::<String>(None)?;
Expand Down
4 changes: 2 additions & 2 deletions anni-workspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ impl AnniWorkspace {
Ok(())
}

pub fn apply_tags<P>(&self, album_path: P) -> Result<(), WorkspaceError>
pub fn apply_tags<P>(&self, album_path: P, detailed: bool) -> Result<(), WorkspaceError>
where
P: AsRef<Path>,
{
Expand All @@ -624,7 +624,7 @@ impl AnniWorkspace {
let album = repo
.album(&album_id)
.expect("Album not found in metadata repository");
album.apply_strict(controlled_album_path)?;
album.apply_strict(controlled_album_path, detailed)?;

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion anni/src/subcommands/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub fn library_apply_tag(
.albums()
.get(&Uuid::parse_str(folder_name.as_ref())?)
.ok_or_else(|| anyhow::anyhow!("Album {} not found", folder_name))?;
album.apply_strict(&path)?;
album.apply_strict(&path, true)?;
} else if let Ok(AlbumFolderInfo {
release_date,
catalog,
Expand Down
9 changes: 3 additions & 6 deletions anni/src/subcommands/workspace/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use std::path::PathBuf;
pub struct WorkspacePublishAction {
// #[clap(long)]
// copy: bool,
#[clap(short = 'w', long = "write")]
write: bool,
#[clap(long)]
detailed: bool,

#[clap(short = 'u', long = "uuid")]
parse_path_as_uuid: bool,
Expand Down Expand Up @@ -58,10 +58,7 @@ pub async fn handle_workspace_publish(mut me: WorkspacePublishAction) -> anyhow:
.collect();

for path in me.path {
if me.write {
workspace.apply_tags(&path)?;
}

workspace.apply_tags(&path, me.detailed)?;
workspace.publish(path, me.soft)?;
}
Ok(())
Expand Down
7 changes: 5 additions & 2 deletions anni/src/subcommands/workspace/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@ use std::path::PathBuf;

#[derive(Args, Debug, Clone)]
pub struct WorkspaceUpdateAction {
pub path: PathBuf,
#[clap(long)]
detailed: bool,

path: PathBuf,
}

#[handler(WorkspaceUpdateAction)]
pub async fn handle_workspace_update(me: WorkspaceUpdateAction) -> anyhow::Result<()> {
let workspace = AnniWorkspace::new()?;
workspace.apply_tags(me.path)?;
workspace.apply_tags(me.path, me.detailed)?;

Ok(())
}

0 comments on commit e767cc3

Please sign in to comment.