|
| 1 | +import { FileBackupsDevice, FileBackupsMapping } from '@web/Device/DesktopSnjsExports' |
| 2 | +import { AppState } from 'app/application' |
| 3 | +import { StoreKeys } from '../Store' |
| 4 | +import { |
| 5 | + ensureDirectoryExists, |
| 6 | + moveDirContents, |
| 7 | + openDirectoryPicker, |
| 8 | + readJSONFile, |
| 9 | + writeFile, |
| 10 | + writeJSONFile, |
| 11 | +} from '../Utils/FileUtils' |
| 12 | +import { FileDownloader } from './FileDownloader' |
| 13 | +import { shell } from 'electron' |
| 14 | + |
| 15 | +export const FileBackupsConstantsV1 = { |
| 16 | + Version: '1.0.0', |
| 17 | + MetadataFileName: 'metadata.sn.json', |
| 18 | + BinaryFileName: 'file.encrypted', |
| 19 | +} |
| 20 | + |
| 21 | +export class FilesBackupManager implements FileBackupsDevice { |
| 22 | + constructor(private appState: AppState) {} |
| 23 | + |
| 24 | + public isFilesBackupsEnabled(): Promise<boolean> { |
| 25 | + return Promise.resolve(this.appState.store.get(StoreKeys.FileBackupsEnabled)) |
| 26 | + } |
| 27 | + |
| 28 | + public async enableFilesBackups(): Promise<void> { |
| 29 | + const currentLocation = await this.getFilesBackupsLocation() |
| 30 | + |
| 31 | + if (!currentLocation) { |
| 32 | + const result = await this.changeFilesBackupsLocation() |
| 33 | + |
| 34 | + if (!result) { |
| 35 | + return |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + this.appState.store.set(StoreKeys.FileBackupsEnabled, true) |
| 40 | + |
| 41 | + const mapping = this.getMappingFileFromDisk() |
| 42 | + |
| 43 | + if (!mapping) { |
| 44 | + await this.saveFilesBackupsMappingFile(this.defaultMappingFileValue()) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public disableFilesBackups(): Promise<void> { |
| 49 | + this.appState.store.set(StoreKeys.FileBackupsEnabled, false) |
| 50 | + |
| 51 | + return Promise.resolve() |
| 52 | + } |
| 53 | + |
| 54 | + public async changeFilesBackupsLocation(): Promise<string | undefined> { |
| 55 | + const newPath = await openDirectoryPicker() |
| 56 | + |
| 57 | + if (!newPath) { |
| 58 | + return undefined |
| 59 | + } |
| 60 | + |
| 61 | + const oldPath = await this.getFilesBackupsLocation() |
| 62 | + |
| 63 | + if (oldPath) { |
| 64 | + await moveDirContents(oldPath, newPath) |
| 65 | + } |
| 66 | + |
| 67 | + this.appState.store.set(StoreKeys.FileBackupsLocation, newPath) |
| 68 | + |
| 69 | + return newPath |
| 70 | + } |
| 71 | + |
| 72 | + public getFilesBackupsLocation(): Promise<string> { |
| 73 | + return Promise.resolve(this.appState.store.get(StoreKeys.FileBackupsLocation)) |
| 74 | + } |
| 75 | + |
| 76 | + private getMappingFileLocation(): string { |
| 77 | + const base = this.appState.store.get(StoreKeys.FileBackupsLocation) |
| 78 | + return `${base}/info.json` |
| 79 | + } |
| 80 | + |
| 81 | + private async getMappingFileFromDisk(): Promise<FileBackupsMapping | undefined> { |
| 82 | + return readJSONFile<FileBackupsMapping>(this.getMappingFileLocation()) |
| 83 | + } |
| 84 | + |
| 85 | + private defaultMappingFileValue(): FileBackupsMapping { |
| 86 | + return { version: FileBackupsConstantsV1.Version, files: {} } |
| 87 | + } |
| 88 | + |
| 89 | + async getFilesBackupsMappingFile(): Promise<FileBackupsMapping> { |
| 90 | + const data = await this.getMappingFileFromDisk() |
| 91 | + |
| 92 | + if (!data) { |
| 93 | + return this.defaultMappingFileValue() |
| 94 | + } |
| 95 | + |
| 96 | + return data |
| 97 | + } |
| 98 | + |
| 99 | + async openFilesBackupsLocation(): Promise<void> { |
| 100 | + const location = await this.getFilesBackupsLocation() |
| 101 | + |
| 102 | + shell.openPath(location) |
| 103 | + } |
| 104 | + |
| 105 | + async saveFilesBackupsMappingFile(file: FileBackupsMapping): Promise<'success' | 'failed'> { |
| 106 | + await writeJSONFile(this.getMappingFileLocation(), file) |
| 107 | + |
| 108 | + return 'success' |
| 109 | + } |
| 110 | + |
| 111 | + async saveFilesBackupsFile( |
| 112 | + uuid: string, |
| 113 | + metaFile: string, |
| 114 | + downloadRequest: { |
| 115 | + chunkSizes: number[] |
| 116 | + valetToken: string |
| 117 | + url: string |
| 118 | + }, |
| 119 | + ): Promise<'success' | 'failed'> { |
| 120 | + const backupsDir = await this.getFilesBackupsLocation() |
| 121 | + |
| 122 | + const fileDir = `${backupsDir}/${uuid}` |
| 123 | + const metaFilePath = `${fileDir}/${FileBackupsConstantsV1.MetadataFileName}` |
| 124 | + const binaryPath = `${fileDir}/${FileBackupsConstantsV1.BinaryFileName}` |
| 125 | + |
| 126 | + await ensureDirectoryExists(fileDir) |
| 127 | + |
| 128 | + await writeFile(metaFilePath, metaFile) |
| 129 | + |
| 130 | + const downloader = new FileDownloader( |
| 131 | + downloadRequest.chunkSizes, |
| 132 | + downloadRequest.valetToken, |
| 133 | + downloadRequest.url, |
| 134 | + binaryPath, |
| 135 | + ) |
| 136 | + |
| 137 | + const result = await downloader.run() |
| 138 | + |
| 139 | + if (result === 'success') { |
| 140 | + const mapping = await this.getFilesBackupsMappingFile() |
| 141 | + |
| 142 | + mapping.files[uuid] = { |
| 143 | + backedUpOn: new Date(), |
| 144 | + absolutePath: fileDir, |
| 145 | + relativePath: uuid, |
| 146 | + metadataFileName: FileBackupsConstantsV1.MetadataFileName, |
| 147 | + binaryFileName: FileBackupsConstantsV1.BinaryFileName, |
| 148 | + version: FileBackupsConstantsV1.Version, |
| 149 | + } |
| 150 | + |
| 151 | + await this.saveFilesBackupsMappingFile(mapping) |
| 152 | + } |
| 153 | + |
| 154 | + return result |
| 155 | + } |
| 156 | +} |
0 commit comments