fix(finalizeMedia): scope subtitles to episode baseName and handle empty season/series folder cleanup
Build / Lint & Test (push) Successful in 18s
Build / Lint & Test (push) Successful in 18s
This commit is contained in:
1 parent
86448988ed
commit
30fb556af7
2 files changed
+163
-10
No files matched your search
@@ -25,12 +25,67 @@ const details = () => ({
|
||||
inputUI: { type: 'dropdown', options: ['true', 'false'] },
|
||||
tooltip: 'Remove the original source directory only when it is empty.',
|
||||
},
|
||||
{
|
||||
label: 'Remove Empty Parent Series Directory',
|
||||
name: 'removeEmptyParentSeriesDir',
|
||||
type: 'boolean',
|
||||
defaultValue: 'true',
|
||||
inputUI: { type: 'dropdown', options: ['true', 'false'] },
|
||||
tooltip: 'If the removed source directory was a season folder, also remove the parent series directory if it is empty.',
|
||||
},
|
||||
],
|
||||
outputs: [{ number: 1, tooltip: 'Finalization complete' }],
|
||||
});
|
||||
|
||||
const LANGUAGE_CODES = { it: 'ita', en: 'eng' };
|
||||
|
||||
const IGNORED_JUNK_FILES = new Set([
|
||||
'.ds_store',
|
||||
'thumbs.db',
|
||||
'desktop.ini',
|
||||
'ehthumbs.db',
|
||||
'ehthumbs_vista.db',
|
||||
]);
|
||||
|
||||
const SEASON_DIR_REGEX = /^(season|stagione|staffel|saison|s)?\s*\d+$/i;
|
||||
const SPECIALS_DIR_REGEX = /^(specials?|extras?)$/i;
|
||||
|
||||
const isSeasonDirectory = (dirName) => {
|
||||
const trimmed = (dirName || '').trim();
|
||||
return SEASON_DIR_REGEX.test(trimmed) || SPECIALS_DIR_REGEX.test(trimmed);
|
||||
};
|
||||
|
||||
const isDirEmptyIgnoringJunk = (dirPath) => {
|
||||
if (!fs.existsSync(dirPath)) return false;
|
||||
try {
|
||||
const entries = fs.readdirSync(dirPath);
|
||||
const significant = entries.filter((e) => !IGNORED_JUNK_FILES.has(e.toLowerCase()));
|
||||
return significant.length === 0;
|
||||
} catch (_) {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const removeDirAndJunk = (dirPath, jobLog) => {
|
||||
if (!fs.existsSync(dirPath)) return false;
|
||||
try {
|
||||
const entries = fs.readdirSync(dirPath);
|
||||
for (const entry of entries) {
|
||||
if (IGNORED_JUNK_FILES.has(entry.toLowerCase())) {
|
||||
try {
|
||||
fs.unlinkSync(path.join(dirPath, entry));
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
fs.rmdirSync(dirPath);
|
||||
if (jobLog) jobLog(`[finalizeMedia] Removed empty directory: ${dirPath}\n`);
|
||||
return true;
|
||||
} catch (err) {
|
||||
if (jobLog) jobLog(`[finalizeMedia] Could not remove directory ${dirPath}: ${err.message}\n`);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const normalizeSubtitleParts = (parts) => {
|
||||
const normalized = [];
|
||||
for (const part of parts) {
|
||||
@@ -65,6 +120,11 @@ const normalizeSubtitleName = (fileName) => {
|
||||
return `${prefix.concat(suffix).join('.')}${ext}`;
|
||||
};
|
||||
|
||||
const isMediaSubtitle = (fileName, mediaBaseName) => {
|
||||
if (!/\.srt$/i.test(fileName)) return false;
|
||||
return fileName.toLowerCase().startsWith(`${mediaBaseName.toLowerCase()}.`);
|
||||
};
|
||||
|
||||
const subtitleKey = (fileName) => {
|
||||
const stem = fileName.slice(0, -path.extname(fileName).length);
|
||||
return stem.replace(/_chapters(?=\.|$)/i, '').toLowerCase();
|
||||
@@ -116,8 +176,9 @@ const plugin = async (args) => {
|
||||
throw new Error(`[finalizeMedia] Final directory does not exist: ${finalDir}`);
|
||||
}
|
||||
|
||||
// Filter only subtitles that belong to this specific media/episode file
|
||||
const files = fs.readdirSync(finalDir)
|
||||
.filter((file) => /\.srt$/i.test(file))
|
||||
.filter((file) => isMediaSubtitle(file, mediaBaseName))
|
||||
.map((file) => ({ file, fullPath: path.join(finalDir, file) }));
|
||||
|
||||
for (const entry of files) {
|
||||
@@ -137,9 +198,10 @@ const plugin = async (args) => {
|
||||
entry.fullPath = destination;
|
||||
}
|
||||
|
||||
// Group and deduplicate only subtitles belonging to this specific media file
|
||||
const grouped = new Map();
|
||||
for (const entry of fs.readdirSync(finalDir)
|
||||
.filter((file) => /\.srt$/i.test(file))
|
||||
.filter((file) => isMediaSubtitle(file, mediaBaseName))
|
||||
.map((file) => ({ file, fullPath: path.join(finalDir, file) }))) {
|
||||
const key = subtitleKey(entry.file);
|
||||
if (!grouped.has(key)) grouped.set(key, []);
|
||||
@@ -191,15 +253,31 @@ const plugin = async (args) => {
|
||||
jobLog(`[finalizeMedia] Renamed chapter XML: ${entry.file} -> ${path.basename(canonicalChapter)}\n`);
|
||||
}
|
||||
|
||||
if ((args.inputs.removeEmptySourceDir || 'true') === 'true'
|
||||
&& sourceDir && path.resolve(sourceDir) !== path.resolve(finalDir)
|
||||
// Remove source directory when empty (e.g. when transcoding from a staging/ingest directory).
|
||||
// For series (where sourceDir is e.g. Season 01), it will only be removed when the last episode is processed.
|
||||
// When the season folder is removed, if the parent Series directory is also empty, it is also cleaned up.
|
||||
const removeEmptySourceDir = (args.inputs.removeEmptySourceDir || 'true') === 'true';
|
||||
const removeEmptyParentSeriesDir = (args.inputs.removeEmptyParentSeriesDir || 'true') === 'true';
|
||||
|
||||
if (removeEmptySourceDir
|
||||
&& sourceDir
|
||||
&& path.resolve(sourceDir) !== path.resolve(finalDir)
|
||||
&& fs.existsSync(sourceDir)) {
|
||||
const remaining = fs.readdirSync(sourceDir);
|
||||
if (remaining.length === 0) {
|
||||
fs.rmdirSync(sourceDir);
|
||||
jobLog(`[finalizeMedia] Removed empty source directory: ${sourceDir}\n`);
|
||||
if (isDirEmptyIgnoringJunk(sourceDir)) {
|
||||
const isSeason = isSeasonDirectory(path.basename(sourceDir));
|
||||
const parentDir = path.dirname(sourceDir);
|
||||
|
||||
const removed = removeDirAndJunk(sourceDir, jobLog);
|
||||
if (removed && isSeason && removeEmptyParentSeriesDir) {
|
||||
if (parentDir && parentDir !== sourceDir && isDirEmptyIgnoringJunk(parentDir)) {
|
||||
const grandParent = path.dirname(parentDir);
|
||||
if (grandParent && grandParent !== parentDir) {
|
||||
removeDirAndJunk(parentDir, jobLog);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
jobLog(`[finalizeMedia] Source directory is not empty; keeping it: ${sourceDir}\n`);
|
||||
jobLog(`[finalizeMedia] Source directory is not empty (contains other episodes/files); keeping it: ${sourceDir}\n`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user