home
/
aioutajg
/
public_html
/
resizer
/
assets
/
js
/
Go to Home Directory
+
Upload
Create File
root@0UT1S:~$
Execute
By Order of Mr.0UT1S
[DIR] ..
N/A
script.js
8.91 KB
Rename
Delete
document.addEventListener('DOMContentLoaded', () => { const uploadContainer = document.getElementById('uploadContainer'); const fileInput = document.getElementById('fileInput'); const uploadButton = document.getElementById('uploadButton'); const clearButton = document.getElementById('clearButton'); const convertForm = document.getElementById('uploadForm'); const widthInput = document.getElementById('width'); const heightInput = document.getElementById('height'); const socialMediaSizeSelect = document.getElementById('socialMediaSize'); const convertedSection = document.getElementById('convertedSection'); const downloadLinks = document.getElementById('downloadLinks'); const deleteButton = document.getElementById('deleteButton'); const progressLoader = document.getElementById('progressLoader'); const thumbnailsContainer = document.getElementById('displayThumbnails'); let selectedFiles = []; // Drag-and-drop functionality ['dragenter', 'dragover', 'dragleave', 'drop'].forEach((eventName) => { uploadContainer.addEventListener(eventName, preventDefaults, false); }); function preventDefaults(e) { e.preventDefault(); e.stopPropagation(); } // Handle dropped files uploadContainer.addEventListener('drop', (e) => { const files = e.dataTransfer.files; handleFiles(files); }); // Trigger file input when clicking the upload button uploadButton.addEventListener('click', () => fileInput.click()); // Handle file selection via file input or drag-and-drop fileInput.addEventListener('change', (e) => handleFiles(e.target.files)); // Handle files function handleFiles(files) { if (files.length > 0) { selectedFiles = Array.from(files).slice(0, 50); // Limit to 50 files displayThumbnails(selectedFiles); clearButton.style.display = 'inline-block'; } } // Display thumbnails with close buttons function displayThumbnails(files) { thumbnailsContainer.innerHTML = ''; // Clear existing thumbnails files.forEach((file) => { displayThumbnail(file); }); } // Display a single thumbnail with a close button function displayThumbnail(file) { const thumbnailDiv = document.createElement('div'); thumbnailDiv.className = 'position-relative d-inline-block'; thumbnailDiv.style.width = '160px'; const img = document.createElement('img'); img.src = URL.createObjectURL(file); img.style.maxWidth = '150px'; img.style.maxHeight = '150px'; img.style.borderRadius = '8px'; img.style.objectFit = 'cover'; // Close button const closeButton = document.createElement('button'); closeButton.className = 'btn-close position-absolute top-0 end-0 m-1'; closeButton.style.zIndex = '1'; closeButton.style.backgroundColor = 'rgba(255, 255, 255, 0.7)'; closeButton.onclick = () => removeFile(file); thumbnailDiv.appendChild(img); thumbnailDiv.appendChild(closeButton); thumbnailsContainer.appendChild(thumbnailDiv); } // Remove a selected file function removeFile(file) { selectedFiles = selectedFiles.filter((selected) => selected !== file); displayThumbnails(selectedFiles); } // Clear all selected files clearButton.addEventListener('click', () => { selectedFiles = []; fileInput.value = ''; uploadContainer.querySelector('p').textContent = 'Drag & Drop your images here or click to upload'; clearButton.style.display = 'none'; uploadContainer.classList.remove('error'); thumbnailsContainer.innerHTML = ''; }); // CAPTCHA generation const generateCaptcha = () => { const num1 = Math.floor(Math.random() * 10); const num2 = Math.floor(Math.random() * 10); const answer = num1 + num2; document.getElementById('captchaQuestion').textContent = `${num1} + ${num2}`; document.getElementById('captchaAnswer').value = answer; }; generateCaptcha(); // Form submission convertForm.addEventListener('submit', async (e) => { e.preventDefault(); // Validate CAPTCHA const captchaInput = document.getElementById('captchaInput'); const captchaAnswer = document.getElementById('captchaAnswer'); if (parseInt(captchaInput.value) !== parseInt(captchaAnswer.value)) { alert('Invalid CAPTCHA. Please try again.'); generateCaptcha(); return; } // Validate file selection if (selectedFiles.length === 0) { alert('Please select images to resize.'); return; } // Show loader progressLoader.classList.remove('d-none'); const formData = new FormData(); selectedFiles.forEach((file) => { formData.append('image', file); }); // Get dimensions or social media size const width = widthInput.value; const height = heightInput.value; const socialMediaSize = socialMediaSizeSelect.value; if (socialMediaSize) { formData.append('socialMediaSize', socialMediaSize); } else if (width && height) { formData.append('width', width); formData.append('height', height); } else { alert('Please enter valid dimensions or select a social media size.'); progressLoader.classList.add('d-none'); return; } try { const response = await fetch('/resizer/resize.php', { method: 'POST', body: formData, }); progressLoader.classList.add('d-none'); if (!response.ok) { throw new Error(`HTTP error! Status: ${response.status}`); } const result = await response.json(); if (result.error) { alert(result.error); } else { convertedSection.classList.remove('d-none'); downloadLinks.innerHTML = ''; if (result.zip) { // ZIP file for multiple images const zipLink = document.createElement('a'); zipLink.href = `/resizer/download.php?file=${result.zip}`; zipLink.textContent = 'Download ZIP File'; zipLink.className = 'btn btn-primary d-block mb-3'; zipLink.download = result.zip; // Trigger download downloadLinks.appendChild(zipLink); } else { // Individual files if (result.files) { result.files.forEach((file) => { const link = document.createElement('a'); link.href = `/resizer/download.php?file=${file}`; link.textContent = `Download ${file}`; link.className = 'btn btn-primary d-block mb-3'; link.download = file; // Trigger download downloadLinks.appendChild(link); }); } else if (result.file) { // For a single resized file const link = document.createElement('a'); link.href = `/resizer/download.php?file=${result.file}`; link.textContent = `Download ${result.file}`; link.className = 'btn btn-primary d-block mb-3'; link.download = result.file; // Trigger download downloadLinks.appendChild(link); } } // Set delete button data deleteButton.dataset.file = result.zip || result.file || ''; } } catch (error) { progressLoader.classList.add('d-none'); alert(`An error occurred: ${error.message}`); } }); // Delete button functionality deleteButton.addEventListener('click', async () => { const fileName = deleteButton.dataset.file; if (!fileName) return; try { const response = await fetch(`/resizer/delete.php?file=${fileName}`); const result = await response.json(); if (result.success) { alert('File deleted successfully.'); convertedSection.classList.add('d-none'); downloadLinks.innerHTML = ''; } else { alert('Failed to delete file.'); } } catch (error) { alert('An error occurred while deleting the file.'); } }); });
Save