This repository has been archived by the owner on Apr 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add local file syncing support
- Loading branch information
Showing
11 changed files
with
389 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { SyncData } from '@esync/data'; | ||
import { File, FileCreateFlags } from '@imports/Gio-2.0'; | ||
import { SyncOperationStatus, SyncProvider } from '../types'; | ||
|
||
export class Local implements SyncProvider { | ||
private backupfileLocation: string; | ||
|
||
constructor(backupfileLocation: string) { | ||
this.backupfileLocation = backupfileLocation; | ||
} | ||
|
||
async save(syncData: SyncData): Promise<SyncOperationStatus> { | ||
if (!this.backupfileLocation) { | ||
throw new Error('Please select a backup file location from preferences'); | ||
} | ||
const backupFile = File.new_for_uri(this.backupfileLocation); | ||
if (!backupFile.query_exists(null)) { | ||
throw new Error(`Failed to backup settings. ${this.backupfileLocation} does not exist`); | ||
} | ||
|
||
backupFile.replace_contents( | ||
imports.byteArray.fromString(JSON.stringify(syncData)), | ||
null, | ||
false, | ||
FileCreateFlags.REPLACE_DESTINATION, | ||
null, | ||
); | ||
|
||
return SyncOperationStatus.SUCCESS; | ||
} | ||
|
||
async read(): Promise<SyncData> { | ||
const backupFile = File.new_for_uri(this.backupfileLocation); | ||
if (!backupFile.query_exists(null)) { | ||
throw new Error(`Failed to read settings from backup. ${this.backupfileLocation} does not exist`); | ||
} | ||
|
||
const [status, syncDataBytes] = backupFile.load_contents(null); | ||
|
||
if (!syncDataBytes.length || !status) { | ||
throw new Error(`Failed to read settings from backup. ${this.backupfileLocation} is corrupted`); | ||
} | ||
|
||
try { | ||
return JSON.parse(imports.byteArray.toString(syncDataBytes)); | ||
} catch (err) { | ||
throw new Error(`${this.backupfileLocation} is not a json file`); | ||
} | ||
} | ||
|
||
getName(): string { | ||
return 'Local'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,25 @@ | ||
import { SyncData } from '@esync/data'; | ||
|
||
export enum ApiOperationStatus { | ||
export enum SyncOperationStatus { | ||
SUCCESS, | ||
FAIL, | ||
} | ||
|
||
export enum ApiEvent { | ||
UPLOAD = 'UPLOAD', | ||
UPLOAD_FINISHED = 'UPLOAD_FINISHED', | ||
DOWNLOAD = 'DOWNLOAD', | ||
DOWNLOAD_FINISHED = 'DOWNLOAD_FINISHED', | ||
export enum SyncEvent { | ||
SAVE = 'SAVE', | ||
SAVE_FINISHED = 'SAVE_FINISHED', | ||
READ = 'READ', | ||
READ_FINISHED = 'READ_FINISHED', | ||
} | ||
|
||
export enum ApiProviderType { | ||
export enum SyncProviderType { | ||
GITHUB, | ||
GITLAB, | ||
LOCAL, | ||
} | ||
|
||
export interface ApiProvider { | ||
upload(syncData: SyncData): Promise<ApiOperationStatus>; | ||
download(): Promise<SyncData>; | ||
export interface SyncProvider { | ||
save(syncData: SyncData): Promise<SyncOperationStatus>; | ||
read(): Promise<SyncData>; | ||
getName(): string; | ||
} |
Oops, something went wrong.