app.service.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. import { Injectable } from '@nestjs/common';
  2. import { ConfigService } from './config.service';
  3. import { DatasetsService } from './datasets.service';
  4. import { DbService } from './db.service';
  5. import { HandbrakeService } from './handbrake.service';
  6. import { MaintenanceService } from './maintenance.service';
  7. import { TaskQueueService } from './task-queue.service';
  8. import { WatcherService } from './watcher.service';
  9. @Injectable()
  10. export class AppService {
  11. constructor(
  12. private readonly db: DbService,
  13. private readonly watcher: WatcherService,
  14. private readonly config: ConfigService,
  15. private readonly maintenance: MaintenanceService,
  16. private readonly handbrake: HandbrakeService,
  17. private readonly datasets: DatasetsService,
  18. private readonly taskQueue: TaskQueueService,
  19. ) {}
  20. listDatasets() {
  21. return this.datasets.getEnabledDatasetPaths();
  22. }
  23. listAllDatasets() {
  24. return this.datasets.getAllDatasetNames();
  25. }
  26. getDatasetConfig() {
  27. return this.datasets.getDatasetConfig();
  28. }
  29. getTotalSuccessfulFiles() {
  30. const datasetNames = this.datasets.getAllDatasetNames();
  31. let total = 0;
  32. for (const datasetName of datasetNames) {
  33. if (datasetName) {
  34. const files = this.db.getAllFiles(datasetName);
  35. total += files.length;
  36. }
  37. }
  38. return total;
  39. }
  40. getTotalProcessedFiles() {
  41. const datasetNames = this.datasets.getAllDatasetNames();
  42. let total = 0;
  43. for (const datasetName of datasetNames) {
  44. if (datasetName) {
  45. const files = this.db.getAllFiles(datasetName);
  46. total += files.length;
  47. }
  48. }
  49. return total;
  50. }
  51. deleteExpiredFiles(days?: number) {
  52. return this.db.deleteExpiredFiles(days);
  53. }
  54. migrateJsonToSqlite(opts?: { datasets?: string[]; dataDir?: string }) {
  55. return this.db.migrateJsonToSqlite(opts);
  56. }
  57. processWithHandbrake(input: string, output: string, preset: string) {
  58. return this.handbrake.processWithHandbrake(input, output, preset);
  59. }
  60. // Task queue methods
  61. createTask(taskData: {
  62. dataset: string;
  63. input: string;
  64. output: string;
  65. preset: string;
  66. priority?: number;
  67. status?: string;
  68. }) {
  69. return this.taskQueue.createTask(taskData);
  70. }
  71. getQueueStatus() {
  72. return this.taskQueue.getQueueStatus();
  73. }
  74. getQueueSettings() {
  75. return this.taskQueue.getQueueSettings();
  76. }
  77. updateQueueSettings(settings: any) {
  78. return this.taskQueue.updateQueueSettings(settings);
  79. }
  80. getAllTasks() {
  81. return this.db.getAllTasks();
  82. }
  83. getTaskById(id: number) {
  84. return this.db.getTaskById(id);
  85. }
  86. updateTask(id: number, updates: any) {
  87. return this.db.updateTask(id, updates);
  88. }
  89. deleteTask(id: number) {
  90. return this.db.deleteTask(id);
  91. }
  92. getHandbrakePresets() {
  93. return this.handbrake.getPresetList();
  94. }
  95. cleanup(file: string, dirs: string[]) {
  96. return this.maintenance.cleanup(file, dirs);
  97. }
  98. purge(dirs: string[], dayMs?: number, cleanerMs?: number) {
  99. return this.maintenance.purge(dirs, dayMs, cleanerMs);
  100. }
  101. prune(dirs: string[]) {
  102. return this.maintenance.prune(dirs);
  103. }
  104. getSettings(key?: string, defaultValue?: any) {
  105. return this.config.getSettings(key, defaultValue);
  106. }
  107. setSettings(settings: Record<string, any>) {
  108. return this.config.setSettings(settings);
  109. }
  110. deleteSetting(key: string) {
  111. return this.config.deleteSetting(key);
  112. }
  113. getConfigFile(name: string) {
  114. return this.config.getConfigFile(name);
  115. }
  116. listConfigs() {
  117. return this.config.listConfigs();
  118. }
  119. startWatcher(watches: string[], options?: any) {
  120. return this.watcher.start(watches, options);
  121. }
  122. async stopWatcher() {
  123. return this.watcher.stop();
  124. }
  125. watcherStatus() {
  126. return this.watcher.status();
  127. }
  128. startTaskProcessing() {
  129. return this.taskQueue.start();
  130. }
  131. stopTaskProcessing() {
  132. return this.taskQueue.stop();
  133. }
  134. taskProcessingStatus() {
  135. return this.taskQueue.getQueueStatus();
  136. }
  137. // Task maintenance methods
  138. cleanupTasksByStatus(status: string, olderThanDays?: number) {
  139. return this.db.deleteTasksByStatus(status, olderThanDays);
  140. }
  141. cleanupOldTasks(days: number) {
  142. return this.db.deleteTasksOlderThan(days);
  143. }
  144. getTaskStats() {
  145. return this.db.getTaskStats();
  146. }
  147. archiveOldTasks(daysOld: number = 30): { changes?: number } {
  148. return this.db.archiveOldTasks(daysOld);
  149. }
  150. // Purge all tasks
  151. purgeAllTasks() {
  152. return this.db.purgeAllTasks();
  153. }
  154. // Scheduled maintenance
  155. scheduledTaskCleanup() {
  156. return this.maintenance.scheduledTaskCleanup();
  157. }
  158. getHello(): string {
  159. return 'Hello World!';
  160. }
  161. findFile(dataset: string, file: string) {
  162. return this.db.findFile(dataset, file);
  163. }
  164. setFile(dataset: string, file: string, payload: any) {
  165. return this.db.setFile(dataset, file, payload);
  166. }
  167. removeFile(dataset: string, file: string, soft = true) {
  168. return this.db.removeFile(dataset, file, soft);
  169. }
  170. clearAllFiles() {
  171. return this.db.clearAllFiles();
  172. }
  173. getAllFiles(dataset: string) {
  174. return this.db.getAllFiles(dataset);
  175. }
  176. getDeletedOlderThan(dataset: string, isoDate: string) {
  177. return this.db.getDeletedOlderThan(dataset, isoDate);
  178. }
  179. }