backup.handlers.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { ipcMain, BrowserWindow } from 'electron'
  2. import * as fs from 'fs'
  3. import * as path from 'path'
  4. import { IpcChannels } from '@shared/ipc-channels'
  5. import * as backup from '../services/backup.service'
  6. import * as library from '../services/library.service'
  7. import * as steam from '../services/steam.service'
  8. import { getAutoUnsubscribeAfterBackup } from '../services/config.service'
  9. export interface BackupItemResult {
  10. ok: boolean
  11. alreadyBackedUp: boolean
  12. unsubscribed: boolean
  13. }
  14. const inFlight = new Set<string>()
  15. async function backupOne(id: string, win: BrowserWindow): Promise<BackupItemResult> {
  16. if (inFlight.has(id)) throw new Error(`Wallpaper ${id} is already being backed up`)
  17. inFlight.add(id)
  18. try {
  19. const wallpaper = library.getWallpaper(id)
  20. if (!wallpaper) throw new Error(`Wallpaper ${id} is not in the library`)
  21. if (wallpaper.source !== 'workshop') throw new Error(`Wallpaper ${id} is not a workshop wallpaper`)
  22. if (wallpaper.downloading) throw new Error(`Wallpaper ${id} is still downloading`)
  23. if (!wallpaper.localPath || !(await backup.hasBackupFiles(wallpaper.localPath))) {
  24. throw new Error(`Wallpaper ${id} has no complete local files to back up`)
  25. }
  26. const sourcePath = wallpaper.localPath
  27. let backupDir = backup.resolveBackupDir(wallpaper)
  28. const alreadyBackedUp =
  29. !!backupDir && (await backup.hasBackupFiles(backupDir)) && (await backup.backupMatchesSource(sourcePath, backupDir))
  30. if (!alreadyBackedUp) {
  31. backupDir = await backup.backupWallpaper(id, sourcePath, win)
  32. }
  33. library.updateWallpaper(id, { backedUp: true, backupDir: backupDir!, backupOutdated: false })
  34. let unsubscribed = false
  35. if (getAutoUnsubscribeAfterBackup()) {
  36. if (!(await backup.hasBackupFiles(backupDir))) {
  37. throw new Error(`Backup of ${id} is missing after copying, not unsubscribing`)
  38. }
  39. await steam.unsubscribeFromItem(BigInt(id))
  40. library.updateWallpaper(id, {
  41. source: 'backup',
  42. localPath: backupDir!,
  43. previewLocal: await backup.previewInBackup(wallpaper, backupDir!),
  44. subscribed: false,
  45. downloading: false,
  46. downloadFailed: false
  47. })
  48. if (path.resolve(sourcePath) !== path.resolve(backupDir!)) {
  49. try {
  50. await fs.promises.rm(sourcePath, { recursive: true, force: true })
  51. } catch {
  52. }
  53. }
  54. unsubscribed = true
  55. }
  56. return { ok: true, alreadyBackedUp, unsubscribed }
  57. } finally {
  58. inFlight.delete(id)
  59. }
  60. }
  61. export function registerBackupHandlers(win: BrowserWindow): void {
  62. ipcMain.handle(IpcChannels.BACKUP_ITEM, async (_e, itemId: string) => {
  63. return backupOne(itemId, win)
  64. })
  65. ipcMain.handle(IpcChannels.BACKUP_SELECTION, async (_e, itemIds: string[]) => {
  66. let backedUp = 0
  67. let alreadyBackedUp = 0
  68. let failed = 0
  69. let unsubscribed = 0
  70. for (const id of itemIds) {
  71. try {
  72. const result = await backupOne(id, win)
  73. if (result.alreadyBackedUp) alreadyBackedUp++
  74. else backedUp++
  75. if (result.unsubscribed) unsubscribed++
  76. } catch (err) {
  77. console.error(`[Backup] ${id}:`, err)
  78. failed++
  79. }
  80. }
  81. return { backedUp, alreadyBackedUp, failed, unsubscribed }
  82. })
  83. ipcMain.handle(IpcChannels.BACKUP_SCAN, async () => {
  84. const result = await backup.scanBackupFolder()
  85. await backup.checkOutdatedBackups()
  86. return result
  87. })
  88. ipcMain.handle(IpcChannels.BACKUP_REMOVE, async (_e, itemIds: string[]) => {
  89. let removed = 0
  90. let failed = 0
  91. const keptDirs: string[] = []
  92. for (const id of itemIds) {
  93. try {
  94. const { keptDir } = await backup.removeBackup(id)
  95. if (keptDir) keptDirs.push(keptDir)
  96. removed++
  97. } catch (err) {
  98. console.error(`[Backup] Failed to remove backup of ${id}:`, err)
  99. failed++
  100. }
  101. }
  102. return { removed, failed, keptDirs }
  103. })
  104. }