-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
175 lines (144 loc) · 4.68 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
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
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://insta-share-com.onrender.com";
const uploadURL = `${baseURL}/api/files`;
const emailURL = `${baseURL}/api/files/send`;
const maxAllowedSize = 100 * 1024 * 1024; //100mb
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 100MB");
}
} 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("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);
}
};
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";
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 = {
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);
};