-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
190 lines (156 loc) · 5.52 KB
/
script.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
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
const dropZone = document.querySelector(".drop-zone");
const fileInput = document.querySelector("#fileInput");
const browseBtn = document.querySelector("#browseBtn");
const bgProgress = document.querySelector(".bg-progress");
const progressPercent = document.querySelector("#progressPercent");
const progressContainer = document.querySelector(".progress-container");
const progressBar = document.querySelector(".progress-bar");
const status = document.querySelector(".status");
const sharingContainer = document.querySelector(".sharing-container");
const copyURLBtn = document.querySelector("#copyURLBtn");
const fileURL = document.querySelector("#fileURL");
const emailForm = document.querySelector("#emailForm");
const toast = document.querySelector(".toast");
const baseURL = "https://innshare.herokuapp.com";
const uploadURL = `${baseURL}/api/files`;
const emailURL = `${baseURL}/api/files/send`;
const maxAllowedSize = 100 * 1024 * 1024; //100mb
browseBtn.addEventListener("click", () => {
fileInput.click();
});
// Event fired when item is dropped at targeted area
dropZone.addEventListener("drop", (e) => {
e.preventDefault();
// console.log("dropped", e.dataTransfer.files[0].name);
// Gives file taken as input
const files = e.dataTransfer.files;
//Files coming from drop event wil be transfered to fileInput
if (files.length === 1) {
// Insures if some file is dropped not only simply dragged and dropped
if (files[0].size < maxAllowedSize) {
fileInput.files = files;
uploadFile(); //function called
} else {
showToast("Max file size is 100MB");
}
} else if (files.length > 1) {
showToast("You can't upload multiple files");
}
dropZone.classList.remove("dragged");
});
// dragover is an event works when something is draggedover
dropZone.addEventListener("dragover", (e) => {
// Bydefault it was downloading it so it is prevented through it
e.preventDefault();
dropZone.classList.add("dragged");
// console.log("dropping file");
});
dropZone.addEventListener("dragleave", (e) => {
dropZone.classList.remove("dragged");
console.log("drag ended");
});
// file input change and uploader
//upload using browse
fileInput.addEventListener("change", () => {
if (fileInput.files[0].size > maxAllowedSize) {
showToast("Max file size is 100MB");
fileInput.value = ""; // reset the input
return;
}
uploadFile();
});
// sharing container listenrs
copyURLBtn.addEventListener("click", () => {
fileURL.select();
document.execCommand("copy");
showToast("Copied to clipboard");
});
fileURL.addEventListener("click", () => {
fileURL.select();
});
//uploading files
const uploadFile = () => {
console.log("file added uploading");
files = fileInput.files;
const formData = new FormData();
formData.append("myfile", files[0]);
//show the uploader
progressContainer.style.display = "block";
// upload file
const xhr = new XMLHttpRequest();
// listen for upload progress
xhr.upload.onprogress = function (event) {
// find the percentage of uploaded
let percent = Math.round((100 * event.loaded) / event.total);
progressPercent.innerText = percent;
const scaleX = `scaleX(${percent / 100})`;
bgProgress.style.transform = scaleX;
progressBar.style.transform = scaleX;
};
// handle error
xhr.upload.onerror = function () {
showToast(`Error in upload: ${xhr.status}.`);
fileInput.value = ""; // reset the input
};
// listen for response which will give the link
xhr.onreadystatechange = function () {
if (xhr.readyState == XMLHttpRequest.DONE) {
onFileUploadSuccess(xhr.responseText);
}
};
//the host link will be opened and then will be sent there
xhr.open("POST", uploadURL);
xhr.send(formData);
};
const onFileUploadSuccess = (res) => {
fileInput.value = ""; // reset the input
status.innerText = "Uploaded";
// remove the disabled attribute from form btn & make text send
emailForm[2].removeAttribute("disabled");
emailForm[2].innerText = "Send";
//once progree bar finishes uploading it gets hidden
progressContainer.style.display = "none"; // hide the box
//its a json object hence needs to be passed before calling
const { file: url } = JSON.parse(res);
console.log(url);
sharingContainer.style.display = "block";
fileURL.value = url;
};
emailForm.addEventListener("submit", (e) => {
e.preventDefault(); // stop submission
// disable the button
emailForm[2].setAttribute("disabled", "true");
emailForm[2].innerText = "Sending";
const url = fileURL.value;
const formData = {
//gives the unique id present at the end of the link generated
uuid: url.split("/").splice(-1, 1)[0],
emailTo: emailForm.elements["to-email"].value,
emailFrom: emailForm.elements["from-email"].value,
};
console.log(formData);
fetch(emailURL, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(formData),
})
.then((res) => res.json())
.then((data) => {
if (data.success) {
showToast("Email Sent");
sharingContainer.style.display = "none"; // hide the box
}
});
});
let toastTimer;
// the toast function
const showToast = (msg) => {
clearTimeout(toastTimer);
toast.innerText = msg;
toast.classList.add("show");
toastTimer = setTimeout(() => {
toast.classList.remove("show");
}, 2000);
};