forked from hackclub/sprig
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.js
70 lines (54 loc) · 1.53 KB
/
s3.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
import md5 from "md5";
async function importS3(id) {
const url = `https://project-bucket-hackclub.s3.eu-west-1.amazonaws.com/${id}.json`;
const saved = await fetch(url, { mode: "cors" }).then((r) => r.json());
return saved;
}
export async function exportS3(text) {
const uniqueID = md5(JSON.stringify(text));
const { exists, uploadURL, jsonFilename, id } = await fetch(
`https://vt4x133ukg.execute-api.eu-west-1.amazonaws.com/default/getPresignedURL?id=${uniqueID}`
).then((r) => r.json());
if (!exists) {
await fetch(uploadURL, {
mode: "cors",
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ text }),
});
}
const link = getURLPath(`?id=${id}`);
copy(link);
return link;
}
function getURLPath(extension) {
return (
window.location.protocol +
"//" +
window.location.host +
window.location.pathname +
extension
);
}
function isSafari() {
var is_safari = navigator.userAgent.toLowerCase().indexOf('safari/') > -1;
return is_safari;
}
// Copy a string into the user's clipboard
export default function copy(str) {
// const inp = document.createElement("input");
// document.body.appendChild(inp);
// inp.value = str;
// inp.select();
// document.execCommand("copy", false);
// inp.remove();
const button = document.createElement("button");
document.body.appendChild(button);
button.addEventListener("click", () => {
navigator.clipboard.writeText(str);
})
button.click();
button.remove();
}