-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
185 lines (156 loc) · 5.29 KB
/
index.js
File metadata and controls
185 lines (156 loc) · 5.29 KB
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
const content = document.querySelector(".content");
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://file-expressapi.onrender.com/api/v1";
const uploadURL = `${baseURL}/files`;
const emailURL = `${baseURL}/send`;
const maxAllowedSize = 200 * 1024 * 1024; //200MB
browseBtn.addEventListener("click", () => {
fileInput.click();
});
dropZone.addEventListener("drop", (e) => {
e.preventDefault();
console.log("dropped", e.dataTransfer.files[0].name);
const files = e.dataTransfer.files;
if (files.length === 1) {
if (files[0].size < maxAllowedSize) {
fileInput.files = files;
uploadFile();
} else {
showToast("Max file size is 200MB");
}
} else if (files.length > 1) {
showToast("You can't upload multiple files");
}
dropZone.classList.remove("dragged");
});
dropZone.addEventListener("dragover", (e) => {
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
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();
});
const uploadFile = () => {
console.log("Uploading your file...");
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);
}
};
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";
progressContainer.style.display = "none"; // hide the box
const { file: url } = JSON.parse(res);
console.log(url);
sharingContainer.style.display = "block";
dropZone.style.display = "none";
content.innerHTML = `<h1>File uploaded successfully</h1>
<h3>Share this link with your buddy</h3>
<h3>or surprise them with mail</h3>`;
let ID = url.split("/").splice(-1, 1)[0];
fileURL.value = `${baseURL}/download/${ID}`;
console.log("uuid", ID, "url", 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 = {
uuid: url.split("/").splice(-1, 1)[0],
emailFrom: emailForm.elements["from-email"].value,
emailTo: emailForm.elements["to-email"].value,
name: emailForm.elements["name"].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.sucess) {
showToast(data.message);
sharingContainer.style.display = "none";
dropZone.style.display = "flex";
content.innerHTML = `<h1>Your files are safe with us!</h1>
<h3>Just upload file and link will be availabe to share</h3>
<h3>Do you know you can also share download link via email now</h3>`; // 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);
};