implement backup import
This commit is contained in:
@@ -107,7 +107,7 @@ export default class FileTransferModel {
|
||||
parsed,
|
||||
FileDataReference,
|
||||
);
|
||||
if (isFileData == false) return;
|
||||
if (isFileData == false) throw "not file data";
|
||||
|
||||
const fileData: FileData = parsed;
|
||||
this.storageModel.write(fileData.path, fileData.body);
|
||||
@@ -149,15 +149,15 @@ export default class FileTransferModel {
|
||||
});
|
||||
}
|
||||
const rawBackup = stringify(files);
|
||||
console.log(rawBackup);
|
||||
const encrypted = await encryptString(rawBackup, passphrase);
|
||||
const blob = new Blob([encrypted], {type: "text/plain"});
|
||||
return blob;
|
||||
};
|
||||
|
||||
readonly prepareFileForSending = (filePath: string[]): string => {
|
||||
if (this.transferData == undefined) return;
|
||||
const fileContent: string | null = this.storageModel.read(filePath);
|
||||
if (fileContent == null) return;
|
||||
const fileContent: string | undefined = this.storageModel.read(filePath);
|
||||
if (fileContent == null) return "";
|
||||
|
||||
const fileData: FileData = {
|
||||
path: filePath,
|
||||
|
||||
@@ -30,6 +30,7 @@ export function DataTransferModalWrapper(
|
||||
{ExportFileSelectionModal(coreViewModel, fileTransferViewModel)}
|
||||
{ExportModal(coreViewModel, fileTransferViewModel)}
|
||||
{ImportModal(coreViewModel, fileTransferViewModel)}
|
||||
{ImportDecryptionDataModal(coreViewModel, fileTransferViewModel)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -97,22 +98,12 @@ function DirectionSelectionModal(
|
||||
}
|
||||
</h2>
|
||||
|
||||
<p
|
||||
class="error"
|
||||
toggle:hidden={connectionViewModel.isConnected}
|
||||
>
|
||||
{
|
||||
coreViewModel.translations.dataTransferModal
|
||||
.notConnectedError
|
||||
}
|
||||
</p>
|
||||
|
||||
<div
|
||||
class="flex-column gap content-margin-bottom"
|
||||
toggle:hidden={isDisconnected}
|
||||
>
|
||||
<button
|
||||
class="tile"
|
||||
toggle:disabled={isDisconnected}
|
||||
on:click={
|
||||
fileTransferViewModel.showFileSelectionModal
|
||||
}
|
||||
@@ -131,6 +122,7 @@ function DirectionSelectionModal(
|
||||
</button>
|
||||
<button
|
||||
class="tile"
|
||||
toggle:disabled={isDisconnected}
|
||||
on:click={
|
||||
fileTransferViewModel.showTransferDataInputModal
|
||||
}
|
||||
@@ -772,7 +764,7 @@ function ImportModal(
|
||||
[fileTransferViewModel.presentedModal],
|
||||
() =>
|
||||
fileTransferViewModel.presentedModal.value ==
|
||||
FileTransferModals.Import,
|
||||
FileTransferModals.ImportSelection,
|
||||
);
|
||||
|
||||
return (
|
||||
@@ -786,28 +778,101 @@ function ImportModal(
|
||||
}
|
||||
</h2>
|
||||
|
||||
<p
|
||||
class="secondary"
|
||||
subscribe:innerText={
|
||||
fileTransferViewModel.filesReceivedText
|
||||
<input id="file-transfer-input" type="file" on:change={fileTransferViewModel.updateImportSelection}></input>
|
||||
</main>
|
||||
<div class="flex-row width-100">
|
||||
<button
|
||||
class="flex"
|
||||
on:click={
|
||||
fileTransferViewModel.showImportModal
|
||||
}
|
||||
></p>
|
||||
|
||||
<hr></hr>
|
||||
|
||||
<div
|
||||
class="tile flex-column align-start"
|
||||
children:append={[
|
||||
fileTransferViewModel.filePathsReceived,
|
||||
StringToTextSpan,
|
||||
]}
|
||||
></div>
|
||||
</main>
|
||||
<button on:click={ViewController.reload}>
|
||||
{coreViewModel.translations.general.reloadAppButton}
|
||||
<span class="icon">refresh</span>
|
||||
</button>
|
||||
>
|
||||
{coreViewModel.translations.general.backButton}
|
||||
</button>
|
||||
<button
|
||||
class="primary flex"
|
||||
on:click={fileTransferViewModel.importFile}
|
||||
toggle:disabled={
|
||||
fileTransferViewModel.cannotImport
|
||||
}
|
||||
>
|
||||
{coreViewModel.translations.dataTransferModal.importFileButton}
|
||||
<span class="icon">arrow_forward</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
);
|
||||
}
|
||||
|
||||
function ImportDecryptionDataModal(
|
||||
coreViewModel: CoreViewModel,
|
||||
fileTransferViewModel: FileTransferViewModel,
|
||||
) {
|
||||
const isPresented = React.createProxyState(
|
||||
[fileTransferViewModel.presentedModal],
|
||||
() =>
|
||||
fileTransferViewModel.presentedModal.value ==
|
||||
FileTransferModals.ImportDecryptData,
|
||||
);
|
||||
|
||||
return (
|
||||
<div class="modal" toggle:open={isPresented}>
|
||||
<div>
|
||||
<main>
|
||||
<h2>
|
||||
{
|
||||
coreViewModel.translations.dataTransferModal
|
||||
.importHeadline
|
||||
}
|
||||
</h2>
|
||||
|
||||
<label class="tile">
|
||||
<span class="icon">key</span>
|
||||
<div>
|
||||
<span class="secondary">
|
||||
{
|
||||
coreViewModel.translations
|
||||
.dataTransferModal
|
||||
.exportKey
|
||||
}
|
||||
</span>
|
||||
<input
|
||||
bind:value={
|
||||
fileTransferViewModel.importKey
|
||||
}
|
||||
type="password"
|
||||
></input>
|
||||
</div>
|
||||
</label>
|
||||
|
||||
<p
|
||||
class="error"
|
||||
toggle:hidden={fileTransferViewModel.isImportKeyCorrect}>
|
||||
{coreViewModel.translations.dataTransferModal.incorrectPassphraseError}
|
||||
</p>
|
||||
</main>
|
||||
<div class="flex-row width-100">
|
||||
<button
|
||||
class="flex"
|
||||
on:click={
|
||||
fileTransferViewModel.showImportModal
|
||||
}
|
||||
>
|
||||
{coreViewModel.translations.general.backButton}
|
||||
</button>
|
||||
<button
|
||||
class="primary flex"
|
||||
on:click={fileTransferViewModel.decryptImport}
|
||||
toggle:disabled={
|
||||
fileTransferViewModel.cannotDecryptImport
|
||||
}
|
||||
>
|
||||
{coreViewModel.translations.dataTransferModal.decryptImportButton}
|
||||
<span class="icon">arrow_forward</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -114,8 +114,6 @@ const englishTranslations = {
|
||||
dataEntryInputDescription:
|
||||
"Enter the data displayed on the other device.",
|
||||
|
||||
notConnectedError: "You are not connected to any server.",
|
||||
|
||||
///
|
||||
|
||||
fromThisDeviceButton: "Send to other device",
|
||||
@@ -145,13 +143,16 @@ const englishTranslations = {
|
||||
filesSentCount: (count: number) => `Files sent: ${count}.`,
|
||||
allFilesSent: "Done.",
|
||||
|
||||
filesReceivedCount: (count: number) => `Files received: ${count}.`,
|
||||
filesReceivedCount: (count: number) => `Files processed: ${count}.`,
|
||||
|
||||
//
|
||||
|
||||
exportKey: "Encryption key",
|
||||
exportKeyConfirmation: "Confirm",
|
||||
downloadFileButton: "Download",
|
||||
importFileButton: "Import",
|
||||
decryptImportButton: "Decrypt",
|
||||
incorrectPassphraseError: "The encryption key is incorrect",
|
||||
},
|
||||
|
||||
storage: {
|
||||
|
||||
@@ -54,6 +54,11 @@ export default class FileTransferViewModel extends Context {
|
||||
exportKey: React.State<string> = new React.State("");
|
||||
exportKeyConfirmation: React.State<string> = new React.State("");
|
||||
|
||||
importedFile: React.State<File|undefined> = new React.State(undefined);
|
||||
importedFileString: React.State<string> = new React.State("");
|
||||
importKey: React.State<string> = new React.State("");
|
||||
isImportKeyCorrect: React.State<boolean> = new React.State(true);
|
||||
|
||||
// guards
|
||||
hasNoPathsSelected: React.State<boolean> = React.createProxyState(
|
||||
[this.selectedPaths],
|
||||
@@ -72,6 +77,16 @@ export default class FileTransferViewModel extends Context {
|
||||
() =>
|
||||
this.exportKey.value == "" || this.exportKey.value != this.exportKeyConfirmation.value
|
||||
);
|
||||
cannotImport: React.State<boolean> = React.createProxyState(
|
||||
[this.importedFile],
|
||||
() =>
|
||||
this.importedFile.value == undefined
|
||||
);
|
||||
cannotDecryptImport: React.State<boolean> = React.createProxyState(
|
||||
[this.importKey],
|
||||
() =>
|
||||
this.importKey.value == ""
|
||||
);
|
||||
|
||||
// handlers
|
||||
handleReceivedFile = (path: string): void => {
|
||||
@@ -130,92 +145,133 @@ export default class FileTransferViewModel extends Context {
|
||||
anchor.click();
|
||||
}
|
||||
|
||||
updateImportSelection = (): void => {
|
||||
const input = document.getElementById("file-transfer-input");
|
||||
if (!input) return;
|
||||
if (!(input instanceof HTMLInputElement)) return;
|
||||
|
||||
const file = input.files[0];
|
||||
this.importedFile.value = file;
|
||||
}
|
||||
|
||||
importFile = (): void => {
|
||||
const file = this.importedFile.value;
|
||||
if (!file) return;
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.readAsText(file, "utf8");
|
||||
reader.onload = () => {
|
||||
const result = reader.result;
|
||||
this.importedFileString.value = result.toString();
|
||||
this.showImportDecryptDataModal();
|
||||
}
|
||||
}
|
||||
|
||||
decryptImport = async (): Promise<void> => {
|
||||
const fileString = this.importedFileString.value;
|
||||
const key = this.importKey.value;
|
||||
if (!fileString || !key) return;
|
||||
|
||||
this.cannotDecryptImport.value = true;
|
||||
try {
|
||||
await this.coreViewModel.fileTransferModel.handleBackupFile(fileString, key);
|
||||
window.location.reload();
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
this.isImportKeyCorrect.value = false;
|
||||
this.cannotDecryptImport.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// view
|
||||
showDirectionSelectionModal = (): void => {
|
||||
this.coreViewModel.context = this;
|
||||
this.presentedModal.value = FileTransferModals.DirectionSelection;
|
||||
this.getOptions();
|
||||
this.coreViewModel.context = this;
|
||||
this.presentedModal.value = FileTransferModals.DirectionSelection;
|
||||
this.getOptions();
|
||||
};
|
||||
|
||||
showFileSelectionModal = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.FileSelection;
|
||||
this.presentedModal.value = FileTransferModals.FileSelection;
|
||||
};
|
||||
|
||||
showTransferDataModal = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.TransferDataDisplay;
|
||||
this.getTransferData();
|
||||
this.coreViewModel.fileTransferModel.prepareToSend();
|
||||
this.presentedModal.value = FileTransferModals.TransferDataDisplay;
|
||||
this.getTransferData();
|
||||
this.coreViewModel.fileTransferModel.prepareToSend();
|
||||
};
|
||||
|
||||
initiateTransfer = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.TransferDisplay;
|
||||
this.didNotFinishSending.value = true;
|
||||
this.filePathsSent.clear();
|
||||
this.presentedModal.value = FileTransferModals.TransferDisplay;
|
||||
this.didNotFinishSending.value = true;
|
||||
this.filePathsSent.clear();
|
||||
|
||||
this.coreViewModel.fileTransferModel.sendFiles(
|
||||
this.selectedPaths.value.values(),
|
||||
(path: string) => {
|
||||
console.log(path);
|
||||
this.filePathsSent.add(path);
|
||||
},
|
||||
);
|
||||
this.coreViewModel.fileTransferModel.sendFiles(
|
||||
this.selectedPaths.value.values(),
|
||||
(path: string) => {
|
||||
this.filePathsSent.add(path);
|
||||
},
|
||||
);
|
||||
|
||||
this.didNotFinishSending.value = false;
|
||||
this.didNotFinishSending.value = false;
|
||||
};
|
||||
|
||||
showTransferDataInputModal = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.TransferDataInput;
|
||||
this.presentedModal.value = FileTransferModals.TransferDataInput;
|
||||
};
|
||||
|
||||
showExportSelectionModal = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.ExportFileSelection;
|
||||
this.presentedModal.value = FileTransferModals.ExportFileSelection;
|
||||
};
|
||||
|
||||
showExportModal = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.Export;
|
||||
this.presentedModal.value = FileTransferModals.Export;
|
||||
};
|
||||
|
||||
showImportModal = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.Import;
|
||||
this.presentedModal.value = FileTransferModals.ImportSelection;
|
||||
};
|
||||
|
||||
showImportDecryptDataModal = (): void => {
|
||||
this.presentedModal.value = FileTransferModals.ImportDecryptData;
|
||||
};
|
||||
|
||||
prepareReceivingData = (): void => {
|
||||
if (this.cannotPrepareToReceive.value == true) return;
|
||||
this.presentedModal.value = FileTransferModals.ReceptionDisplay;
|
||||
this.filePathsReceived.clear();
|
||||
if (this.cannotPrepareToReceive.value == true) return;
|
||||
this.presentedModal.value = FileTransferModals.ReceptionDisplay;
|
||||
this.filePathsReceived.clear();
|
||||
|
||||
const transferData: TransferData = {
|
||||
channel: this.receivingTransferChannel.value,
|
||||
key: this.receivingTransferKey.value,
|
||||
};
|
||||
this.coreViewModel.fileTransferModel.prepareToReceive(transferData);
|
||||
const transferData: TransferData = {
|
||||
channel: this.receivingTransferChannel.value,
|
||||
key: this.receivingTransferKey.value,
|
||||
};
|
||||
this.coreViewModel.fileTransferModel.prepareToReceive(transferData);
|
||||
};
|
||||
|
||||
// exit
|
||||
close = (): void => {
|
||||
this.coreViewModel.closeContext(this.contextId);
|
||||
this.coreViewModel.closeContext(this.contextId);
|
||||
};
|
||||
|
||||
handleContextClose = (): void => {
|
||||
this.presentedModal.value = undefined;
|
||||
this.presentedModal.value = undefined;
|
||||
};
|
||||
|
||||
// init
|
||||
constructor(public readonly coreViewModel: CoreViewModel) {
|
||||
super("file-transfer");
|
||||
super("file-transfer");
|
||||
|
||||
this.coreViewModel.fileTransferModel.fileHandlerManager.setHandler(
|
||||
"file-transfer-view-model",
|
||||
this.handleReceivedFile,
|
||||
);
|
||||
this.coreViewModel.fileTransferModel.fileHandlerManager.setHandler(
|
||||
"file-transfer-view-model",
|
||||
this.handleReceivedFile,
|
||||
);
|
||||
|
||||
this.coreViewModel.fileTransferModel.readyToSendHandlerManager.setHandler(
|
||||
"file-transfer-view-model",
|
||||
() => this.initiateTransfer(),
|
||||
);
|
||||
this.coreViewModel.fileTransferModel.readyToSendHandlerManager.setHandler(
|
||||
"file-transfer-view-model",
|
||||
() => this.initiateTransfer(),
|
||||
);
|
||||
|
||||
// keystrokes
|
||||
this.registerKeyStroke(CommonKeys.CloseOrCancel, this.close);
|
||||
// keystrokes
|
||||
this.registerKeyStroke(CommonKeys.CloseOrCancel, this.close);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,17 +283,18 @@ export interface FileTransferOption {
|
||||
export enum FileTransferModals {
|
||||
DirectionSelection,
|
||||
|
||||
// sending
|
||||
FileSelection,
|
||||
TransferDataDisplay,
|
||||
TransferDisplay,
|
||||
// sending
|
||||
FileSelection,
|
||||
TransferDataDisplay,
|
||||
TransferDisplay,
|
||||
|
||||
// receiving
|
||||
TransferDataInput,
|
||||
ReceptionDisplay,
|
||||
// receiving
|
||||
TransferDataInput,
|
||||
ReceptionDisplay,
|
||||
|
||||
// export
|
||||
ExportFileSelection,
|
||||
Export,
|
||||
Import,
|
||||
// export
|
||||
ExportFileSelection,
|
||||
Export,
|
||||
ImportSelection,
|
||||
ImportDecryptData,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user