Bulk snooze, alert rules judged on report, photos on notes
Bulk snooze: Snooze... and Wake on the ticked alerts, with undo that puts back each alert's earlier snooze exactly. Alert rules are judged for a server right after its agent reports, instead of at the next 15-minute pass; only that server's rule findings change, and the check is serialized with the pass so neither overwrites the other's state. Photos on server and device notes: up to 12 each, shrunk in the browser, accepted by their own bytes (JPEG/PNG/GIF/WebP, 4 MB), kept in the database and removed with the server or device. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
251a4a9d73
commit
a16996d975
27 files changed
+8442
-54
No files matched your search
@@ -0,0 +1,43 @@
|
||||
/** The most a photo may be, on its longer side, and the most the app will send (the server's own limit is 4 MB). */
|
||||
export const MAX_EDGE = 1600;
|
||||
export const MAX_SEND_BYTES = 3.5 * 1024 * 1024;
|
||||
|
||||
const canvasToBlob = (canvas: HTMLCanvasElement, type: string, quality: number) => new Promise<Blob | null>((resolve) => canvas.toBlob(resolve, type, quality));
|
||||
|
||||
/**
|
||||
* Makes a photo small before it is sent: a phone's picture is several megabytes, and 1600 pixels on the long side, as a JPEG, is a few hundred kilobytes and enough to read a label.
|
||||
* The way the phone was held is honoured (EXIF orientation), and the picture is drawn again, which also leaves out where it was taken. A GIF is sent as it is (so it still moves) and
|
||||
* a small PNG too (a screenshot stays sharp); one that cannot be drawn (a format the browser does not know) is sent as it is if it is small enough, and refused with a reason if not.
|
||||
*/
|
||||
export async function shrinkPhoto(file: File): Promise<Blob> {
|
||||
if (file.type === "image/gif") {
|
||||
if (file.size > MAX_SEND_BYTES) throw new Error("That GIF is too big — keep it under 3 MB.");
|
||||
return file;
|
||||
}
|
||||
if (file.type === "image/png" && file.size <= 1.5 * 1024 * 1024) return file;
|
||||
let bitmap: ImageBitmap;
|
||||
try {
|
||||
bitmap = await createImageBitmap(file, { imageOrientation: "from-image" });
|
||||
} catch {
|
||||
if (["image/jpeg", "image/png", "image/webp"].includes(file.type) && file.size <= MAX_SEND_BYTES) return file;
|
||||
throw new Error("This browser could not read that picture. Try a JPEG or a PNG.");
|
||||
}
|
||||
try {
|
||||
const scale = Math.min(1, MAX_EDGE / Math.max(bitmap.width, bitmap.height));
|
||||
const canvas = document.createElement("canvas");
|
||||
canvas.width = Math.max(1, Math.round(bitmap.width * scale));
|
||||
canvas.height = Math.max(1, Math.round(bitmap.height * scale));
|
||||
const ctx = canvas.getContext("2d");
|
||||
if (!ctx) throw new Error("This browser could not draw that picture.");
|
||||
ctx.fillStyle = "#ffffff"; // a transparent picture becomes white rather than black
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
||||
ctx.drawImage(bitmap, 0, 0, canvas.width, canvas.height);
|
||||
for (const quality of [0.85, 0.7, 0.55]) {
|
||||
const blob = await canvasToBlob(canvas, "image/jpeg", quality);
|
||||
if (blob && blob.size <= MAX_SEND_BYTES) return blob;
|
||||
}
|
||||
throw new Error("That picture is still too big after making it smaller.");
|
||||
} finally {
|
||||
bitmap.close();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user