import { StaffPhotoError } from '../../../packages/contracts/src/staff-photo.js'; import { staffPath } from './staff-platform.js'; const message = { memory: 'This HEIC is too large for safe local decoding. Export a JPEG or PNG copy under 20 megapixels and retry. Nothing was uploaded; the original stays on this phone.', colour: 'This HEIC uses HDR or a colour profile this local decoder cannot safely prepare. In Photos, export an SDR/sRGB JPEG or PNG copy and retry. Nothing was uploaded.', sequence: 'Choose one still HEIC photo, or export one JPEG frame from Photos. A burst/sequence cannot be prepared as one site photo. Nothing was uploaded.', platform: 'The local HEIC decoder is unavailable. Reconnect and check Staff’s offline files/update, then retry. If needed, export a JPEG copy from Photos or use Camera → Formats → Most Compatible. Nothing was uploaded.', decode: 'This HEIC could not be decoded locally. In Photos, export/share a JPEG copy and retry, or use Camera → Formats → Most Compatible. Nothing was uploaded; the original remains on this phone.', }; export function decodeStaffHeic(source, { signal, onProgress }) { if (typeof Worker !== 'function') throw new StaffPhotoError(message.platform,'STAFF_PHOTO_HEIC_DECODER'); return new Promise((resolve,reject) => { let worker; const cleanup = () => { signal.removeEventListener('abort',stop); worker?.terminate(); }; const stop = () => { cleanup(); reject(signal.reason); }; try { worker = new Worker(staffPath('photo-decoder/libheif-1.23.6/worker.js'),{type:'module',name:'staff-local-heic'}); worker.onmessage = ({data}) => { if (data.progress) { onProgress({status:'preparing',progress:data.progress,message:'Decoding HEIC privately on this phone…'}); return; } cleanup(); if (data.error || !(data.blob instanceof Blob) || data.blob.type !== 'image/png') { reject(new StaffPhotoError(message[data.error] || message.decode,'STAFF_PHOTO_HEIC_DECODER')); return; } resolve(data); }; worker.onerror = event => { event.preventDefault(); cleanup(); reject(new StaffPhotoError(message.platform,'STAFF_PHOTO_HEIC_DECODER')); }; signal.addEventListener('abort',stop,{once:true}); if (signal.aborted) { stop(); return; } // This is a private decoder copy. The File and its saved phone original // retain their own immutable bytes; only this temporary buffer is moved. worker.postMessage({buffer:source.buffer},[source.buffer]); } catch { cleanup(); reject(new StaffPhotoError(message.platform,'STAFF_PHOTO_HEIC_DECODER')); } }); }