Files
Chronos/src/Chronos.Server/Admin/web/capture-time.js
T
Alby96 316c182b19 fix(sync,upload): keep tombstones honest and let an explicit upload bring a photo back
Sync:
- A server frame that has not been hashed is known by content only once it is
  downloaded: one this PC deleted (or holds as its replacement) is now deleted
  from staging instead of filed again, which also dropped its tombstone.
- An unhashed local original that turns out to be a photo the server deleted
  is not uploaded, and the archive verify then writes the hashes into the
  index, so the row settles instead of offering Upload for good.
- A tombstone leaves a photo out of the comparison only when its side no longer
  holds that content, so a tombstone left beside a held copy does not keep the
  other side's copy missing forever.

Uploads:
- StartUploadRequest.Restore (optional, never sent by sync or older clients)
  lets an explicit user upload file a photo deleted from the sequence again,
  also after the trash was emptied; the completion repeats it with
  ?restore=true and the ingest drops the tombstone.
- The completion checks the tombstone again: a photo deleted while its bytes
  were on the way is not filed (409 photo_removed, upload cancelled); the
  protocol client reports that as not sent.
- A tombstone of content a frame of the target still holds refuses no repair.
- Hidden dot names (AppleDouble "._" files) are refused, as the archive
  scanner leaves them out.

Web upload dialog:
- Upload again on rows marked Deleted before; the tooltip no longer promises
  a trash restore that may be impossible.
- Sends a capture time: the Exif DateTimeOriginal of a JPEG or TIFF-based file
  (minimal in-house reader, capture-time.js), else the file's time, so photos
  without Exif keep their order.
- Esc is stopped at keydown during a run (Chromium allows one prevented cancel
  per activation); hidden dot files are left out; a completion refused while
  another one files the upload waits for it; the same photo picked under two
  names waits for its first copy.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d051bcfc351f5a08500ca864a6508576a95492a4)
2026-10-27 12:45:33 +01:00

92 lines
4.5 KiB
JavaScript

// The capture time the upload dialog sends with a photo (owner review 2, R3), as local wall time without an offset
// ("2025-05-01T06:30:00", the way the desktop clients send it): the Exif DateTimeOriginal of a JPEG or of a TIFF-based file
// (TIFF, DNG and most RAW formats), else the file's modification time. The server keeps the time a photo records itself
// and uses this one only for a photo that records none (a PNG, an export stripped of its Exif data), so such photos keep
// their order instead of taking the moment they arrived. The reader is minimal: it follows one IFD pointer, checks every
// offset against the bytes it read and gives up (null) on anything it does not expect.
/** Bytes read from the start of a file: every camera writes the Exif block within them. */
const READ = 256 * 1024;
/** The capture time of a picked File; null when neither the photo nor the file gives a usable one. */
export async function captureTime(file) {
try {
const taken = exifCaptureTime(new Uint8Array(await file.slice(0, READ).arrayBuffer()));
if (taken) return taken;
} catch {
// Unreadable here: the file's own time stands in, and the server reads the photo again anyway.
}
const modified = file.lastModified;
return Number.isFinite(modified) && modified > 0 ? localIso(new Date(modified)) : null;
}
/** The Exif DateTimeOriginal of the JPEG or TIFF whose first bytes are given, as "yyyy-MM-ddTHH:mm:ss"; null when there is none. */
export function exifCaptureTime(bytes) {
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
const size = view.byteLength;
let tiff = -1;
if (size >= 4 && view.getUint16(0) === 0xffd8) {
// JPEG: the segments before the image data, up to the APP1 that starts with "Exif\0\0".
for (let at = 2; at + 4 <= size;) {
const marker = view.getUint16(at);
if ((marker & 0xff00) !== 0xff00 || marker === 0xffda) break;
const length = view.getUint16(at + 2);
if (marker === 0xffe1 && length >= 16 && at + 10 <= size && view.getUint32(at + 4) === 0x45786966 && view.getUint16(at + 8) === 0) {
tiff = at + 10;
break;
}
if (length < 2) break;
at += 2 + length;
}
} else {
tiff = 0;
}
if (tiff < 0 || tiff + 8 > size) return null;
const order = view.getUint16(tiff);
if (order !== 0x4949 && order !== 0x4d4d) return null;
const little = order === 0x4949;
const u16 = (at) => view.getUint16(at, little);
const u32 = (at) => view.getUint32(at, little);
if (u16(tiff + 2) !== 42) return null;
// The 12-byte entry of a tag in the IFD at an offset from the TIFF header; -1 when it is not within reach.
const entry = (ifd, tag) => {
const at = tiff + ifd;
if (ifd < 8 || at + 2 > size) return -1;
const count = u16(at);
for (let i = 0; i < count; i++) {
const e = at + 2 + i * 12;
if (e + 12 > size) return -1;
if (u16(e) === tag) return e;
}
return -1;
};
const exif = entry(u32(tiff + 4), 0x8769);
if (exif < 0) return null;
const original = entry(u32(exif + 8), 0x9003);
// ASCII "YYYY:MM:DD HH:MM:SS" and its terminator, stored after the IFD.
if (original < 0 || u16(original + 2) !== 2 || u32(original + 4) < 19) return null;
const text = tiff + u32(original + 8);
if (text + 19 > size) return null;
let value = "";
for (let i = 0; i < 19; i++) value += String.fromCharCode(view.getUint8(text + i));
const m = /^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})$/.exec(value);
if (!m) return null;
const [year, month, day, hour, minute, second] = m.slice(1).map(Number);
// A real date and time only ("0000:00:00 00:00:00" is what cameras write without a clock): checked in UTC, where every
// wall time exists.
const check = new Date(Date.UTC(year, month - 1, day, hour, minute, second));
if (year < 1900 || check.getUTCFullYear() !== year || check.getUTCMonth() !== month - 1 || check.getUTCDate() !== day ||
check.getUTCHours() !== hour || check.getUTCMinutes() !== minute || check.getUTCSeconds() !== second) return null;
return `${m[1]}-${m[2]}-${m[3]}T${m[4]}:${m[5]}:${m[6]}`;
}
/** Local wall time with milliseconds and no offset; null outside the years a server date takes. */
export function localIso(date) {
const year = date.getFullYear();
if (!(year >= 1 && year <= 9999)) return null;
const p = (n, width = 2) => String(n).padStart(width, "0");
return `${p(year, 4)}-${p(date.getMonth() + 1)}-${p(date.getDate())}T${p(date.getHours())}:${p(date.getMinutes())}:${p(date.getSeconds())}` +
`.${p(date.getMilliseconds(), 3)}`;
}