@dominevo/series-scraper-core 0.0.92 → 0.0.94
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/repositories/settings.repository.js +3 -0
- package/dist/services/download.service.d.ts +6 -0
- package/dist/services/download.service.js +42 -16
- package/dist/services/library-permissions.d.ts +27 -0
- package/dist/services/library-permissions.js +113 -0
- package/dist/services/media-verify.d.ts +19 -0
- package/dist/services/media-verify.js +79 -0
- package/dist/services/sonarr.service.d.ts +8 -0
- package/dist/services/sonarr.service.js +31 -0
- package/dist/types.d.ts +6 -0
- package/package.json +1 -1
|
@@ -301,6 +301,12 @@ export declare class DownloadService implements OnApplicationBootstrap, OnModule
|
|
|
301
301
|
/**
|
|
302
302
|
* Move a file atomically. Falls back to copy+delete if src and dest are on different devices.
|
|
303
303
|
*/
|
|
304
|
+
/** The season folder name: Sonarr's own naming when readable, else "Season NN". */
|
|
305
|
+
private seasonFolderName;
|
|
306
|
+
/** A finished download goes into the library only once ffmpeg can read a video in it; else it is deleted and the job fails. */
|
|
307
|
+
private placeDownloadInLibrary;
|
|
308
|
+
/** mkdir the library dir, move the file in, and open permissions on both (a chmod failure never fails the move). */
|
|
309
|
+
private placeInLibrary;
|
|
304
310
|
private moveFile;
|
|
305
311
|
/**
|
|
306
312
|
* Strip filesystem-reserved characters from a filename component.
|
|
@@ -47,6 +47,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
|
|
|
47
47
|
var DownloadService_1;
|
|
48
48
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
49
49
|
exports.DownloadService = void 0;
|
|
50
|
+
const media_verify_1 = require("./media-verify");
|
|
50
51
|
const common_1 = require("@nestjs/common");
|
|
51
52
|
const tokens_1 = require("../ports/tokens");
|
|
52
53
|
const logger_1 = require("../ports/logger");
|
|
@@ -83,6 +84,7 @@ const captcha_service_1 = require("./captcha.service");
|
|
|
83
84
|
const maxstream_page_1 = require("./maxstream-page");
|
|
84
85
|
const captcha_fallback_1 = require("./captcha-fallback");
|
|
85
86
|
const failure_reason_1 = require("./failure-reason");
|
|
87
|
+
const library_permissions_1 = require("./library-permissions");
|
|
86
88
|
/**
|
|
87
89
|
* Bounded-concurrency scheduler with **per-group auto-balancing cap**.
|
|
88
90
|
*
|
|
@@ -716,7 +718,7 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
716
718
|
}
|
|
717
719
|
if (!cachedFinalDirBase)
|
|
718
720
|
return null;
|
|
719
|
-
const seasonFolder =
|
|
721
|
+
const seasonFolder = await this.seasonFolderName(seasonNumber);
|
|
720
722
|
return path.join(path.resolve(settings.sonarrRootPath), cachedFinalDirBase, seasonFolder);
|
|
721
723
|
};
|
|
722
724
|
const tempDir = (0, download_paths_1.resolveDownloadDir)(settings.downloadPath, this.paths);
|
|
@@ -1026,8 +1028,7 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
1026
1028
|
this.logger.log(`${attemptTag} downloading → ${fileName}`);
|
|
1027
1029
|
const tempPath = await this.downloadFile(resolvedLink, tempDir, fileName, childId, job);
|
|
1028
1030
|
if (finalDir) {
|
|
1029
|
-
await
|
|
1030
|
-
await this.moveFile(tempPath, path.join(finalDir, path.basename(tempPath)));
|
|
1031
|
+
await this.placeDownloadInLibrary(tempPath, finalDir);
|
|
1031
1032
|
}
|
|
1032
1033
|
await this.sonarr.rescanSeries(entry.arrId);
|
|
1033
1034
|
if (settings.renameEp)
|
|
@@ -1341,9 +1342,7 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
1341
1342
|
// Move atomically to Radarr's library folder if moveEp is enabled
|
|
1342
1343
|
let finalPath = tempPath;
|
|
1343
1344
|
if (finalDir) {
|
|
1344
|
-
await
|
|
1345
|
-
const destPath = path.join(finalDir, path.basename(tempPath));
|
|
1346
|
-
await this.moveFile(tempPath, destPath);
|
|
1345
|
+
const destPath = await this.placeDownloadInLibrary(tempPath, finalDir);
|
|
1347
1346
|
finalPath = destPath;
|
|
1348
1347
|
}
|
|
1349
1348
|
await this.radarr.rescanMovie(entry.arrId);
|
|
@@ -2240,6 +2239,30 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
2240
2239
|
/**
|
|
2241
2240
|
* Move a file atomically. Falls back to copy+delete if src and dest are on different devices.
|
|
2242
2241
|
*/
|
|
2242
|
+
/** The season folder name: Sonarr's own naming when readable, else "Season NN". */
|
|
2243
|
+
async seasonFolderName(season) {
|
|
2244
|
+
const named = await this.sonarr.getSeasonFolderName(season);
|
|
2245
|
+
return named ?? `Season ${String(season).padStart(2, '0')}`;
|
|
2246
|
+
}
|
|
2247
|
+
/** A finished download goes into the library only once ffmpeg can read a video in it; else it is deleted and the job fails. */
|
|
2248
|
+
async placeDownloadInLibrary(tempPath, finalDir) {
|
|
2249
|
+
const verdict = await (0, media_verify_1.verifyMediaFile)(await this.binaries.ffmpeg(), tempPath);
|
|
2250
|
+
if (!verdict.ok) {
|
|
2251
|
+
this.logger.error(`Refusing to place ${path.basename(tempPath)} in the library: ${verdict.reason}`);
|
|
2252
|
+
await fs.unlink(tempPath).catch(() => { });
|
|
2253
|
+
throw new Error(`Downloaded file is not a playable video: ${verdict.reason}`);
|
|
2254
|
+
}
|
|
2255
|
+
return this.placeInLibrary(tempPath, finalDir);
|
|
2256
|
+
}
|
|
2257
|
+
/** mkdir the library dir, move the file in, and open permissions on both (a chmod failure never fails the move). */
|
|
2258
|
+
async placeInLibrary(tempPath, finalDir) {
|
|
2259
|
+
const settings = this.settings.get();
|
|
2260
|
+
await (0, library_permissions_1.mkdirLibrary)(finalDir, settings, this.logger);
|
|
2261
|
+
const dest = path.join(finalDir, path.basename(tempPath));
|
|
2262
|
+
await this.moveFile(tempPath, dest);
|
|
2263
|
+
await (0, library_permissions_1.openLibraryFile)(dest, settings, this.logger);
|
|
2264
|
+
return dest;
|
|
2265
|
+
}
|
|
2243
2266
|
async moveFile(src, dest) {
|
|
2244
2267
|
try {
|
|
2245
2268
|
await fs.rename(src, dest);
|
|
@@ -2744,6 +2767,13 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
2744
2767
|
fs.unlink(outputPath).catch(() => { });
|
|
2745
2768
|
reject(new Error('Download cancelled'));
|
|
2746
2769
|
}
|
|
2770
|
+
else if (code === 0 && (0, media_verify_1.hlsStoppedEarly)(totalDuration, currentTimeSec)) {
|
|
2771
|
+
// ffmpeg exits 0 on a shutdown signal too (process restart, not a user abort): the file is truncated.
|
|
2772
|
+
const message = `ffmpeg stopped at ${Math.round(currentTimeSec)} of ${Math.round(totalDuration)} s`;
|
|
2773
|
+
this.logger.error(`HLS download incomplete (${message}): ${outputPath}`);
|
|
2774
|
+
fs.unlink(outputPath).catch(() => { });
|
|
2775
|
+
reject(new Error(message));
|
|
2776
|
+
}
|
|
2747
2777
|
else if (code === 0) {
|
|
2748
2778
|
this.logger.log(`HLS download completed: ${outputPath}`);
|
|
2749
2779
|
resolve();
|
|
@@ -3068,7 +3098,7 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
3068
3098
|
const series = await this.sonarr.getSeriesById(entry.arrId);
|
|
3069
3099
|
if (series) {
|
|
3070
3100
|
const seriesFolder = path.basename(series.path);
|
|
3071
|
-
const seasonFolder =
|
|
3101
|
+
const seasonFolder = await this.seasonFolderName(season);
|
|
3072
3102
|
finalDir = path.join(path.resolve(settings.sonarrRootPath), seriesFolder, seasonFolder);
|
|
3073
3103
|
}
|
|
3074
3104
|
}
|
|
@@ -3100,8 +3130,7 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
3100
3130
|
this.logger.log(`${attemptTag} downloading → ${fileName}`);
|
|
3101
3131
|
const tempPath = await this.downloadFile(resolvedLink, tempDir, fileName, entry.id, job);
|
|
3102
3132
|
if (finalDir) {
|
|
3103
|
-
await
|
|
3104
|
-
await this.moveFile(tempPath, path.join(finalDir, path.basename(tempPath)));
|
|
3133
|
+
await this.placeDownloadInLibrary(tempPath, finalDir);
|
|
3105
3134
|
}
|
|
3106
3135
|
await this.sonarr.rescanSeries(entry.arrId);
|
|
3107
3136
|
if (settings.renameEp)
|
|
@@ -3153,8 +3182,7 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
3153
3182
|
};
|
|
3154
3183
|
const tempPath = await this.downloadFile(resolvedLink, tempDir, fileName, entry.id, job);
|
|
3155
3184
|
if (finalDir) {
|
|
3156
|
-
await
|
|
3157
|
-
await this.moveFile(tempPath, path.join(finalDir, path.basename(tempPath)));
|
|
3185
|
+
await this.placeDownloadInLibrary(tempPath, finalDir);
|
|
3158
3186
|
}
|
|
3159
3187
|
await this.radarr.rescanMovie(entry.arrId);
|
|
3160
3188
|
if (settings.renameEp)
|
|
@@ -3389,13 +3417,12 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
3389
3417
|
const series = await this.sonarr.getSeriesById(entry.arrId);
|
|
3390
3418
|
if (series) {
|
|
3391
3419
|
const seriesFolder = path.basename(series.path);
|
|
3392
|
-
const seasonFolder =
|
|
3420
|
+
const seasonFolder = await this.seasonFolderName(partial.season);
|
|
3393
3421
|
finalDir = path.join(path.resolve(settings.sonarrRootPath), seriesFolder, seasonFolder);
|
|
3394
3422
|
}
|
|
3395
3423
|
}
|
|
3396
3424
|
if (finalDir) {
|
|
3397
|
-
await
|
|
3398
|
-
await this.moveFile(tempPath, path.join(finalDir, path.basename(tempPath)));
|
|
3425
|
+
await this.placeDownloadInLibrary(tempPath, finalDir);
|
|
3399
3426
|
}
|
|
3400
3427
|
await this.sonarr.rescanSeries(entry.arrId);
|
|
3401
3428
|
if (settings.renameEp)
|
|
@@ -3416,8 +3443,7 @@ let DownloadService = DownloadService_1 = class DownloadService {
|
|
|
3416
3443
|
finalDir = path.join(path.resolve(settings.radarrRootPath), path.basename(movie.path));
|
|
3417
3444
|
}
|
|
3418
3445
|
if (finalDir) {
|
|
3419
|
-
await
|
|
3420
|
-
await this.moveFile(tempPath, path.join(finalDir, path.basename(tempPath)));
|
|
3446
|
+
await this.placeDownloadInLibrary(tempPath, finalDir);
|
|
3421
3447
|
}
|
|
3422
3448
|
await this.radarr.rescanMovie(entry.arrId);
|
|
3423
3449
|
if (settings.renameEp)
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/** Unraid "New Permissions" convention: every user may read and write the library. */
|
|
2
|
+
export declare const DEFAULT_LIBRARY_DIR_MODE = "0777";
|
|
3
|
+
export declare const DEFAULT_LIBRARY_FILE_MODE = "0666";
|
|
4
|
+
export interface LibraryPermissionSettings {
|
|
5
|
+
libraryOpenPermissions?: boolean;
|
|
6
|
+
libraryDirMode?: string;
|
|
7
|
+
libraryFileMode?: string;
|
|
8
|
+
}
|
|
9
|
+
export interface PermissionLog {
|
|
10
|
+
warn(message: string): void;
|
|
11
|
+
}
|
|
12
|
+
/** Parse an octal string ("0777", "777", "0o775"); null when it is not a valid mode. */
|
|
13
|
+
export declare function parseOctalMode(raw: unknown): number | null;
|
|
14
|
+
/** chmod that never throws: a failure is logged with the path and errno. */
|
|
15
|
+
export declare function chmodQuiet(target: string, mode: number, log: PermissionLog): Promise<void>;
|
|
16
|
+
/**
|
|
17
|
+
* mkdir -p that also opens every directory it CREATED (an existing ancestor is
|
|
18
|
+
* left alone). `recursive` mkdir applies the umask, so each new level is chmod-ed.
|
|
19
|
+
*/
|
|
20
|
+
export declare function mkdirLibrary(dir: string, s: LibraryPermissionSettings, log: PermissionLog): Promise<void>;
|
|
21
|
+
/** Open a file that was just moved or copied into a library. */
|
|
22
|
+
export declare function openLibraryFile(file: string, s: LibraryPermissionSettings, log: PermissionLog): Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Render a Sonarr `seasonFolderFormat`. Only `{season}` and `{season:00}` are
|
|
25
|
+
* understood; any other token returns null so the caller falls back.
|
|
26
|
+
*/
|
|
27
|
+
export declare function renderSeasonFolder(format: string, season: number): string | null;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.DEFAULT_LIBRARY_FILE_MODE = exports.DEFAULT_LIBRARY_DIR_MODE = void 0;
|
|
37
|
+
exports.parseOctalMode = parseOctalMode;
|
|
38
|
+
exports.chmodQuiet = chmodQuiet;
|
|
39
|
+
exports.mkdirLibrary = mkdirLibrary;
|
|
40
|
+
exports.openLibraryFile = openLibraryFile;
|
|
41
|
+
exports.renderSeasonFolder = renderSeasonFolder;
|
|
42
|
+
const fs = __importStar(require("fs/promises"));
|
|
43
|
+
const path = __importStar(require("path"));
|
|
44
|
+
/** Unraid "New Permissions" convention: every user may read and write the library. */
|
|
45
|
+
exports.DEFAULT_LIBRARY_DIR_MODE = '0777';
|
|
46
|
+
exports.DEFAULT_LIBRARY_FILE_MODE = '0666';
|
|
47
|
+
/** Parse an octal string ("0777", "777", "0o775"); null when it is not a valid mode. */
|
|
48
|
+
function parseOctalMode(raw) {
|
|
49
|
+
if (typeof raw !== 'string')
|
|
50
|
+
return null;
|
|
51
|
+
const text = raw.trim().replace(/^0o/i, '');
|
|
52
|
+
if (!/^[0-7]{3,4}$/.test(text))
|
|
53
|
+
return null;
|
|
54
|
+
return parseInt(text, 8);
|
|
55
|
+
}
|
|
56
|
+
function modeOrDefault(raw, fallback) {
|
|
57
|
+
return parseOctalMode(raw) ?? parseOctalMode(fallback);
|
|
58
|
+
}
|
|
59
|
+
function errnoOf(err) {
|
|
60
|
+
if (err && typeof err === 'object' && 'code' in err)
|
|
61
|
+
return String(err.code);
|
|
62
|
+
return 'unknown';
|
|
63
|
+
}
|
|
64
|
+
/** chmod that never throws: a failure is logged with the path and errno. */
|
|
65
|
+
async function chmodQuiet(target, mode, log) {
|
|
66
|
+
try {
|
|
67
|
+
await fs.chmod(target, mode);
|
|
68
|
+
}
|
|
69
|
+
catch (err) {
|
|
70
|
+
log.warn(`chmod ${mode.toString(8)} failed on ${target} (${errnoOf(err)}): ${err instanceof Error ? err.message : String(err)}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* mkdir -p that also opens every directory it CREATED (an existing ancestor is
|
|
75
|
+
* left alone). `recursive` mkdir applies the umask, so each new level is chmod-ed.
|
|
76
|
+
*/
|
|
77
|
+
async function mkdirLibrary(dir, s, log) {
|
|
78
|
+
const first = await fs.mkdir(dir, { recursive: true });
|
|
79
|
+
if (s.libraryOpenPermissions === false || !first)
|
|
80
|
+
return;
|
|
81
|
+
const mode = modeOrDefault(s.libraryDirMode, exports.DEFAULT_LIBRARY_DIR_MODE);
|
|
82
|
+
// `first` is the topmost directory created; open it and every level down to `dir`.
|
|
83
|
+
const levels = [];
|
|
84
|
+
for (let cur = dir;; cur = path.dirname(cur)) {
|
|
85
|
+
levels.unshift(cur);
|
|
86
|
+
if (cur === first || path.dirname(cur) === cur)
|
|
87
|
+
break;
|
|
88
|
+
}
|
|
89
|
+
for (const level of levels)
|
|
90
|
+
await chmodQuiet(level, mode, log);
|
|
91
|
+
}
|
|
92
|
+
/** Open a file that was just moved or copied into a library. */
|
|
93
|
+
async function openLibraryFile(file, s, log) {
|
|
94
|
+
if (s.libraryOpenPermissions === false)
|
|
95
|
+
return;
|
|
96
|
+
await chmodQuiet(file, modeOrDefault(s.libraryFileMode, exports.DEFAULT_LIBRARY_FILE_MODE), log);
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Render a Sonarr `seasonFolderFormat`. Only `{season}` and `{season:00}` are
|
|
100
|
+
* understood; any other token returns null so the caller falls back.
|
|
101
|
+
*/
|
|
102
|
+
function renderSeasonFolder(format, season) {
|
|
103
|
+
let unknown = false;
|
|
104
|
+
const out = format.replace(/\{([^}]*)\}/g, (_m, token) => {
|
|
105
|
+
if (token === 'season')
|
|
106
|
+
return String(season);
|
|
107
|
+
if (token === 'season:00')
|
|
108
|
+
return String(season).padStart(2, '0');
|
|
109
|
+
unknown = true;
|
|
110
|
+
return '';
|
|
111
|
+
});
|
|
112
|
+
return unknown || !out.trim() ? null : out;
|
|
113
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** An ffmpeg that exits 0 before this share of the stream length was written did not finish. */
|
|
2
|
+
export declare const HLS_COMPLETE_SHARE = 0.97;
|
|
3
|
+
/** True when ffmpeg stopped (even with code 0) well short of the length it announced. Unknown length: false. */
|
|
4
|
+
export declare function hlsStoppedEarly(totalDurationSec: number, currentTimeSec: number): boolean;
|
|
5
|
+
export interface MediaProbe {
|
|
6
|
+
readonly hasVideo: boolean;
|
|
7
|
+
readonly durationSec: number;
|
|
8
|
+
}
|
|
9
|
+
export type MediaVerdict = {
|
|
10
|
+
readonly ok: true;
|
|
11
|
+
readonly durationSec: number;
|
|
12
|
+
} | {
|
|
13
|
+
readonly ok: false;
|
|
14
|
+
readonly reason: string;
|
|
15
|
+
};
|
|
16
|
+
/** Reads what `ffmpeg -i <file>` prints to stderr: a Video stream and the Duration line. */
|
|
17
|
+
export declare function parseMediaProbe(stderr: string): MediaProbe;
|
|
18
|
+
/** Is `file` a playable video: non-empty, has a video stream, has a duration. Never throws. */
|
|
19
|
+
export declare function verifyMediaFile(ffmpegPath: string, file: string): Promise<MediaVerdict>;
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.HLS_COMPLETE_SHARE = void 0;
|
|
37
|
+
exports.hlsStoppedEarly = hlsStoppedEarly;
|
|
38
|
+
exports.parseMediaProbe = parseMediaProbe;
|
|
39
|
+
exports.verifyMediaFile = verifyMediaFile;
|
|
40
|
+
const child_process_1 = require("child_process");
|
|
41
|
+
const fs = __importStar(require("fs/promises"));
|
|
42
|
+
/** An ffmpeg that exits 0 before this share of the stream length was written did not finish. */
|
|
43
|
+
exports.HLS_COMPLETE_SHARE = 0.97;
|
|
44
|
+
/** True when ffmpeg stopped (even with code 0) well short of the length it announced. Unknown length: false. */
|
|
45
|
+
function hlsStoppedEarly(totalDurationSec, currentTimeSec) {
|
|
46
|
+
return totalDurationSec > 0 && currentTimeSec < exports.HLS_COMPLETE_SHARE * totalDurationSec;
|
|
47
|
+
}
|
|
48
|
+
/** Reads what `ffmpeg -i <file>` prints to stderr: a Video stream and the Duration line. */
|
|
49
|
+
function parseMediaProbe(stderr) {
|
|
50
|
+
const dm = stderr.match(/Duration:\s*(\d+):(\d+):(\d+(?:\.\d+)?)/);
|
|
51
|
+
const durationSec = dm ? parseInt(dm[1], 10) * 3600 + parseInt(dm[2], 10) * 60 + parseFloat(dm[3]) : 0;
|
|
52
|
+
return { hasVideo: /Stream #\S+.*:\s*Video:/.test(stderr), durationSec };
|
|
53
|
+
}
|
|
54
|
+
function probeStderr(ffmpegPath, file) {
|
|
55
|
+
// `ffmpeg -i <file>` with no output prints the probe and exits non-zero: only stderr matters.
|
|
56
|
+
return new Promise((resolve) => {
|
|
57
|
+
(0, child_process_1.execFile)(ffmpegPath, ['-hide_banner', '-i', file], { maxBuffer: 4 * 1024 * 1024, timeout: 30_000 }, (_err, _out, stderr) => {
|
|
58
|
+
resolve(String(stderr ?? ''));
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
/** Is `file` a playable video: non-empty, has a video stream, has a duration. Never throws. */
|
|
63
|
+
async function verifyMediaFile(ffmpegPath, file) {
|
|
64
|
+
let size;
|
|
65
|
+
try {
|
|
66
|
+
size = (await fs.stat(file)).size;
|
|
67
|
+
}
|
|
68
|
+
catch (err) {
|
|
69
|
+
return { ok: false, reason: `file not readable: ${err instanceof Error ? err.message : String(err)}` };
|
|
70
|
+
}
|
|
71
|
+
if (size === 0)
|
|
72
|
+
return { ok: false, reason: 'file is 0 bytes' };
|
|
73
|
+
const probe = parseMediaProbe(await probeStderr(ffmpegPath, file));
|
|
74
|
+
if (!probe.hasVideo)
|
|
75
|
+
return { ok: false, reason: 'no video stream (unreadable or truncated container)' };
|
|
76
|
+
if (probe.durationSec <= 0)
|
|
77
|
+
return { ok: false, reason: 'duration is 0 or unknown' };
|
|
78
|
+
return { ok: true, durationSec: probe.durationSec };
|
|
79
|
+
}
|
|
@@ -3,7 +3,15 @@ import { SettingsService } from './settings.service';
|
|
|
3
3
|
export declare class SonarrService {
|
|
4
4
|
private readonly settings;
|
|
5
5
|
private readonly logger;
|
|
6
|
+
/** Sonarr's `seasonFolderFormat`, per Sonarr address; null = unreadable (fall back). */
|
|
7
|
+
private namingCache;
|
|
8
|
+
private namingErrorLogged;
|
|
6
9
|
constructor(settings: SettingsService);
|
|
10
|
+
/**
|
|
11
|
+
* The season folder name as Sonarr itself would build it ("Season 5" or "Season 05"), from its naming
|
|
12
|
+
* config. Null when Sonarr is absent, errors (logged once) or uses a token we do not render.
|
|
13
|
+
*/
|
|
14
|
+
getSeasonFolderName(season: number): Promise<string | null>;
|
|
7
15
|
private getConfig;
|
|
8
16
|
status(): Promise<{
|
|
9
17
|
configured: boolean;
|
|
@@ -16,10 +16,41 @@ const logger_1 = require("../ports/logger");
|
|
|
16
16
|
const egress_context_1 = require("./egress/egress-context");
|
|
17
17
|
const arr_images_1 = require("./arr-images");
|
|
18
18
|
const settings_service_1 = require("./settings.service");
|
|
19
|
+
const library_permissions_1 = require("./library-permissions");
|
|
19
20
|
let SonarrService = SonarrService_1 = class SonarrService {
|
|
20
21
|
constructor(settings) {
|
|
21
22
|
this.settings = settings;
|
|
22
23
|
this.logger = new logger_1.ScopedLogger(SonarrService_1.name);
|
|
24
|
+
/** Sonarr's `seasonFolderFormat`, per Sonarr address; null = unreadable (fall back). */
|
|
25
|
+
this.namingCache = null;
|
|
26
|
+
this.namingErrorLogged = false;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* The season folder name as Sonarr itself would build it ("Season 5" or "Season 05"), from its naming
|
|
30
|
+
* config. Null when Sonarr is absent, errors (logged once) or uses a token we do not render.
|
|
31
|
+
*/
|
|
32
|
+
async getSeasonFolderName(season) {
|
|
33
|
+
const config = this.getConfig();
|
|
34
|
+
if (!config)
|
|
35
|
+
return null;
|
|
36
|
+
const key = `${config.url}\n${config.apiKey}`;
|
|
37
|
+
if (this.namingCache?.key !== key) {
|
|
38
|
+
let format = null;
|
|
39
|
+
try {
|
|
40
|
+
const data = await this.apiGet(config, '/api/v3/config/naming');
|
|
41
|
+
format = typeof data.seasonFolderFormat === 'string' ? data.seasonFolderFormat : null;
|
|
42
|
+
this.namingErrorLogged = false;
|
|
43
|
+
}
|
|
44
|
+
catch (err) {
|
|
45
|
+
if (!this.namingErrorLogged) {
|
|
46
|
+
this.namingErrorLogged = true;
|
|
47
|
+
this.logger.warn(`Sonarr naming config unreadable, using "Season NN" folders: ${err}`);
|
|
48
|
+
}
|
|
49
|
+
return null; // not cached: a later call retries
|
|
50
|
+
}
|
|
51
|
+
this.namingCache = { key, format };
|
|
52
|
+
}
|
|
53
|
+
return this.namingCache.format ? (0, library_permissions_1.renderSeasonFolder)(this.namingCache.format, season) : null;
|
|
23
54
|
}
|
|
24
55
|
getConfig() {
|
|
25
56
|
const s = this.settings.get();
|
package/dist/types.d.ts
CHANGED
|
@@ -441,6 +441,12 @@ export interface AppSettings {
|
|
|
441
441
|
scanDelay: number;
|
|
442
442
|
autoBind: boolean;
|
|
443
443
|
moveEp: boolean;
|
|
444
|
+
/** chmod everything the scraper puts in a library so Sonarr/Radarr (another uid) can rename it. */
|
|
445
|
+
libraryOpenPermissions: boolean;
|
|
446
|
+
/** Octal mode for directories the scraper creates in a library. */
|
|
447
|
+
libraryDirMode: string;
|
|
448
|
+
/** Octal mode for files the scraper moves into a library. */
|
|
449
|
+
libraryFileMode: string;
|
|
444
450
|
renameEp: boolean;
|
|
445
451
|
tagsMode: 'WHITELIST' | 'BLACKLIST';
|
|
446
452
|
logLevel: 'debug' | 'info' | 'warning' | 'error';
|
package/package.json
CHANGED