Downloads land in Albums, Playlists or Tracks folders

This commit is contained in:
Christian
2026-11-19 01:23:06 -05:00
parent 9de1d3f117
commit 6c2bbdf15b
3 changed files with 44 additions and 10 deletions
+24 -5
View File
@@ -39,6 +39,8 @@ pub struct JobRequest {
pub trim: Option<Trim>,
#[serde(default)]
pub playlist: Option<String>,
#[serde(default)]
pub kind: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
@@ -654,11 +656,17 @@ async fn cleanup(dir: &Path, stem: &str) {
}
}
fn target_dir(settings: &Settings, req: &JobRequest) -> PathBuf {
pub fn target_dir(settings: &Settings, req: &JobRequest) -> PathBuf {
let base = PathBuf::from(&settings.output_dir);
match (&req.folder, settings.collection_folders) {
(Some(name), true) if !name.trim().is_empty() => base.join(sanitize(name)),
_ => base,
let named = req.folder.as_deref().map(str::trim).filter(|n| !n.is_empty() && settings.collection_folders);
match (req.kind.as_deref(), named) {
(Some("album"), Some(name)) => base.join("Albums").join(sanitize(name)),
(Some("playlist"), Some(name)) => {
let lists = PathBuf::from(&settings.playlists_dir);
if settings.playlists_dir.trim().is_empty() { base.join("Playlists") } else { lists }.join(sanitize(name))
}
(None, Some(name)) => base.join(sanitize(name)),
_ => base.join("Tracks"),
}
}
@@ -764,11 +772,22 @@ mod tests {
assert!(plan(&entry(Some(128), false, vec![720]), &save(SaveAs::Mp3, 320, 0), false).is_err());
}
#[test]
fn sorts_downloads_into_albums_playlists_and_tracks() {
let settings = Settings { output_dir: "M".into(), playlists_dir: "M/Playlists".into(), collection_folders: true, ..Default::default() };
let req = |kind: Option<&str>, folder: Option<&str>| JobRequest { entry: entry(None, false, vec![]), meta: TagFields::default(), folder: folder.map(String::from), trim: None, playlist: None, kind: kind.map(String::from) };
assert_eq!(target_dir(&settings, &req(Some("album"), Some("Luxury"))), PathBuf::from("M").join("Albums").join("Luxury"));
assert_eq!(target_dir(&settings, &req(Some("playlist"), Some("Late night"))), PathBuf::from("M/Playlists").join("Late night"));
assert_eq!(target_dir(&settings, &req(Some("track"), None)), PathBuf::from("M").join("Tracks"));
let flat = Settings { collection_folders: false, ..settings.clone() };
assert_eq!(target_dir(&flat, &req(Some("album"), Some("Luxury"))), PathBuf::from("M").join("Tracks"));
}
#[test]
fn builds_stems_and_escapes_templates() {
let settings = Settings { output_dir: "out".into(), ..Default::default() };
let meta = TagFields { title: "100% Night".into(), track_number: Some(3), track_total: Some(120), ..Default::default() };
let req = JobRequest { entry: entry(None, false, vec![]), meta, folder: Some("Nova - Glass: Vol 1".into()), trim: None, playlist: None };
let req = JobRequest { entry: entry(None, false, vec![]), meta, folder: Some("Nova - Glass: Vol 1".into()), trim: None, playlist: None, kind: None };
let stem = file_stem(&settings, &req);
assert_eq!(stem, "003 - Nova - 100% Night");
assert_eq!(template_escape(&stem), "003 - Nova - 100%% Night");
+19 -4
View File
@@ -628,8 +628,22 @@ function finalMeta(c, e) {
function folderFor(c) {
if (c.data.single || c.releaseType === "search") return null;
if (isRelease(c)) return [c.album.artist, c.album.title].filter(Boolean).join(" - ");
return c.album.title;
return c.album.title || null;
}
function kindFor(c) {
if (c.data.single || c.releaseType === "search") return "track";
return isRelease(c) ? "album" : "playlist";
}
function destFor(c) {
const s = state.settings;
const sep = s.outputDir.includes("/") ? "/" : "\\";
const folder = s.collectionFolders ? folderFor(c) : null;
const kind = kindFor(c);
if (folder && kind === "album") return [s.outputDir, "Albums", folder].join(sep);
if (folder && kind === "playlist") return [s.playlistsDir || [s.outputDir, "Playlists"].join(sep), folder].join(sep);
return [s.outputDir, "Tracks"].join(sep);
}
function selectedEntries(c) {
@@ -1214,7 +1228,7 @@ function updateCommit() {
sel.replaceChildren(h("option", { value: "" }, "No playlist"), ...lists.map((p) => h("option", { value: p.name }, `+ ${p.name}`)));
sel.value = lists.some((p) => p.name === keep) ? keep : "";
sel.hidden = lists.length === 0;
$("#destPath").textContent = folder ? `${state.settings.outputDir}${sep}${folder}` : state.settings.outputDir;
$("#destPath").textContent = destFor(c);
$("#attestText").textContent = n > 1 ? `I have the rights to these ${n}` : "I have the rights to this";
const btn = $("#downloadBtn");
btn.disabled = n === 0 || !state.tools?.ytdlp.path;
@@ -1237,7 +1251,8 @@ async function startDownload() {
const ents = selectedEntries(c);
const folder = state.settings.collectionFolders ? folderFor(c) : null;
const playlist = $("#destPlaylist").value || null;
const jobs = ents.map((e) => ({ entry: e, meta: finalMeta(c, e), folder, trim: c.trims.get(e.index) || null, playlist }));
const kind = kindFor(c);
const jobs = ents.map((e) => ({ entry: e, meta: finalMeta(c, e), folder, trim: c.trims.get(e.index) || null, playlist, kind }));
const s = state.settings;
try {
const ids = await invoke("enqueue", { jobs, save: { saveAs: s.saveAs, mp3Bitrate: s.mp3Bitrate, videoHeight: s.videoHeight, videoContainer: s.videoContainer }, attested: true });
+1 -1
View File
@@ -185,7 +185,7 @@
<div><strong>Save to</strong><span id="setDir"></span></div>
<button class="btn" id="setDirBtn">Change…</button>
</div>
<label class="setting"><div><strong>Give albums and playlists their own folder</strong><span>Named “Artist - Album”, or after the playlist.</span></div><input type="checkbox" class="switch" data-key="collectionFolders" /></label>
<label class="setting"><div><strong>Give albums and playlists their own folder</strong><span>Albums go in Albums, playlists in Playlists (so they show up as playlists), and single songs in Tracks.</span></div><input type="checkbox" class="switch" data-key="collectionFolders" /></label>
<label class="setting"><div><strong>Number tracks in albums and playlists</strong><span>Keeps files sorted in their original order.</span></div><input type="checkbox" class="switch" data-key="numberTracks" /></label>
<div class="setting stack">
<div><strong>File names</strong><span>Click a token to add it. Preview: <code id="templatePreview"></code></span></div>