File size: 11,418 Bytes
93f7b1e | 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 | <!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>
|