-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdecode.js
57 lines (51 loc) · 1.68 KB
/
decode.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
import fs from 'fs/promises';
import { getQRCodeData } from './getqrcodedata.js';
import CryptoJS from 'crypto-js';
import { exec } from 'child_process';
import path from 'path';
export const decode = async (i, pw, o, silent, test_mode = false, debug = false) => {
try {
const qrCodeData = await getQRCodeData(i);
if (!qrCodeData) {
throw new Error('No QR code found in the image');
}
const ed = qrCodeData;
const decrypted = pw ? CryptoJS.AES.decrypt(ed, pw).toString(CryptoJS.enc.Utf8) : ed;
let decodedData;
try {
decodedData = JSON.parse(decrypted);
} catch (error) {
console.log("Couldn't decode the data as JSON, displaying as a string:");
console.log(decrypted);
return decrypted;
}
if (!silent || !test_mode) {
let outputFile = decodedData.filename || o;
try {
await fs.stat(outputFile);
const ext = path.extname(outputFile);
const base = path.basename(outputFile, ext);
const dir = path.dirname(outputFile);
outputFile = path.join(dir, `${base}-${Date.now()}${ext}`);
} catch (error) {
// File does not exist, no need to change outputFile
}
console.log(`Decoded data: ${decodedData.data}`);
await fs.writeFile(outputFile, decodedData.data);
console.log(`Decoded data saved as ${outputFile}`);
exec(`open ${outputFile}`);
} else {
if (!test_mode) {
console.log(`Decoded data: ${decodedData.data}`);
}
}
return decodedData;
} catch (e) {
if (debug) {
console.error('Error decoding data:', e.message);
console.log(e);
} else {
console.error('Error decoding data');
}
}
};