@dominevo/series-scraper-core 0.0.91 → 0.0.93

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.
Files changed (31) hide show
  1. package/dist/controllers/notifications.controller.d.ts +4 -11
  2. package/dist/controllers/notifications.controller.js +7 -5
  3. package/dist/controllers/settings.controller.d.ts +3 -3
  4. package/dist/controllers/settings.controller.js +6 -4
  5. package/dist/controllers/sites.controller.d.ts +5 -53
  6. package/dist/controllers/sites.controller.js +47 -14
  7. package/dist/controllers/vpn.controller.js +2 -0
  8. package/dist/docker-layout/secret-layout.js +19 -7
  9. package/dist/ports/secret-store.d.ts +2 -0
  10. package/dist/ports/secret-store.js +2 -0
  11. package/dist/repositories/settings.repository.d.ts +5 -0
  12. package/dist/repositories/settings.repository.js +18 -1
  13. package/dist/repositories/sites.repository.d.ts +6 -1
  14. package/dist/repositories/sites.repository.js +34 -5
  15. package/dist/repositories/webhook.repository.d.ts +3 -0
  16. package/dist/repositories/webhook.repository.js +13 -4
  17. package/dist/scraper.module.js +2 -0
  18. package/dist/services/download.service.d.ts +4 -0
  19. package/dist/services/download.service.js +24 -16
  20. package/dist/services/library-permissions.d.ts +27 -0
  21. package/dist/services/library-permissions.js +113 -0
  22. package/dist/services/plugin-settings.service.d.ts +2 -0
  23. package/dist/services/plugin-settings.service.js +16 -3
  24. package/dist/services/secret-migration.service.d.ts +22 -0
  25. package/dist/services/secret-migration.service.js +48 -0
  26. package/dist/services/secret-views.d.ts +56 -0
  27. package/dist/services/secret-views.js +120 -0
  28. package/dist/services/sonarr.service.d.ts +8 -0
  29. package/dist/services/sonarr.service.js +31 -0
  30. package/dist/types.d.ts +6 -0
  31. package/package.json +1 -1
@@ -1,28 +1,21 @@
1
1
  import { Observable } from 'rxjs';
2
2
  import { NotificationService } from '../services/notification.service';
3
+ import { WebhookView } from '../services/secret-views';
3
4
  import { WebhookRepository, WebhookConfig } from '../repositories/webhook.repository';
4
5
  export declare class NotificationsController {
5
6
  private readonly notifications;
6
7
  private readonly webhookRepo;
7
8
  constructor(notifications: NotificationService, webhookRepo: WebhookRepository);
8
9
  stream(): Observable<MessageEvent>;
9
- getWebhooks(): WebhookConfig[];
10
+ getWebhooks(): WebhookView[];
10
11
  createWebhook(body: {
11
12
  name: string;
12
13
  url: string;
13
14
  events?: string[];
14
15
  headers?: Record<string, string>;
15
16
  payloadTemplate?: any;
16
- }): WebhookConfig;
17
- updateWebhook(id: string, body: Partial<WebhookConfig>): {
18
- id: string;
19
- name: string;
20
- url: string;
21
- enabled: boolean;
22
- events: string[];
23
- headers: Record<string, string>;
24
- payloadTemplate?: any;
25
- };
17
+ }): WebhookView;
18
+ updateWebhook(id: string, body: Partial<WebhookConfig>): WebhookView;
26
19
  deleteWebhook(id: string): {
27
20
  ok: boolean;
28
21
  };
@@ -17,6 +17,7 @@ const common_1 = require("@nestjs/common");
17
17
  const rxjs_1 = require("rxjs");
18
18
  const uuid_1 = require("uuid");
19
19
  const notification_service_1 = require("../services/notification.service");
20
+ const secret_views_1 = require("../services/secret-views");
20
21
  const webhook_repository_1 = require("../repositories/webhook.repository");
21
22
  let NotificationsController = class NotificationsController {
22
23
  constructor(notifications, webhookRepo) {
@@ -30,7 +31,7 @@ let NotificationsController = class NotificationsController {
30
31
  }
31
32
  // --- Webhook CRUD ---
32
33
  getWebhooks() {
33
- return this.webhookRepo.getAll();
34
+ return this.webhookRepo.getAll().map(secret_views_1.toWebhookView);
34
35
  }
35
36
  createWebhook(body) {
36
37
  const webhook = {
@@ -39,19 +40,20 @@ let NotificationsController = class NotificationsController {
39
40
  url: body.url,
40
41
  enabled: true,
41
42
  events: body.events || [],
42
- headers: body.headers || {},
43
+ headers: (0, secret_views_1.mergeHeaders)({}, body.headers ?? {}),
43
44
  payloadTemplate: body.payloadTemplate,
44
45
  };
45
46
  this.webhookRepo.save(webhook);
46
- return webhook;
47
+ return (0, secret_views_1.toWebhookView)(webhook);
47
48
  }
48
49
  updateWebhook(id, body) {
49
50
  const existing = this.webhookRepo.getById(id);
50
51
  if (!existing)
51
52
  throw new common_1.HttpException('Webhook not found', common_1.HttpStatus.NOT_FOUND);
52
- const updated = { ...existing, ...body, id: existing.id };
53
+ const { headers, secretHeaders: _view, urlSecretSet: _set, ...rest } = body;
54
+ const updated = { ...existing, ...rest, id: existing.id, url: (0, secret_views_1.mergeWebhookUrl)(existing.url, rest.url), headers: headers === undefined ? existing.headers : (0, secret_views_1.mergeHeaders)(existing.headers, headers) };
53
55
  this.webhookRepo.save(updated);
54
- return updated;
56
+ return (0, secret_views_1.toWebhookView)(updated);
55
57
  }
56
58
  deleteWebhook(id) {
57
59
  const ok = this.webhookRepo.delete(id);
@@ -1,16 +1,16 @@
1
- import { AppSettings } from '../types';
2
1
  import { SettingsService } from '../services/settings.service';
3
2
  import { DownloadService } from '../services/download.service';
3
+ import { SettingsView } from '../services/secret-views';
4
4
  import { SUPPORTED_LANGUAGES, LANGUAGE_TYPES } from '../constants/languages';
5
5
  export declare class SettingsController {
6
6
  private readonly settingsService;
7
7
  private readonly downloadService;
8
8
  constructor(settingsService: SettingsService, downloadService: DownloadService);
9
- get(): AppSettings;
9
+ get(): SettingsView;
10
10
  /** Predefined choices exposed to the UI for the preferred-language dropdowns. */
11
11
  getLanguages(): {
12
12
  languages: typeof SUPPORTED_LANGUAGES;
13
13
  types: typeof LANGUAGE_TYPES;
14
14
  };
15
- update(body: Partial<AppSettings>): AppSettings;
15
+ update(body: unknown): SettingsView;
16
16
  }
@@ -16,6 +16,7 @@ exports.SettingsController = void 0;
16
16
  const common_1 = require("@nestjs/common");
17
17
  const settings_service_1 = require("../services/settings.service");
18
18
  const download_service_1 = require("../services/download.service");
19
+ const secret_views_1 = require("../services/secret-views");
19
20
  const languages_1 = require("../constants/languages");
20
21
  let SettingsController = class SettingsController {
21
22
  constructor(settingsService, downloadService) {
@@ -23,18 +24,19 @@ let SettingsController = class SettingsController {
23
24
  this.downloadService = downloadService;
24
25
  }
25
26
  get() {
26
- return this.settingsService.get();
27
+ return (0, secret_views_1.toSettingsView)(this.settingsService.get());
27
28
  }
28
29
  /** Predefined choices exposed to the UI for the preferred-language dropdowns. */
29
30
  getLanguages() {
30
31
  return { languages: languages_1.SUPPORTED_LANGUAGES, types: languages_1.LANGUAGE_TYPES };
31
32
  }
32
33
  update(body) {
33
- const updated = this.settingsService.update(body);
34
- if (body.concurrentDownloads !== undefined) {
34
+ const patch = (0, secret_views_1.parseSettingsBody)(body);
35
+ const updated = this.settingsService.update(patch);
36
+ if (patch.concurrentDownloads !== undefined) {
35
37
  this.downloadService.syncConcurrency();
36
38
  }
37
- return updated;
39
+ return (0, secret_views_1.toSettingsView)(updated);
38
40
  }
39
41
  };
40
42
  exports.SettingsController = SettingsController;
@@ -7,70 +7,22 @@ export declare class SitesController {
7
7
  private readonly adapterRegistry;
8
8
  private readonly analyzer;
9
9
  constructor(sitesRepo: SitesRepository, adapterRegistry: AdapterRegistryService, analyzer: SiteAnalyzerService);
10
- getAll(): GenericSiteConfig[];
11
- getById(id: string): GenericSiteConfig;
10
+ getAll(): import("../services/secret-views").SiteView[];
11
+ getById(id: string): import("../services/secret-views").SiteView;
12
12
  analyze(body: {
13
13
  url: string;
14
14
  }): Promise<import("../services/site-analyzer.service").AnalysisResult>;
15
15
  analyzeSearch(body: {
16
16
  url: string;
17
17
  }): Promise<Partial<import("../services/site-analyzer.service").AnalysisResult>>;
18
- create(body: Omit<GenericSiteConfig, 'id'>): GenericSiteConfig;
19
- update(id: string, body: Partial<GenericSiteConfig>): {
20
- id: string;
21
- name: string;
22
- baseUrl: string;
23
- hosts: string[];
24
- languages?: import("../types").AdapterLanguageInfo[];
25
- search: {
26
- urlPattern: string;
27
- resultSelector: string;
28
- titleSelector: string;
29
- urlSelector: string;
30
- imageSelector?: string;
31
- imageAttr?: string;
32
- };
33
- seasons?: {
34
- listSelector: string;
35
- numberAttr?: string;
36
- numberSelector?: string;
37
- urlSelector?: string;
38
- singlePage?: boolean;
39
- };
40
- episodes: {
41
- listSelector: string;
42
- numberAttr?: string;
43
- numberSelector?: string;
44
- titleSelector?: string;
45
- urlSelector?: string;
46
- seasonFilterAttr?: string;
47
- seasonFilterSelector?: string;
48
- episodeIdAttr?: string;
49
- };
50
- downloads: {
51
- linkSelector?: string;
52
- linkAttr?: string;
53
- videoUrlPattern?: boolean;
54
- iframeSelector?: string;
55
- mirrorSelector?: string;
56
- mirrorFilterSelector?: string;
57
- mirrorLinkAttr?: string;
58
- languageFromQuery?: string;
59
- languageMap?: Record<string, string>;
60
- defaultLanguage?: string;
61
- grabberUrlPattern?: string;
62
- playerIdSelector?: string;
63
- playerIdAttr?: string;
64
- };
65
- headers?: Record<string, string>;
66
- useHeadless?: boolean;
67
- };
18
+ create(body: Omit<GenericSiteConfig, 'id'>): import("../services/secret-views").SiteView;
19
+ update(id: string, body: Partial<GenericSiteConfig>): import("../services/secret-views").SiteView;
68
20
  delete(id: string): {
69
21
  ok: boolean;
70
22
  };
71
23
  /** Export every site as a single JSON blob — suitable for backup / cross-instance copy. */
72
24
  exportAll(): {
73
- sites: GenericSiteConfig[];
25
+ sites: import("../services/secret-views").SiteView[];
74
26
  };
75
27
  /**
76
28
  * Bulk import site configs. Matches by `name`: existing sites are updated,
@@ -15,9 +15,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SitesController = void 0;
16
16
  const common_1 = require("@nestjs/common");
17
17
  const uuid_1 = require("uuid");
18
+ const secret_views_1 = require("../services/secret-views");
18
19
  const sites_repository_1 = require("../repositories/sites.repository");
19
20
  const adapter_registry_service_1 = require("../services/adapter-registry.service");
20
21
  const site_analyzer_service_1 = require("../services/site-analyzer.service");
22
+ function hostOf(url) {
23
+ if (typeof url !== 'string')
24
+ return undefined;
25
+ try {
26
+ return new URL(url).host.toLowerCase();
27
+ }
28
+ catch {
29
+ return undefined;
30
+ }
31
+ }
32
+ /** Whether an imported site stays on a host the stored one already served (its baseUrl host, or one of its hosts). */
33
+ function sameSiteHost(stored, incoming) {
34
+ const host = hostOf(incoming.baseUrl);
35
+ if (host === undefined)
36
+ return false;
37
+ return host === hostOf(stored.baseUrl) || stored.hosts.some((h) => h.toLowerCase() === host);
38
+ }
21
39
  let SitesController = class SitesController {
22
40
  constructor(sitesRepo, adapterRegistry, analyzer) {
23
41
  this.sitesRepo = sitesRepo;
@@ -25,13 +43,13 @@ let SitesController = class SitesController {
25
43
  this.analyzer = analyzer;
26
44
  }
27
45
  getAll() {
28
- return this.sitesRepo.getAll();
46
+ return this.sitesRepo.getAll().map(secret_views_1.toSiteView);
29
47
  }
30
48
  getById(id) {
31
49
  const site = this.sitesRepo.getById(id);
32
50
  if (!site)
33
51
  throw new common_1.HttpException('Site not found', common_1.HttpStatus.NOT_FOUND);
34
- return site;
52
+ return (0, secret_views_1.toSiteView)(site);
35
53
  }
36
54
  async analyze(body) {
37
55
  if (!body.url)
@@ -54,19 +72,19 @@ let SitesController = class SitesController {
54
72
  }
55
73
  }
56
74
  create(body) {
57
- const site = { ...body, id: (0, uuid_1.v4)() };
75
+ const site = { ...(0, secret_views_1.siteFromWrite)(body), id: (0, uuid_1.v4)() };
58
76
  this.sitesRepo.save(site);
59
77
  this.adapterRegistry.reloadGenericAdapters();
60
- return site;
78
+ return (0, secret_views_1.toSiteView)(site);
61
79
  }
62
80
  update(id, body) {
63
81
  const existing = this.sitesRepo.getById(id);
64
82
  if (!existing)
65
83
  throw new common_1.HttpException('Site not found', common_1.HttpStatus.NOT_FOUND);
66
- const updated = { ...existing, ...body, id: existing.id };
84
+ const updated = { ...existing, ...(0, secret_views_1.siteFromWrite)(body, existing), id: existing.id };
67
85
  this.sitesRepo.save(updated);
68
86
  this.adapterRegistry.reloadGenericAdapters();
69
- return updated;
87
+ return (0, secret_views_1.toSiteView)(updated);
70
88
  }
71
89
  delete(id) {
72
90
  const ok = this.sitesRepo.delete(id);
@@ -77,7 +95,7 @@ let SitesController = class SitesController {
77
95
  }
78
96
  /** Export every site as a single JSON blob — suitable for backup / cross-instance copy. */
79
97
  exportAll() {
80
- return { sites: this.sitesRepo.getAll() };
98
+ return { sites: this.sitesRepo.getAll().map(secret_views_1.toSiteView) };
81
99
  }
82
100
  /**
83
101
  * Bulk import site configs. Matches by `name`: existing sites are updated,
@@ -108,25 +126,40 @@ let SitesController = class SitesController {
108
126
  }
109
127
  let created = 0;
110
128
  let updated = 0;
129
+ // Validate every entry before anything is deleted: a bad one must leave the stored sites untouched.
130
+ for (const raw of incoming) {
131
+ if (!raw || typeof raw !== 'object' || !raw.name) {
132
+ throw new common_1.HttpException('Each site must include a "name"', common_1.HttpStatus.BAD_REQUEST);
133
+ }
134
+ }
135
+ // A replace wipes the sites, and their stored credentials with them: remember what a site that comes back held,
136
+ // so its credential headers (which an export never carries) survive the round trip.
137
+ const before = this.sitesRepo.getAll();
138
+ const priorById = new Map(before.map((s) => [s.id, s]));
139
+ const priorByName = new Map(before.map((s) => [s.name, s]));
111
140
  if (mode === 'replace') {
112
- for (const s of this.sitesRepo.getAll())
141
+ for (const s of before)
113
142
  this.sitesRepo.delete(s.id);
114
143
  }
115
144
  const existing = this.sitesRepo.getAll();
116
145
  const byName = new Map(existing.map((s) => [s.name, s]));
117
146
  for (const raw of incoming) {
118
- if (!raw || typeof raw !== 'object' || !raw.name) {
119
- throw new common_1.HttpException('Each site must include a "name"', common_1.HttpStatus.BAD_REQUEST);
120
- }
121
- const match = byName.get(raw.name);
147
+ const match = byName.get(String(raw.name));
122
148
  if (match) {
123
- const merged = { ...match, ...raw, id: match.id };
149
+ // The stored headers (a cookie) follow the name only while the site stays on the same host.
150
+ const { headers: _stored, ...bare } = match;
151
+ const merged = sameSiteHost(match, raw)
152
+ ? { ...match, ...(0, secret_views_1.siteFromWrite)(raw, match), id: match.id }
153
+ : { ...bare, ...(0, secret_views_1.siteFromWrite)(raw), id: match.id };
124
154
  this.sitesRepo.save(merged);
125
155
  updated++;
126
156
  }
127
157
  else {
128
158
  const id = ('id' in raw && typeof raw.id === 'string') ? raw.id : (0, uuid_1.v4)();
129
- this.sitesRepo.save({ ...raw, id });
159
+ const byId = priorById.get(id);
160
+ const byNameMatch = priorByName.get(String(raw.name));
161
+ const prior = byId ?? (byNameMatch && sameSiteHost(byNameMatch, raw) ? byNameMatch : undefined);
162
+ this.sitesRepo.save({ ...(0, secret_views_1.siteFromWrite)(raw, prior), id });
130
163
  created++;
131
164
  }
132
165
  }
@@ -120,6 +120,8 @@ let VpnController = class VpnController {
120
120
  const known = new Set(this.providersRepo.getAll().map((e) => e.id));
121
121
  const homeOnly = new Set(this.providersRepo.getAll().filter(vpn_service_2.isHomeByDesign).map((e) => e.id));
122
122
  const next = validated(() => (0, egress_input_1.parseSettingsPatch)(body, this.settingsRepo.get(), known, homeOnly));
123
+ // Deliberate order: the flag goes first. If the settings write then fails, the old exit has lost its exemption
124
+ // (it fails closed, back under the VPN leak check); the reverse order could leave an exempt exit with no role.
123
125
  this.clearHomeFlagLeaving(this.settingsRepo.get().captchaFallbackEgressId, next.captchaFallbackEgressId);
124
126
  this.settingsRepo.update(next);
125
127
  await this.vpn.reload();
@@ -100,12 +100,8 @@ const egressLayout = recordListLayout({
100
100
  prefix: 'egress.',
101
101
  fields: ['password', 'privateKey'],
102
102
  });
103
- /** Webhook headers are an object in the file and one JSON text through the port. */
104
- const webhookLayout = recordListLayout({
105
- collection: 'webhooks',
106
- prefix: 'webhook.',
107
- fields: ['headers'],
108
- listKey: 'webhooks',
103
+ /** An object in the file, one JSON text through the port. */
104
+ const headersCodec = {
109
105
  encode: (_field, value) => {
110
106
  try {
111
107
  const parsed = JSON.parse(value);
@@ -116,5 +112,21 @@ const webhookLayout = recordListLayout({
116
112
  }
117
113
  },
118
114
  decode: (_field, value) => (isObject(value) ? JSON.stringify(value) : undefined),
115
+ };
116
+ /** Webhook headers are an object in the file and one JSON text through the port. */
117
+ const webhookLayout = recordListLayout({
118
+ collection: 'webhooks',
119
+ prefix: 'webhook.',
120
+ fields: ['headers'],
121
+ listKey: 'webhooks',
122
+ ...headersCodec,
123
+ });
124
+ /** A site's HTTP headers can carry a session cookie: same shape as a webhook's. */
125
+ const siteLayout = recordListLayout({
126
+ collection: 'sites',
127
+ prefix: 'site.',
128
+ fields: ['headers'],
129
+ listKey: 'sites',
130
+ ...headersCodec,
119
131
  });
120
- exports.DOCKER_SECRET_LAYOUT = [settingsLayout, egressLayout, webhookLayout];
132
+ exports.DOCKER_SECRET_LAYOUT = [settingsLayout, egressLayout, webhookLayout, siteLayout];
@@ -22,4 +22,6 @@ export declare const secretNames: {
22
22
  readonly egressPrivateKey: (egressId: string) => string;
23
23
  /** The webhook's headers as one JSON object text. */
24
24
  readonly webhookHeaders: (webhookId: string) => string;
25
+ /** A generic site's HTTP headers as one JSON object text (they can carry a session cookie). */
26
+ readonly siteHeaders: (siteId: string) => string;
25
27
  };
@@ -10,4 +10,6 @@ exports.secretNames = {
10
10
  egressPrivateKey: (egressId) => `egress.${egressId}.privateKey`,
11
11
  /** The webhook's headers as one JSON object text. */
12
12
  webhookHeaders: (webhookId) => `webhook.${webhookId}.headers`,
13
+ /** A generic site's HTTP headers as one JSON object text (they can carry a session cookie). */
14
+ siteHeaders: (siteId) => `site.${siteId}.headers`,
13
15
  };
@@ -12,5 +12,10 @@ export declare class SettingsRepository {
12
12
  get(): AppSettings;
13
13
  save(settings: AppSettings): void;
14
14
  update(partial: Partial<AppSettings>): AppSettings;
15
+ /**
16
+ * Moves a credential a state file from an older version still carries into the secret store, leaving none behind.
17
+ * Returns how many it moved; a second run finds none.
18
+ */
19
+ migratePlaintext(): number;
15
20
  private keepSecret;
16
21
  }
@@ -29,6 +29,9 @@ exports.DEFAULT_SETTINGS = {
29
29
  autoBind: false,
30
30
  autoStart: false,
31
31
  moveEp: true,
32
+ libraryOpenPermissions: true,
33
+ libraryDirMode: '0777',
34
+ libraryFileMode: '0666',
32
35
  renameEp: true,
33
36
  tagsMode: 'BLACKLIST',
34
37
  logLevel: 'info',
@@ -70,10 +73,12 @@ let SettingsRepository = class SettingsRepository {
70
73
  }
71
74
  save(settings) {
72
75
  const { sonarrApiKey, radarrApiKey, plexToken, ...rest } = settings;
73
- this.repo.write(rest);
76
+ // Secrets first, state after: a host that writes behind persists them in this order, so a crash between the two
77
+ // leaves a credential in both places (harmless) and never in neither.
74
78
  this.keepSecret(secret_store_1.secretNames.sonarrApiKey, sonarrApiKey);
75
79
  this.keepSecret(secret_store_1.secretNames.radarrApiKey, radarrApiKey);
76
80
  this.keepSecret(secret_store_1.secretNames.plexToken, plexToken);
81
+ this.repo.write(rest);
77
82
  }
78
83
  update(partial) {
79
84
  const current = this.get();
@@ -81,6 +86,18 @@ let SettingsRepository = class SettingsRepository {
81
86
  this.save(updated);
82
87
  return updated;
83
88
  }
89
+ /**
90
+ * Moves a credential a state file from an older version still carries into the secret store, leaving none behind.
91
+ * Returns how many it moved; a second run finds none.
92
+ */
93
+ migratePlaintext() {
94
+ const raw = { ...this.repo.read() };
95
+ const keys = ['sonarrApiKey', 'radarrApiKey', 'plexToken'];
96
+ const moved = keys.filter((k) => typeof raw[k] === 'string' && raw[k] !== '').length;
97
+ if (moved > 0)
98
+ this.save(this.get());
99
+ return moved;
100
+ }
84
101
  keepSecret(name, value) {
85
102
  if (this.secrets.get(name) !== value)
86
103
  this.secrets.set(name, value);
@@ -1,10 +1,15 @@
1
1
  import { GenericSiteConfig } from '../types';
2
+ import { SecretStore } from '../ports/secret-store';
2
3
  import { StateStore } from '../ports/state-store';
3
4
  export declare class SitesRepository {
5
+ private readonly secrets;
4
6
  private readonly repo;
5
- constructor(state: StateStore);
7
+ constructor(state: StateStore, secrets: SecretStore);
6
8
  getAll(): GenericSiteConfig[];
7
9
  getById(id: string): GenericSiteConfig | undefined;
8
10
  save(site: GenericSiteConfig): void;
9
11
  delete(id: string): boolean;
12
+ /** Moves headers an older state file still holds into the secret store. Returns how many sites it moved; a second run finds none. */
13
+ migratePlaintext(): number;
14
+ private hydrate;
10
15
  }
@@ -15,25 +15,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.SitesRepository = void 0;
16
16
  const common_1 = require("@nestjs/common");
17
17
  const tokens_1 = require("../ports/tokens");
18
+ const secret_store_1 = require("../ports/secret-store");
19
+ const webhook_repository_1 = require("./webhook.repository");
18
20
  const state_store_1 = require("../ports/state-store");
19
21
  let SitesRepository = class SitesRepository {
20
- constructor(state) {
22
+ constructor(state, secrets) {
23
+ this.secrets = secrets;
21
24
  this.repo = new state_store_1.StoredCollection(state, 'sites', { sites: [] });
22
25
  }
23
26
  getAll() {
24
- return this.repo.read().sites;
27
+ return this.repo.read().sites.map((s) => this.hydrate(s));
25
28
  }
26
29
  getById(id) {
27
30
  return this.getAll().find((s) => s.id === id);
28
31
  }
29
32
  save(site) {
33
+ const { headers, ...stored } = site;
34
+ // Secret first, state after (see SettingsRepository.save): a crash between the two never loses the headers.
35
+ const name = secret_store_1.secretNames.siteHeaders(site.id);
36
+ if (headers && Object.keys(headers).length > 0) {
37
+ const text = JSON.stringify(headers);
38
+ if (this.secrets.get(name) !== text)
39
+ this.secrets.set(name, text);
40
+ }
41
+ else if (this.secrets.get(name) !== undefined) {
42
+ this.secrets.delete(name);
43
+ }
30
44
  const store = this.repo.read();
31
45
  const idx = store.sites.findIndex((s) => s.id === site.id);
32
46
  if (idx >= 0) {
33
- store.sites[idx] = site;
47
+ store.sites[idx] = stored;
34
48
  }
35
49
  else {
36
- store.sites.push(site);
50
+ store.sites.push(stored);
37
51
  }
38
52
  this.repo.write(store);
39
53
  }
@@ -43,14 +57,29 @@ let SitesRepository = class SitesRepository {
43
57
  store.sites = store.sites.filter((s) => s.id !== id);
44
58
  if (store.sites.length < before) {
45
59
  this.repo.write(store);
60
+ this.secrets.delete(secret_store_1.secretNames.siteHeaders(id));
46
61
  return true;
47
62
  }
48
63
  return false;
49
64
  }
65
+ /** Moves headers an older state file still holds into the secret store. Returns how many sites it moved; a second run finds none. */
66
+ migratePlaintext() {
67
+ const legacy = this.repo.read().sites.filter((s) => s.headers !== undefined);
68
+ for (const stored of legacy)
69
+ this.save(this.hydrate(stored));
70
+ return legacy.filter((s) => Object.keys(s.headers ?? {}).length > 0).length;
71
+ }
72
+ hydrate(stored) {
73
+ const text = this.secrets.get(secret_store_1.secretNames.siteHeaders(stored.id));
74
+ const headers = text === undefined ? stored.headers : (0, webhook_repository_1.parseHeaders)(text);
75
+ const { headers: _own, ...rest } = stored;
76
+ return headers && Object.keys(headers).length > 0 ? { ...rest, headers } : rest;
77
+ }
50
78
  };
51
79
  exports.SitesRepository = SitesRepository;
52
80
  exports.SitesRepository = SitesRepository = __decorate([
53
81
  (0, common_1.Injectable)(),
54
82
  __param(0, (0, common_1.Inject)(tokens_1.STATE_STORE)),
55
- __metadata("design:paramtypes", [Object])
83
+ __param(1, (0, common_1.Inject)(tokens_1.SECRET_STORE)),
84
+ __metadata("design:paramtypes", [Object, Object])
56
85
  ], SitesRepository);
@@ -17,5 +17,8 @@ export declare class WebhookRepository {
17
17
  getById(id: string): WebhookConfig | undefined;
18
18
  save(webhook: WebhookConfig): void;
19
19
  delete(id: string): boolean;
20
+ /** Moves headers an older state file still holds into the secret store. Returns how many webhooks it moved; a second run finds none. */
21
+ migratePlaintext(): number;
20
22
  private hydrate;
21
23
  }
24
+ export declare function parseHeaders(text: string): Record<string, string>;
@@ -13,6 +13,7 @@ var __param = (this && this.__param) || function (paramIndex, decorator) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.WebhookRepository = void 0;
16
+ exports.parseHeaders = parseHeaders;
16
17
  const common_1 = require("@nestjs/common");
17
18
  const tokens_1 = require("../ports/tokens");
18
19
  const secret_store_1 = require("../ports/secret-store");
@@ -30,6 +31,11 @@ let WebhookRepository = class WebhookRepository {
30
31
  }
31
32
  save(webhook) {
32
33
  const { headers, ...stored } = webhook;
34
+ // Secret first, state after (see SettingsRepository.save): a crash between the two never loses the headers.
35
+ const text = JSON.stringify(headers ?? {});
36
+ const name = secret_store_1.secretNames.webhookHeaders(webhook.id);
37
+ if (this.secrets.get(name) !== text)
38
+ this.secrets.set(name, text);
33
39
  const store = this.repo.read();
34
40
  const idx = store.webhooks.findIndex((w) => w.id === webhook.id);
35
41
  if (idx >= 0) {
@@ -39,10 +45,6 @@ let WebhookRepository = class WebhookRepository {
39
45
  store.webhooks.push(stored);
40
46
  }
41
47
  this.repo.write(store);
42
- const text = JSON.stringify(headers ?? {});
43
- const name = secret_store_1.secretNames.webhookHeaders(webhook.id);
44
- if (this.secrets.get(name) !== text)
45
- this.secrets.set(name, text);
46
48
  }
47
49
  delete(id) {
48
50
  const store = this.repo.read();
@@ -55,6 +57,13 @@ let WebhookRepository = class WebhookRepository {
55
57
  }
56
58
  return false;
57
59
  }
60
+ /** Moves headers an older state file still holds into the secret store. Returns how many webhooks it moved; a second run finds none. */
61
+ migratePlaintext() {
62
+ const legacy = this.repo.read().webhooks.filter((w) => w.headers !== undefined);
63
+ for (const stored of legacy)
64
+ this.save(this.hydrate(stored));
65
+ return legacy.filter((w) => Object.keys(w.headers ?? {}).length > 0).length;
66
+ }
58
67
  hydrate(stored) {
59
68
  const text = this.secrets.get(secret_store_1.secretNames.webhookHeaders(stored.id));
60
69
  return { ...stored, headers: text === undefined ? stored.headers ?? {} : parseHeaders(text) };
@@ -65,6 +65,7 @@ const captcha_controller_1 = require("./controllers/captcha.controller");
65
65
  const plex_controller_1 = require("./controllers/plex.controller");
66
66
  const plex_service_1 = require("./services/plex.service");
67
67
  const captcha_service_1 = require("./services/captcha.service");
68
+ const secret_migration_service_1 = require("./services/secret-migration.service");
68
69
  const captcha_challenges_repository_1 = require("./repositories/captcha-challenges.repository");
69
70
  const CONTROLLERS = [
70
71
  core_controller_1.CoreController,
@@ -121,6 +122,7 @@ const PROVIDERS = [
121
122
  vpn_service_1.VpnService,
122
123
  captcha_service_1.CaptchaService,
123
124
  book_adapter_registry_service_1.BookAdapterRegistryService,
125
+ secret_migration_service_1.SecretMigrationService,
124
126
  ];
125
127
  let ScraperModule = class ScraperModule {
126
128
  };
@@ -301,6 +301,10 @@ 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
+ /** mkdir the library dir, move the file in, and open permissions on both (a chmod failure never fails the move). */
307
+ private placeInLibrary;
304
308
  private moveFile;
305
309
  /**
306
310
  * Strip filesystem-reserved characters from a filename component.