-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
115 lines (100 loc) · 3.19 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const drop = document.querySelector(".drop");
const fileInput = document.querySelector(".fileInput")
const btn = document.querySelector(".btn")
const progressBar = document.querySelector(".progressBar")
const progress = document.querySelector(".progress")
const percentId = document.querySelector("#percent")
const fileURL = document.querySelector("#fileURL")
const sharing = document.querySelector(".sharing")
const copyBtn = document.querySelector("#copyBtn")
const qrcode = document.querySelector("#qr-code")
const qrContainer = document.querySelector(".qr-container")
const toast = document.querySelector(".toast")
const qrURL = "https://api.qrserver.com/v1/create-qr-code/?size=100x100&bgcolor=232533&color=fafafa&data=";
const host = "https://qshare-server.herokuapp.com/"
const uploadURL = `${host}api/files`
const maxSize = 10 * 1024 * 1024 ;
drop.addEventListener("dragover", (e) => {
e.preventDefault()
if (!drop.classList.contains("dragged")){
drop.classList.add("dragged");
}
});
drop.addEventListener("dragleave", () => {
drop.classList.remove("dragged");
});
drop.addEventListener("drop", (e) => {
e.preventDefault()
drop.classList.remove("dragged");
const files = e.dataTransfer.files;
//console.table(files);
if(files.length){
fileInput.files = files;
uploadFile();
}
});
fileInput.addEventListener("change", ()=>{
uploadFile();
})
btn.addEventListener("click", ()=>{
fileInput.click()
})
const uploadFile = () => {
if (fileInput.files.length > 1){
fileInput.value = "";
showToast("upload only 1 file.");
return;
}
const file = fileInput.files[0];
if (file.size > maxSize){
fileInput.value = "";
showToast("Can;t upload more than 10mb")
return ;
}
progressBar.style.display = "block";
const formData = new FormData();
formData.append("Myfile",file)
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = () =>{
if(xhr.readyState === XMLHttpRequest.DONE){
//console.log(xhr.response);
showLink(JSON.parse(xhr.response));
}
}
xhr.upload.onprogress = Updateprogress;
xhr.upload.onerror = () =>{
fileInput.value = "";
showToast(`Error in upload: ${xhr.statusText}`);
}
xhr.open("POST", uploadURL);
xhr.send(formData);
};
const Updateprogress = (e) => {
const percent = Math.round((e.loaded / e.total) * 100);
//console.log(percent);
progress.style.width = `${percent}%`;
percentId.innerText = percent;
}
const showLink = ({file: url}) => {
console.log(url);
progressBar.style.display = "none";
sharing.style.display = "block"
qrContainer.style.display = "block"
fileURL.value = url;
qrcode.setAttribute("src", qrURL + url);
showToast("file uploaded!");
}
copyBtn.addEventListener("click", () =>{
fileURL.select();
document.execCommand("copy");
showToast("Copied to clipboard!")
})
let toastTimer;
const showToast = (msg) => {
toast.innerText = msg;
toast.style.transform = "translate(-50%, 0)";
clearTimeout(toastTimer);
toastTimer = setTimeout( () => {
toast.style.transform = "translate(-50%, 60px)";
}, 2000)
}