-
Notifications
You must be signed in to change notification settings - Fork 1
/
consumer.js
83 lines (71 loc) · 2.53 KB
/
consumer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { asyncRun } from "./py-worker.js";
const script = await fetch('./main.py').then(response => response.text());
const pubKey = document.querySelector('#pubkey').textContent
let dirHandle;
let inputFileHandle;
let outputFileHandle;
async function main() {
let context = {
dirHandle: dirHandle,
inputFileName: inputFileHandle.name,
outputFileName: outputFileHandle.name,
pubKey: pubKey
};
try {
const { results, error } = await asyncRun(script, context);
if (results) {
console.log("pyodideWorker return results: ", results);
alert("Encryption finished successfully!");
} else if (error) {
console.log("pyodideWorker error: ", error);
}
} catch (e) {
console.log(
`Error in pyodideWorker at ${e.filename}, Line: ${e.lineno}, ${e.message}`,
);
}
}
async function mountLocalDirectory() {
// use the same ID crypt4gh to open pickers in the same directory
dirHandle = await showDirectoryPicker({id: "crypt4gh"});
if ((await dirHandle.queryPermission({ mode: "readwrite" })) !== "granted") {
if (
(await dirHandle.requestPermission({ mode: "readwrite" })) !== "granted"
) {
throw Error("Unable to read and write directory");
}
}
}
async function getNewFileHandle() {
let outputFileName = `${inputFileHandle.name}.crypt4gh`;
const options = {
id: "crypt4gh",
suggestedName: outputFileName,
types: [
{
description: 'crypt4gh file',
accept: {
'application/octet-stream': ['.crypt4gh'],
},
},
],
};
return window.showSaveFilePicker(options);
}
document.querySelector('#mount').addEventListener('click', async () => {
if (!('showDirectoryPicker' in window)) {
alert('Your browser does not support the File System Access API. Please use a supported browser.');
return; // Stop execution if the API is not supported
}
document.querySelector('#select').disabled = false;
await mountLocalDirectory();
});
document.querySelector('#select').addEventListener('click', async () => {
// Destructure the one-element array.
[inputFileHandle] = await window.showOpenFilePicker({id: "crypt4gh"});
document.querySelector('#encrypt').disabled = false;
});
document.querySelector('#encrypt').addEventListener('click', async () => {
outputFileHandle = await getNewFileHandle();
await main();
});