This repository has been archived by the owner on Feb 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
upload.worker.js
96 lines (84 loc) · 2.44 KB
/
upload.worker.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
84
85
86
87
88
89
90
91
92
93
94
95
debugger;
import uuid4 from "uuid";
import AWS from "aws-sdk";
addEventListener('message', async ({ data }) => {
postMessage(await getFileInfo(data))
})
const hexString = buffer => {
const byteArray = new Uint8Array(buffer);
const hexCodes = [...byteArray].map(value => {
const hexCode = value.toString(16);
const paddedHexCode = hexCode.padStart(2, "0");
return paddedHexCode;
});
return hexCodes.join("");
};
const generateHash = file => {
const crypto = self.crypto.subtle;
const fileReader = new FileReader();
fileReader.readAsArrayBuffer(file);
return new Promise(resolve => {
fileReader.onload = async function () {
const fileReaderResult = fileReader.result;
if (fileReaderResult instanceof ArrayBuffer) {
const buffer = await crypto.digest("SHA-256", fileReaderResult);
resolve(hexString(buffer));
}
};
});
};
const getFileInfo = async data => {
const { file, id, token } = data
const fileId = uuid4()
const checksum = await generateHash(file)
upload(file, fileId, id, token)
return {
id: fileId,
checksum,
size: file.size.toString(),
path: file.webkitRelativePath,
lastModifiedDate: file.lastModified.toString(),
fileName: file.name
}
}
const getCredentials = async token => {
return new Promise((resolve, reject) => {
const IdentityPoolId = "eu-west-2:4b26364a-3070-4f98-8e86-1e33a1b54d85";
const cognitoLoginId =
"cognito-idp.eu-west-2.amazonaws.com/eu-west-2_6Mn0M2i9C";
AWS.config.region = "eu-west-2";
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId,
Logins: {
[cognitoLoginId]: token
}
});
AWS.config.getCredentials(function (err) {
if (err === undefined || err === null) {
resolve();
} else {
reject(err.code);
}
});
});
};
const upload = async (file, fileId, id, token) => {
var Bucket = "tdr-files";
await getCredentials(token);
var s3 = new AWS.S3({
params: {
Bucket
}
});
s3.upload(
{
Key: `${id}/${fileId}`,
Body: file,
Bucket
},
{},
function (err) {
console.log(err);
}
);
};