Spaces:
Sleeping

tiny / index.html
Beikx's picture
Upload 3 files
93f7b1e verified
Raw History Blame Contribute Delete
11.4 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>TinyPNG Local</title>
<style>
:root {
--primary: #5ec75e;
--bg: #f5f7fa;
--text: #333;
--card-bg: #fff;
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: var(--bg);
color: var(--text);
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
min-height: 100vh;
}
header {
background: #fff;
width: 100%;
padding: 1rem 0;
text-align: center;
box-shadow: 0 2px 10px rgba(0,0,0,0.05);
margin-bottom: 2rem;
}
h1 { margin: 0; color: #333; display: flex; align-items: center; justify-content: center; gap: 10px;}
.container {
width: 90%;
max-width: 800px;
background: var(--card-bg);
border-radius: 12px;
padding: 2rem;
box-shadow: 0 4px 20px rgba(0,0,0,0.05);
margin-bottom: 3rem;
}
#drop-zone {
border: 3px dashed #d0d7de;
border-radius: 12px;
padding: 3rem;
text-align: center;
cursor: pointer;
transition: all 0.2s;
background: #fafbfc;
}
#drop-zone:hover, #drop-zone.dragover {
border-color: var(--primary);
background: #f0fff0;
}
#drop-zone p {
font-size: 1.2rem;
color: #666;
margin: 10px 0 0;
}
.icon { font-size: 3rem; }
.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
margin-top: 2rem;
padding-bottom: 10px;
border-bottom: 2px solid #f0f0f0;
}
.btn-action {
background: #333;
color: white;
border: none;
padding: 10px 20px;
border-radius: 6px;
font-size: 1rem;
cursor: pointer;
transition: background 0.2s;
text-decoration: none;
display: inline-flex;
align-items: center;
gap: 5px;
}
.btn-action:hover { background: #555; }
.btn-action:disabled { background: #ccc; cursor: not-allowed; }
.btn-download-all {
background: var(--primary);
}
.btn-download-all:hover { background: #4db04d; }
#file-list {
width: 100%;
border-collapse: collapse;
margin-top: 10px;
}
#file-list th, #file-list td {
text-align: left;
padding: 12px;
border-bottom: 1px solid #eee;
}
#file-list th { color: #888; font-weight: 500; font-size: 0.9rem; }
.status-pending { color: #888; }
.status-processing { color: #e6a23c; }
.status-done { color: var(--primary); font-weight: bold; }
.status-error { color: #f56c6c; }
.btn-download {
background: var(--primary);
color: white;
text-decoration: none;
padding: 6px 12px;
border-radius: 4px;
font-size: 0.9rem;
display: inline-block;
transition: opacity 0.2s;
}
.btn-download:hover { opacity: 0.9; }
.progress-bar {
height: 4px;
background: #eee;
border-radius: 2px;
overflow: hidden;
width: 100px;
display: inline-block;
vertical-align: middle;
margin-right: 10px;
}
.progress-fill {
height: 100%;
background: var(--primary);
width: 0%;
transition: width 0.3s;
}
/* Hidden input */
#file-input { display: none; }
.hidden { display: none; }
</style>
</head>
<body>
<header>
<h1><span>🐼</span> TinyPNG Local</h1>
</header>
<div class="container">
<div id="drop-zone">
<div class="icon">📂</div>
<p>Drag & drop PNG or JPG images here</p>
<div style="font-size: 0.9rem; color: #999; margin-top: 5px;">or click to select files</div>
</div>
<input type="file" id="file-input" multiple accept="image/png,image/jpeg,image/jpg">
<div id="result-area" class="hidden">
<div class="toolbar">
<div style="font-weight: bold; color: #666;">Processed Files</div>
<button id="btn-zip" class="btn-action btn-download-all" disabled>
⬇️ Download All as ZIP
</button>
</div>
<table id="file-list">
<thead>
<tr>
<th>File Name</th>
<th>Original</th>
<th>Compressed</th>
<th>Status</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<!-- Items will be added here -->
</tbody>
</table>
</div>
</div>
<script>
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
const resultArea = document.getElementById('result-area');
const fileListTable = document.getElementById('file-list');
const tbody = fileListTable.querySelector('tbody');
const btnZip = document.getElementById('btn-zip');
let processedFiles = []; // Store paths for zip
// Drag & Drop handlers
dropZone.addEventListener('click', () => fileInput.click());
dropZone.addEventListener('dragover', (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
});
dropZone.addEventListener('dragleave', () => dropZone.classList.remove('dragover'));
dropZone.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('dragover');
handleFiles(e.dataTransfer.files);
});
fileInput.addEventListener('change', (e) => handleFiles(e.target.files));
btnZip.addEventListener('click', downloadZip);
function formatSize(bytes) {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
function handleFiles(files) {
if (files.length > 0) {
resultArea.classList.remove('hidden');
}
Array.from(files).forEach(file => {
if (!file.type.match('image.*')) return;
uploadFile(file);
});
}
function uploadFile(file) {
const row = document.createElement('tr');
row.innerHTML = `
<td>${file.name}</td>
<td>${formatSize(file.size)}</td>
<td>...</td>
<td>
<div class="progress-bar"><div class="progress-fill" style="width: 0%"></div></div>
<span class="status-text status-pending">Pending</span>
</td>
<td>-</td>
`;
tbody.appendChild(row);
// Upload
const xhr = new XMLHttpRequest();
// Encode filename to handle special characters safely in header
const encodedName = encodeURIComponent(file.name);
xhr.open('POST', `/upload?filename=${encodedName}`, true);
// We send raw bytes
xhr.setRequestHeader('Content-Type', 'application/octet-stream');
xhr.upload.onprogress = (e) => {
if (e.lengthComputable) {
const percent = (e.loaded / e.total) * 100;
row.querySelector('.progress-fill').style.width = percent + '%';
if (percent === 100) {
row.querySelector('.status-text').textContent = 'Compressing...';
row.querySelector('.status-text').className = 'status-text status-processing';
}
}
};
xhr.onload = () => {
if (xhr.status === 200) {
const result = JSON.parse(xhr.responseText);
if (result.status === 'success' || result.status === 'skipped') {
const savedPercent = ((result.original_size - result.new_size) / result.original_size * 100).toFixed(1);
row.querySelector('td:nth-child(3)').textContent = formatSize(result.new_size);
let statusHtml = '';
if (result.status === 'skipped') {
statusHtml = `<span class="status-done" style="color:#e6a23c">Skipped (Larger)</span>`;
} else {
statusHtml = `<span class="status-done">Done (-${savedPercent}%)</span>`;
}
row.querySelector('td:nth-child(4)').innerHTML = statusHtml;
// Download button
const downloadUrl = `/download?path=${encodeURIComponent(result.path)}`;
row.querySelector('td:nth-child(5)').innerHTML = `<a href="${downloadUrl}" class="btn-download" download="${file.name}">Download</a>`;
// Add to processed list for zip
processedFiles.push(result.path);
btnZip.disabled = false;
} else {
row.querySelector('.status-text').textContent = 'Error';
row.querySelector('.status-text').className = 'status-text status-error';
}
} else {
row.querySelector('.status-text').textContent = 'Error';
row.querySelector('.status-text').className = 'status-text status-error';
}
};
xhr.send(file);
}
function downloadZip() {
if (processedFiles.length === 0) return;
btnZip.textContent = "Creating ZIP...";
btnZip.disabled = true;
fetch('/zip', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ files: processedFiles })
})
.then(response => response.json())
.then(data => {
if (data.status === 'success') {
window.location.href = `/download?path=${encodeURIComponent(data.zip_path)}`;
btnZip.textContent = "⬇️ Download All as ZIP";
btnZip.disabled = false;
} else {
alert('Failed to create ZIP');
btnZip.textContent = "⬇️ Download All as ZIP";
btnZip.disabled = false;
}
})
.catch(error => {
console.error('Error:', error);
btnZip.textContent = "⬇️ Download All as ZIP";
btnZip.disabled = false;
});
}
</script>
</body>
</html>