File size: 11,460 Bytes
e1a2a92 |
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 |
// Api Fuctions
async function postJson(url, data) {
data['password'] = getPassword()
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
return await response.json()
}
document.getElementById('pass-login').addEventListener('click', async () => {
const password = document.getElementById('auth-pass').value
const data = { 'pass': password }
const json = await postJson('/api/checkPassword', data)
if (json.status === 'ok') {
localStorage.setItem('password', password)
alert('Logged In Successfully')
window.location.reload()
}
else {
alert('Wrong Password')
}
})
async function getCurrentDirectory() {
let path = getCurrentPath()
if (path === 'redirect') {
return
}
try {
const auth = getFolderAuthFromPath()
console.log(path)
const data = { 'path': path, 'auth': auth }
const json = await postJson('/api/getDirectory', data)
if (json.status === 'ok') {
if (getCurrentPath().startsWith('/share')) {
const sections = document.querySelector('.sidebar-menu').getElementsByTagName('a')
console.log(path)
if (removeSlash(json['auth_home_path']) === removeSlash(path.split('_')[1])) {
sections[0].setAttribute('class', 'selected-item')
} else {
sections[0].setAttribute('class', 'unselected-item')
}
sections[0].href = `/?path=/share_${removeSlash(json['auth_home_path'])}&auth=${auth}`
console.log(`/?path=/share_${removeSlash(json['auth_home_path'])}&auth=${auth}`)
}
console.log(json)
showDirectory(json['data'])
} else {
alert('404 Current Directory Not Found')
}
}
catch (err) {
console.log(err)
alert('404 Current Directory Not Found')
}
}
async function createNewFolder() {
const folderName = document.getElementById('new-folder-name').value;
const path = getCurrentPath()
if (path === 'redirect') {
return
}
if (folderName.length > 0) {
const data = {
'name': folderName,
'path': path
}
try {
const json = await postJson('/api/createNewFolder', data)
if (json.status === 'ok') {
window.location.reload();
} else {
alert(json.status)
}
}
catch (err) {
alert('Error Creating Folder')
}
} else {
alert('Folder Name Cannot Be Empty')
}
}
async function getFolderShareAuth(path) {
const data = { 'path': path }
const json = await postJson('/api/getFolderShareAuth', data)
if (json.status === 'ok') {
return json.auth
} else {
alert('Error Getting Folder Share Auth')
}
}
// File Uploader Start
const MAX_FILE_SIZE = MAX_FILE_SIZE__SDGJDG // Will be replaced by the python
const fileInput = document.getElementById('fileInput');
const progressBar = document.getElementById('progress-bar');
const cancelButton = document.getElementById('cancel-file-upload');
const uploadPercent = document.getElementById('upload-percent');
let uploadRequest = null;
let uploadStep = 0;
let uploadID = null;
fileInput.addEventListener('change', async (e) => {
const file = fileInput.files[0];
if (file.size > MAX_FILE_SIZE) {
alert(`File size exceeds ${(MAX_FILE_SIZE / (1024 * 1024 * 1024)).toFixed(2)} GB limit`);
return;
}
// Showing file uploader
document.getElementById('bg-blur').style.zIndex = '2';
document.getElementById('bg-blur').style.opacity = '0.1';
document.getElementById('file-uploader').style.zIndex = '3';
document.getElementById('file-uploader').style.opacity = '1';
document.getElementById('upload-filename').innerText = 'Filename: ' + file.name;
document.getElementById('upload-filesize').innerText = 'Filesize: ' + (file.size / (1024 * 1024)).toFixed(2) + ' MB';
document.getElementById('upload-status').innerText = 'Status: Uploading To Backend Server';
const formData = new FormData();
formData.append('file', file);
formData.append('path', getCurrentPath());
formData.append('password', getPassword());
const id = getRandomId();
formData.append('id', id);
formData.append('total_size', file.size);
uploadStep = 1;
uploadRequest = new XMLHttpRequest();
uploadRequest.open('POST', '/api/upload', true);
uploadRequest.upload.addEventListener('progress', (e) => {
if (e.lengthComputable) {
const percentComplete = (e.loaded / e.total) * 100;
progressBar.style.width = percentComplete + '%';
uploadPercent.innerText = 'Progress : ' + percentComplete.toFixed(2) + '%';
}
});
uploadRequest.upload.addEventListener('load', async () => {
await updateSaveProgress(id)
});
uploadRequest.upload.addEventListener('error', () => {
alert('Upload failed');
window.location.reload();
});
uploadRequest.send(formData);
});
cancelButton.addEventListener('click', () => {
if (uploadStep === 1) {
uploadRequest.abort();
} else if (uploadStep === 2) {
const data = { 'id': uploadID }
postJson('/api/cancelUpload', data)
}
alert('Upload canceled');
window.location.reload();
});
async function updateSaveProgress(id) {
console.log('save progress')
progressBar.style.width = '0%';
uploadPercent.innerText = 'Progress : 0%'
document.getElementById('upload-status').innerText = 'Status: Processing File On Backend Server';
const interval = setInterval(async () => {
const response = await postJson('/api/getSaveProgress', { 'id': id })
const data = response['data']
if (data[0] === 'running') {
const current = data[1];
const total = data[2];
document.getElementById('upload-filesize').innerText = 'Filesize: ' + (total / (1024 * 1024)).toFixed(2) + ' MB';
const percentComplete = (current / total) * 100;
progressBar.style.width = percentComplete + '%';
uploadPercent.innerText = 'Progress : ' + percentComplete.toFixed(2) + '%';
}
else if (data[0] === 'completed') {
clearInterval(interval);
uploadPercent.innerText = 'Progress : 100%'
progressBar.style.width = '100%';
await handleUpload2(id)
}
}, 3000)
}
async function handleUpload2(id) {
console.log(id)
document.getElementById('upload-status').innerText = 'Status: Uploading To Telegram Server';
progressBar.style.width = '0%';
uploadPercent.innerText = 'Progress : 0%';
const interval = setInterval(async () => {
const response = await postJson('/api/getUploadProgress', { 'id': id })
const data = response['data']
if (data[0] === 'running') {
const current = data[1];
const total = data[2];
document.getElementById('upload-filesize').innerText = 'Filesize: ' + (total / (1024 * 1024)).toFixed(2) + ' MB';
let percentComplete
if (total === 0) {
percentComplete = 0
}
else {
percentComplete = (current / total) * 100;
}
progressBar.style.width = percentComplete + '%';
uploadPercent.innerText = 'Progress : ' + percentComplete.toFixed(2) + '%';
}
else if (data[0] === 'completed') {
clearInterval(interval);
alert('Upload Completed')
window.location.reload();
}
}, 3000)
}
// File Uploader End
// URL Uploader Start
async function get_file_info_from_url(url) {
const data = { 'url': url }
const json = await postJson('/api/getFileInfoFromUrl', data)
if (json.status === 'ok') {
return json.data
} else {
throw new Error(`Error Getting File Info : ${json.status}`)
}
}
async function start_file_download_from_url(url, filename, singleThreaded) {
const data = { 'url': url, 'path': getCurrentPath(), 'filename': filename, 'singleThreaded': singleThreaded }
const json = await postJson('/api/startFileDownloadFromUrl', data)
if (json.status === 'ok') {
return json.id
} else {
throw new Error(`Error Starting File Download : ${json.status}`)
}
}
async function download_progress_updater(id, file_name, file_size) {
uploadID = id;
uploadStep = 2
// Showing file uploader
document.getElementById('bg-blur').style.zIndex = '2';
document.getElementById('bg-blur').style.opacity = '0.1';
document.getElementById('file-uploader').style.zIndex = '3';
document.getElementById('file-uploader').style.opacity = '1';
document.getElementById('upload-filename').innerText = 'Filename: ' + file_name;
document.getElementById('upload-filesize').innerText = 'Filesize: ' + (file_size / (1024 * 1024)).toFixed(2) + ' MB';
const interval = setInterval(async () => {
const response = await postJson('/api/getFileDownloadProgress', { 'id': id })
const data = response['data']
if (data[0] === 'error') {
clearInterval(interval);
alert('Failed To Download File From URL To Backend Server')
window.location.reload()
}
else if (data[0] === 'completed') {
clearInterval(interval);
uploadPercent.innerText = 'Progress : 100%'
progressBar.style.width = '100%';
await handleUpload2(id)
}
else {
const current = data[1];
const total = data[2];
const percentComplete = (current / total) * 100;
progressBar.style.width = percentComplete + '%';
uploadPercent.innerText = 'Progress : ' + percentComplete.toFixed(2) + '%';
if (data[0] === 'Downloading') {
document.getElementById('upload-status').innerText = 'Status: Downloading File From Url To Backend Server';
}
else {
document.getElementById('upload-status').innerText = `Status: ${data[0]}`;
}
}
}, 3000)
}
async function Start_URL_Upload() {
try {
document.getElementById('new-url-upload').style.opacity = '0';
setTimeout(() => {
document.getElementById('new-url-upload').style.zIndex = '-1';
}, 300)
const file_url = document.getElementById('remote-url').value
const singleThreaded = document.getElementById('single-threaded-toggle').checked
const file_info = await get_file_info_from_url(file_url)
const file_name = file_info.file_name
const file_size = file_info.file_size
if (file_size > MAX_FILE_SIZE) {
throw new Error(`File size exceeds ${(MAX_FILE_SIZE / (1024 * 1024 * 1024)).toFixed(2)} GB limit`)
}
const id = await start_file_download_from_url(file_url, file_name, singleThreaded)
await download_progress_updater(id, file_name, file_size)
}
catch (err) {
alert(err)
window.location.reload()
}
}
// URL Uploader End |