Spaces:
Sleeping
Sleeping
| // 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 } | |
| showLoading('Logging in...'); | |
| const json = await postJson('/api/checkPassword', data) | |
| hideLoading(); | |
| if (json.status === 'ok') { | |
| localStorage.setItem('password', password) | |
| showToast('π Login successful!', 'success'); | |
| setTimeout(() => window.location.reload(), 1000); | |
| } | |
| else { | |
| showToast('β Wrong password', 'error'); | |
| } | |
| }) | |
| async function getCurrentDirectory() { | |
| let path = getCurrentPath() | |
| if (path === 'redirect') { | |
| return | |
| } | |
| try { | |
| showLoading('Loading directory...'); | |
| const auth = getFolderAuthFromPath() | |
| console.log(path) | |
| const data = { 'path': path, 'auth': auth } | |
| const json = await postJson('/api/getDirectory', data) | |
| hideLoading(); | |
| 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 { | |
| showToast('β Directory not found', 'error'); | |
| } | |
| } | |
| catch (err) { | |
| console.log(err) | |
| hideLoading(); | |
| showToast('β Directory not found', 'error'); | |
| } | |
| } | |
| 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 { | |
| showLoading('Creating folder...'); | |
| const json = await postJson('/api/createNewFolder', data) | |
| hideLoading(); | |
| if (json.status === 'ok') { | |
| showToast('π Folder created successfully!', 'success'); | |
| setTimeout(() => window.location.reload(), 1000); | |
| } else { | |
| showToast(json.status, 'error'); | |
| } | |
| } | |
| catch (err) { | |
| hideLoading(); | |
| showToast('β Error creating folder', 'error'); | |
| } | |
| } else { | |
| showToast('β Folder name cannot be empty', 'error'); | |
| } | |
| } | |
| async function getFolderShareAuth(path) { | |
| const data = { 'path': path } | |
| const json = await postJson('/api/getFolderShareAuth', data) | |
| if (json.status === 'ok') { | |
| return json.auth | |
| } else { | |
| showToast('β Error getting folder share link', 'error'); | |
| } | |
| } | |
| async function tmdbSearchMovie(title, year) { | |
| const data = { 'title': title, 'year': year } | |
| const json = await postJson('/api/tmdbSearchMovie', data) | |
| return json | |
| } | |
| async function requestMovie(title, year) { | |
| const data = { 'title': title, 'year': year } | |
| const json = await postJson('/api/requestMovie', data) | |
| return json | |
| } | |
| // 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) { | |
| showToast(`β File size exceeds ${(MAX_FILE_SIZE / (1024 * 1024 * 1024)).toFixed(2)} GB limit`, 'error'); | |
| 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', () => { | |
| showToast('β Upload failed', 'error'); | |
| setTimeout(() => window.location.reload(), 1000); | |
| }); | |
| uploadRequest.send(formData); | |
| }); | |
| cancelButton.addEventListener('click', () => { | |
| if (uploadStep === 1) { | |
| uploadRequest.abort(); | |
| } else if (uploadStep === 2) { | |
| const data = { 'id': uploadID } | |
| postJson('/api/cancelUpload', data) | |
| } | |
| showToast('Upload cancelled', 'info'); | |
| setTimeout(() => window.location.reload(), 1000); | |
| }); | |
| 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); | |
| showToast('β¨ Upload completed successfully!', 'success'); | |
| setTimeout(() => window.location.reload(), 1000); | |
| } | |
| }, 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); | |
| showToast('β Failed to download file from URL', 'error'); | |
| setTimeout(() => window.location.reload(), 1000); | |
| } | |
| 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) { | |
| showToast(`β ${err.message}`, 'error'); | |
| setTimeout(() => window.location.reload(), 1500); | |
| } | |
| } | |
| // URL Uploader End |