-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
153 lines (129 loc) · 4.06 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
const dropZone = document.querySelector('.drop-zone');
const browseBtn = document.querySelector('.browseBtn');
const fileInput = document.querySelector('#fileInput');
const progressContainer = document.querySelector('.progess-container');
const bgProgress = document.querySelector('.bg-progress');
const progressBar = document.querySelector('.progress-bar');
const percentDiv = document.querySelector('#percent');
const sharingContainer = document.querySelector('.sharing-container');
const fileURLInput = document.querySelector('#fileURL');
const copyBtn = document.querySelector('#copyBtn');
const emailForm = document.querySelector('#emailForm');
const toast = document.querySelector('.toast');
const host = 'https://file-inshare.herokuapp.com/';
const uploadURL = `${host}api/files`;
const emailURL = `${host}api/files/send`;
const maxAllowedSize = 100 * 1024 * 1024;
dropZone.addEventListener('dragover', e => {
e.preventDefault();
if (!dropZone.classList.contains('dragged')) {
dropZone.classList.add('dragged');
}
});
dropZone.addEventListener('dragleave', e => {
dropZone.classList.remove('dragged');
});
dropZone.addEventListener('drop', e => {
e.preventDefault();
dropZone.classList.remove('dragged');
const files = e.dataTransfer.files;
if (files.length) {
// console.log(files);
fileInput.files = files;
uploadFile();
}
});
fileInput.addEventListener('change', () => {
uploadFile();
});
browseBtn.addEventListener('click', () => {
fileInput.click();
});
copyBtn.addEventListener('click', () => {
fileURLInput.select();
document.execCommand('copy');
showToast('Linked copied');
});
const uploadFile = () => {
if (fileInput.files.length > 1) {
resetFileInput();
showToast('Can upload one at a time!! try to upload zip of all file');
return;
}
const file = fileInput.files[0];
if (file.size > maxAllowedSize) {
showToast('Max allowed size is 100mb');
resetFileInput();
return;
}
progressContainer.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);
onUploadSuccess(JSON.parse(xhr.response));
}
};
xhr.upload.onprogress = updateProgress;
xhr.upload.onerror = () => {
resetFileInput();
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);
bgProgress.style.width = `${percent}%`;
percentDiv.innerText = percent;
progressBar.style.transform = `scaleX(${percent / 100})`;
};
const resetFileInput = () => {
fileInput.value = '';
};
const onUploadSuccess = ({ file: url }) => {
// console.log(url);
resetFileInput();
emailForm[2].removeAttribute('disabled');
progressContainer.style.display = 'none';
sharingContainer.style.display = 'block';
fileURLInput.value = url;
// console.log(`fileURLInput = ${fileURLInput}`);
};
emailForm.addEventListener('submit', e => {
e.preventDefault();
const url = fileURLInput.value;
const formData = {
uuid: url.split('/').splice(-1, 1)[0],
emailTo: emailForm.elements['to-email'].value,
emailFrom: emailForm.elements['from-email'].value,
};
emailForm[2].setAttribute('disabled', 'true');
// console.log(formData);
fetch(emailURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(formData),
})
.then(res => res.json())
.then(({ success }) => {
if (success) {
// This might be uncomment or form data(email to specifically) should be empty and can show success msg
sharingContainer.style.display = 'none';
showToast('Email sent');
}
});
});
let toastTimer;
const showToast = msg => {
toast.innerText = msg;
toast.style.transform = 'translate(-50%, 0)';
clearTimeout(toastTimer);
toastTimer = setTimeout(() => {
toast.style.transform = 'translate(-50%, 70px)';
}, 2000);
};