-
Notifications
You must be signed in to change notification settings - Fork 14
/
App.vue
381 lines (364 loc) · 10.4 KB
/
App.vue
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<template>
<div id="app" @dragover.prevent @drop.prevent>
<h1 class="title">
rage
<span v-if="!decrypting">encrypt</span>
<span v-if="decrypting">decrypt</span>
all the things!
</h1>
<div class="columns is-centered">
<DropZone
class="column"
v-if="!decrypting"
v-bind:dropFiles="dropFiles"
v-bind:encrypting="encrypting"
v-on:files-changed="handleFiles"
v-on:file-removed="removeFileToEncrypt"
/>
<div class="column is-half" v-if="encrypting || decrypting">
<FileInfo
v-bind:fileIcon="fileIcon"
v-bind:iconSize="fileIconSize"
v-on:reset-app="reset"
>
<div v-if="encrypting">TODO: Encryption info.</div>
<div v-if="decrypting">
<dl>
<dt>Name</dt>
<dd>{{ this.decryptFile.name }}</dd>
<dt>Encrypted size</dt>
<dd>{{ this.decryptFile.size }} bytes</dd>
</dl>
</div>
</FileInfo>
<EncryptPane
id="details-pane"
v-if="encrypting"
v-bind:wasm="wasm"
v-on:encrypt-to-recipients="encryptToRecipients"
v-on:encrypt-with-passphrase="encryptWithPassphrase"
/>
<DecryptPane
id="details-pane"
v-if="decrypting"
v-bind:wasm="wasm"
v-bind:fileDecrypted="fileDecrypted"
v-bind:needIdentities="needIdentities"
v-bind:needPassphrase="needPassphrase"
v-on:decrypt-with-identities="decryptWithIdentities"
v-on:decrypt-with-passphrase="decryptWithPassphrase"
v-on:download-file="downloadDecryptedFile"
/>
</div>
</div>
<footer class="footer">
<p>
This is a
<strong>BETA</strong> version; make sure you have backups of the files
you are using!
</p>
<p>
All operations happen locally inside your browser. No data is uploaded
to any server.
</p>
<p>
<a href="https://str4d.xyz/wage">Source available here!</a> Powered by
<a href="https://str4d.xyz/rage">rage</a>.
</p>
<p>
Found a bug or usability issue?
<a href="https://str4d.xyz/wage/report">Report it here!</a>
</p>
</footer>
</div>
</template>
<script>
import DecryptPane from "./components/DecryptPane.vue";
import DropZone from "./components/DropZone.vue";
import EncryptPane from "./components/EncryptPane.vue";
import FileInfo from "./components/FileInfo.vue";
import {
getClassNameForFilename,
getClassNameForMimeType,
} from "font-awesome-filetypes";
export default {
name: "App",
components: {
DecryptPane,
DropZone,
EncryptPane,
FileInfo,
},
data() {
return {
wasm: null,
dropFiles: [],
encryptMode: false,
decryptFile: null,
decryptor: null,
decryptedStream: null,
downloadStream: null,
};
},
beforeCreate() {
// WASM needs to be imported asynchronously.
import("wage").then((wasm) => {
this.wasm = wasm;
});
},
created() {
window.addEventListener("beforeunload", this.checkDownloads);
window.addEventListener("unload", this.cancelDownloads);
},
computed: {
// Are we in "encrypting" mode?
encrypting() {
return this.encryptMode && !this.decrypting;
},
// Are we in "decrypting" mode?
decrypting() {
return this.decryptFile !== null;
},
// Icon matching the file we are encrypting or decrypting.
fileIcon() {
if (this.encrypting) {
return (this.dropFiles.length > 1
? this.getClassNameForFilename("archive.zip")
: this.getClassNameForMimeType(this.dropFiles[0].type)
).substring(3);
} else if (this.decrypting) {
// Default filename is the age-encrypted filename without the .age suffix.
return this.getClassNameForFilename(
this.decryptFile.name.slice(0, -4)
).substring(3);
} else {
return "file";
}
},
fileIconSize() {
if (this.encrypting) {
return "4x";
} else {
return "10x";
}
},
// What do we need from the user?
needIdentities() {
return (
this.decryptor !== null &&
this.decryptor.requires() == this.wasm.DecryptorType.Recipients
);
},
needPassphrase() {
return (
this.decryptor !== null &&
this.decryptor.requires() == this.wasm.DecryptorType.Passphrase
);
},
// Have we successfully decrypted the file?
fileDecrypted() {
return this.decryptedStream !== null;
},
fileDownloading() {
return this.downloadStream !== null;
},
},
methods: {
getClassNameForFilename,
getClassNameForMimeType,
// Reset application to initial state.
reset() {
this.dropFiles = [];
this.encryptMode = false;
this.decryptFile = null;
this.decryptor = null;
this.decryptedStream = null;
this.downloadStream = null;
},
// Status messages.
showError(e) {
console.error(e);
this.$buefy.toast.open({
duration: 5000,
message: e,
position: "is-bottom",
type: "is-danger",
});
},
// This function is called by the drop zone, so only if we are starting out,
// or are already encrypting.
handleFiles() {
if (!this.encrypting) {
// Search for a decryptable file.
var decryptIndex = [...this.dropFiles].findIndex((f) => {
return f.name.endsWith(".age");
});
// Decide whether we are encrypting or decrypting.
if (decryptIndex == -1) {
this.encryptMode = true;
} else {
this.startDecrypt(this.dropFiles[decryptIndex]);
}
}
},
// Encryption methods
removeFileToEncrypt() {
if (!this.dropFiles.length) {
this.reset();
}
},
prepareEncryptStream(callback) {
if (this.dropFiles.length > 1) {
// TODO: Archive and encrypt.
this.reset();
this.showError("Encrypting multiple files is not yet supported");
} else {
// Default filename for a single file is the filename with an .age suffix.
const fileName = this.dropFiles[0].name + ".age";
callback(window.streamSaver.createWriteStream(fileName));
}
},
encryptToRecipients(recipients) {
this.prepareEncryptStream((outputStream) => {
recipients
.into_encryptor()
.wrap_output(outputStream)
.then((sink) => {
this.downloadStream = sink;
this.encryptSingleFile();
});
});
},
encryptWithPassphrase(passphrase) {
this.prepareEncryptStream((outputStream) => {
this.wasm.Encryptor.with_user_passphrase(passphrase)
.wrap_output(outputStream)
.then((sink) => {
this.downloadStream = sink;
this.encryptSingleFile();
});
});
},
encryptSingleFile() {
let fileStream = this.dropFiles[0].stream();
// Use the more optimized ReadableStream.pipeTo if available.
if (window.WritableStream && fileStream.pipeTo) {
return fileStream.pipeTo(this.downloadStream).then(this.reset);
}
const reader = fileStream.getReader();
const writer = this.downloadStream.getWriter();
const pump = () =>
reader
.read()
.then((res) =>
res.done
? writer.close().then(this.reset)
: writer.write(res.value).then(pump)
);
pump();
},
// Decryption methods
startDecrypt(file) {
this.decryptFile = file;
this.wasm.Decryptor.new(file).then((decryptor) => {
this.decryptor = decryptor;
});
},
decryptWithIdentities(identities) {
let decryptor = this.decryptor;
this.decryptor = null;
// TODO:
// - Handle if decryptor === null
// - Disable Decrypt button while decrypting, re-enable on error
decryptor.decrypt_with_identities(identities).then(
(stream) => {
this.decryptedStream = stream;
},
(e) => {
this.reset();
this.showError(e);
}
);
},
decryptWithPassphrase(passphrase) {
let decryptor = this.decryptor;
this.decryptor = null;
// TODO:
// - Handle if decryptor === null
// - Disable Decrypt button while decrypting, re-enable on error
decryptor.decrypt_with_passphrase(passphrase).then(
(stream) => {
this.decryptedStream = stream;
},
(e) => {
this.reset();
this.showError(e);
}
);
},
downloadDecryptedFile() {
// Default filename is the age-encrypted filename without the .age suffix.
const fileName = this.decryptFile.name.slice(0, -4);
this.downloadStream = window.streamSaver.createWriteStream(fileName);
// Use the more optimized ReadableStream.pipeTo if available.
if (window.WritableStream && this.decryptedStream.pipeTo) {
return this.decryptedStream
.pipeTo(this.downloadStream)
.then(this.reset);
}
const reader = this.decryptedStream.getReader();
const writer = this.downloadStream.getWriter();
const pump = () =>
reader
.read()
.then((res) =>
res.done
? writer.close().then(this.reset)
: writer.write(res.value).then(pump)
);
pump();
},
// File downloads happen in the browser, so navigating away from the page
// will break any in-progress downloads.
checkDownloads(evt) {
if (this.fileDownloading) {
evt.returnValue =
"A file is still downloading; leaving will break the download. Are you sure you want to leave?";
}
},
cancelDownloads() {
if (this.fileDownloading) {
this.downloadStream.abort();
this.downloadStream = null;
}
},
},
};
</script>
<style>
#app {
display: grid;
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
margin-left: 10%;
margin-right: 10%;
}
.button {
display: inline-block;
padding: 10px;
background: #ccc;
cursor: pointer;
border-radius: 5px;
border: 1px solid #ccc;
}
.button:hover {
background: #ddd;
}
dt {
font-weight: bold;
}
</style>