Skip to content

🛡️ Warracker is an open source, self-hostable warranty tracker to monitor expirations, store receipts, files. You own the data, your rules!

Apply review fixes to photo capture

Dariusz Górecki9e07aeff758b
diff --git a/frontend/index.html b/frontend/index.html
index d7b0138..900b07a 100644
--- a/frontend/index.html
+++ b/frontend/index.html
@@ -533,11 +533,11 @@
<label data-i18n="warranties.product_photo_optional">Product Photo (Optional)</label>
<div class="file-input-wrapper">
<label for="productPhoto" class="file-input-label">
- <i class="fas fa-camera"></i> <span data-i18n="warranties.choose_photo">Choose Photo</span>
+ <i class="fas fa-upload"></i> <span data-i18n="warranties.choose_photo">Choose Photo</span>
</label>
<input type="file" id="productPhoto" name="product_photo" class="file-input" accept=".png,.jpg,.jpeg,.webp">
</div>
- <label for="productPhotoCamera" class="file-input-label">
+ <label for="productPhotoCamera" class="file-input-label" tabindex="0">
<i class="fas fa-camera"></i> <span data-i18n="warranties.take_photo">Take Photo</span>
</label>
<input type="file" accept="image/*" capture="environment" id="productPhotoCamera" style="display: none;">
@@ -941,11 +941,11 @@
<label data-i18n="warranties.product_photo_optional">Product Photo (Optional)</label>
<div class="file-input-wrapper">
<label for="editProductPhoto" class="file-input-label">
- <i class="fas fa-camera"></i> <span data-i18n="warranties.choose_photo">Choose Photo</span>
+ <i class="fas fa-upload"></i> <span data-i18n="warranties.choose_photo">Choose Photo</span>
</label>
<input type="file" id="editProductPhoto" name="product_photo" class="file-input" accept=".png,.jpg,.jpeg,.webp">
</div>
- <label for="editProductPhotoCamera" class="file-input-label">
+ <label for="editProductPhotoCamera" class="file-input-label" tabindex="0">
<i class="fas fa-camera"></i> <span data-i18n="warranties.take_photo">Take Photo</span>
</label>
<input type="file" accept="image/*" capture="environment" id="editProductPhotoCamera" style="display: none;">
@@ -1335,7 +1335,7 @@
<script type="module" src="js/index.js?v=20260518004"></script>
<script src="auth.js?v=20250119001"></script>
- <script src="script.js?v=20260719001"></script>
+ <script src="script.js?v=20260719002"></script>
<script>
// Lightweight UI logic for Filter/Sort popovers (no change to core logic)
document.addEventListener('DOMContentLoaded', function () {
diff --git a/frontend/script.js b/frontend/script.js
index 7379835..b3dc513 100644
--- a/frontend/script.js
+++ b/frontend/script.js
@@ -1206,16 +1206,19 @@ function updateFileName(event, inputId = 'invoice', outputId = 'fileName') {
}
}
-// Downscale large images client-side before upload (progressive enhancement)
-const downscaledFiles = new WeakSet();
-
+// Downscale large images client-side before upload (progressive enhancement).
+// Also re-encodes large (>= 1MB) non-JPEG images to JPEG, flattening transparency
+// onto white; animated GIFs pass through untouched to preserve animation.
async function downscaleImage(file, maxEdge = 2048, quality = 0.85) {
try {
- if (!file || (file.type && !file.type.startsWith('image/')) || typeof createImageBitmap === 'undefined') {
+ if (!file || file.type === 'image/gif' ||
+ (file.type && !file.type.startsWith('image/')) ||
+ typeof createImageBitmap === 'undefined') {
return file;
}
const bitmap = await createImageBitmap(file, { imageOrientation: 'from-image' });
+ let canvas = null;
try {
if (bitmap.width <= maxEdge && bitmap.height <= maxEdge && (file.type === 'image/jpeg' || file.size < 1024 * 1024)) {
return file;
@@ -1226,13 +1229,14 @@ async function downscaleImage(file, maxEdge = 2048, quality = 0.85) {
const width = Math.round(bitmap.width * scale);
const height = Math.round(bitmap.height * scale);
- const canvas = document.createElement('canvas');
+ canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const ctx = canvas.getContext('2d');
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(bitmap, 0, 0, width, height);
+ bitmap.close(); // free the full-res bitmap before the encode; close() is idempotent
const blob = await new Promise((resolve, reject) => {
canvas.toBlob(function(result) {
@@ -1252,6 +1256,7 @@ async function downscaleImage(file, maxEdge = 2048, quality = 0.85) {
return new File([blob], jpgName, { type: 'image/jpeg' });
} finally {
bitmap.close();
+ if (canvas) canvas.width = canvas.height = 0; // release the backing store deterministically
}
} catch (err) {
console.warn('[DEBUG] Image downscale failed, using original file:', err);
@@ -1263,56 +1268,73 @@ function setupPhotoCapture(namedInputId) {
const namedInput = document.getElementById(namedInputId);
if (!namedInput) return;
+ // Files this handler already produced or inspected; membership means
+ // "do not process again" and breaks the synthetic-change loop below.
+ const handledFiles = new WeakSet();
+
+ function trySetFiles(input, file) {
+ // Copy through a fresh DataTransfer: assigning another input's FileList directly
+ // would share one list object, and clearing the source input would empty it too.
+ try {
+ const dt = new DataTransfer();
+ dt.items.add(file);
+ input.files = dt.files;
+ return true;
+ } catch (e) {
+ console.warn('[DEBUG] Could not set files on #' + input.id + ':', e);
+ return false;
+ }
+ }
+
const cameraInput = document.getElementById(namedInputId + 'Camera');
- if (cameraInput) {
+ const cameraLabel = cameraInput ? document.querySelector('label[for="' + cameraInput.id + '"]') : null;
+ if (cameraInput && typeof DataTransfer === 'undefined' && cameraLabel) {
+ // Without DataTransfer the capture pipeline cannot deliver a file — hide the
+ // button instead of failing silently after the user has taken a photo.
+ cameraLabel.style.display = 'none';
+ } else if (cameraInput) {
+ if (cameraLabel) {
+ cameraLabel.addEventListener('keydown', (e) => {
+ if (e.key === 'Enter' || e.key === ' ') {
+ e.preventDefault();
+ cameraInput.click();
+ }
+ });
+ }
cameraInput.addEventListener('change', () => {
- const file = cameraInput.files && cameraInput.files[0];
+ let file = cameraInput.files && cameraInput.files[0];
+ cameraInput.value = '';
if (!file) return;
// Reject known non-image types only; some Android pickers report an empty
// MIME type for valid camera JPEGs (capture is a hint; desktop pickers allow any file).
- if (file.type && !file.type.startsWith('image/')) {
- cameraInput.value = '';
- return;
+ if (file.type && !file.type.startsWith('image/')) return;
+ if (!file.type) {
+ // Normalize so preview code (gated on image/*) works; camera captures are JPEG.
+ file = new File([file], file.name, { type: 'image/jpeg' });
}
// Synchronous copy: the named input is populated immediately, so a fast
- // Save never races an async gap; downscaling happens via the change listener below.
- // Copy through a fresh DataTransfer — assigning cameraInput.files directly would
- // share one FileList, and clearing the camera input below would empty it too.
- try {
- const dt = new DataTransfer();
- dt.items.add(file);
- namedInput.files = dt.files;
- } catch (e) {
- console.warn('[DEBUG] Camera file copy failed:', e);
- cameraInput.value = '';
- return;
- }
- cameraInput.value = '';
+ // Save never races the async downscale in the change listener below.
+ if (!trySetFiles(namedInput, file)) return;
+ // Note: change listeners on this input fire again after the downscale
+ // swap, with a different File — listeners must be idempotent.
namedInput.dispatchEvent(new Event('change', { bubbles: true }));
});
}
// Downscale-on-change for this input — independent of camera markup presence.
- let generation = 0;
namedInput.addEventListener('change', async () => {
const file = namedInput.files && namedInput.files[0];
- if (!file || downscaledFiles.has(file)) return;
- // Empty MIME types still get a downscale attempt: createImageBitmap sniffs content,
- // and a non-image simply fails decode and falls back to the original.
- if (file.type && !file.type.startsWith('image/')) return;
- const gen = ++generation;
+ if (!file || handledFiles.has(file)) return;
const processed = await downscaleImage(file);
- if (processed === file) return; // nothing to improve
- if (gen !== generation) return; // a newer selection superseded this run
- if (namedInput.files[0] !== file) return; // input was cleared/replaced (form reset, modal close)
- try {
- const dt = new DataTransfer();
- dt.items.add(processed);
- downscaledFiles.add(processed);
- namedInput.files = dt.files;
- namedInput.dispatchEvent(new Event('change', { bubbles: true })); // refresh label/preview with the processed file
- } catch (e) {
- console.warn('[DEBUG] Downscale swap failed, uploading original:', e);
+ if (processed === file) {
+ handledFiles.add(file); // nothing to improve; skip re-inspection on re-fired events
+ return;
+ }
+ if (namedInput.files[0] !== file) return; // input was cleared/replaced meanwhile (new pick, form reset)
+ handledFiles.add(processed);
+ if (trySetFiles(namedInput, processed)) {
+ // Second change with the swapped (downscaled) File — see note above.
+ namedInput.dispatchEvent(new Event('change', { bubbles: true }));
}
});
}
diff --git a/frontend/status.html b/frontend/status.html
index 348bb76..be3ed4e 100644
--- a/frontend/status.html
+++ b/frontend/status.html
@@ -37,7 +37,7 @@
<script src="js/i18n.js?v=20250119001"></script>
<!-- Load fix for auth buttons -->
<script src="fix-auth-buttons-loader.js?v=20250119001"></script>
- <script src="script.js?v=20260719001" defer></script> <!-- Added script.js -->
+ <script src="script.js?v=20260719002" defer></script> <!-- Added script.js -->
<script src="status.js?v=20250119001" defer></script> <!-- Status page specific functionality -->
<style>
.user-menu {
@@ -670,7 +670,7 @@
</label>
<input type="file" id="editProductPhoto" name="product_photo" class="file-input" accept=".png,.jpg,.jpeg,.webp">
</div>
- <label for="editProductPhotoCamera" class="file-input-label">
+ <label for="editProductPhotoCamera" class="file-input-label" tabindex="0">
<i class="fas fa-camera"></i> <span data-i18n="warranties.take_photo">Take Photo</span>
</label>
<input type="file" accept="image/*" capture="environment" id="editProductPhotoCamera" style="display: none;">
diff --git a/frontend/sw.js b/frontend/sw.js
index 9015dbd..9889772 100644
--- a/frontend/sw.js
+++ b/frontend/sw.js
@@ -1,4 +1,4 @@
-const CACHE_NAME = 'warracker-cache-v20260719001';
+const CACHE_NAME = 'warracker-cache-v20260719002';
const urlsToCache = [
// HTML pages
'./',
@@ -21,7 +21,7 @@ const urlsToCache = [
'./mobile-header.css?v=20250119002',
// JavaScript (versioned)
- './script.js?v=20260719001',
+ './script.js?v=20260719002',
'./auth.js?v=20250119001',
'./settings-new.js?v=20250119001',
'./status.js?v=20250119001',