Start every Rule private, with a rotating link and files that follow its readers.
New Rules stay off lists until the person who holds the pen turns on a share link, or the team features one. Uploaded files are readable only by those readers, stripped of location metadata, and deleted with the Rule, draft, or account.
This commit is contained in:
1 parent
642551064c
commit
5cbda35e32
50 files changed
+2429
-219
No files matched your search
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* Strip GPS/author metadata from image bytes. JPEG APP1 (EXIF) and PNG
|
||||
* text / eXIf chunks are dropped. Other types are returned unchanged.
|
||||
*/
|
||||
export function stripImageMetadata(buffer: Buffer, mimeType: string): Buffer {
|
||||
const mime = mimeType.toLowerCase().split(";")[0]?.trim() ?? "";
|
||||
if (mime === "image/jpeg" || mime === "image/jpg") {
|
||||
return stripJpegExif(buffer);
|
||||
}
|
||||
if (mime === "image/png") {
|
||||
return stripPngMetadata(buffer);
|
||||
}
|
||||
return buffer;
|
||||
}
|
||||
|
||||
function stripJpegExif(buffer: Buffer): Buffer {
|
||||
if (buffer.length < 4 || buffer[0] !== 0xff || buffer[1] !== 0xd8) {
|
||||
return buffer;
|
||||
}
|
||||
const out: Buffer[] = [buffer.subarray(0, 2)];
|
||||
let i = 2;
|
||||
while (i + 3 < buffer.length) {
|
||||
if (buffer[i] !== 0xff) {
|
||||
out.push(buffer.subarray(i));
|
||||
break;
|
||||
}
|
||||
const marker = buffer[i + 1]!;
|
||||
if (marker === 0xda) {
|
||||
out.push(buffer.subarray(i));
|
||||
break;
|
||||
}
|
||||
if (marker === 0xd9) {
|
||||
out.push(buffer.subarray(i, i + 2));
|
||||
break;
|
||||
}
|
||||
if (marker >= 0xd0 && marker <= 0xd7) {
|
||||
out.push(buffer.subarray(i, i + 2));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (marker === 0x01 || marker === 0xd8) {
|
||||
out.push(buffer.subarray(i, i + 2));
|
||||
i += 2;
|
||||
continue;
|
||||
}
|
||||
if (i + 3 >= buffer.length) break;
|
||||
const size = buffer.readUInt16BE(i + 2);
|
||||
const next = i + 2 + size;
|
||||
if (size < 2 || next > buffer.length) {
|
||||
out.push(buffer.subarray(i));
|
||||
break;
|
||||
}
|
||||
const isApp1 = marker === 0xe1;
|
||||
const isApp13 = marker === 0xed;
|
||||
if (!isApp1 && !isApp13) {
|
||||
out.push(buffer.subarray(i, next));
|
||||
}
|
||||
i = next;
|
||||
}
|
||||
return Buffer.concat(out);
|
||||
}
|
||||
|
||||
function stripPngMetadata(buffer: Buffer): Buffer {
|
||||
const sig = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
|
||||
if (buffer.length < 16 || !buffer.subarray(0, 8).equals(sig)) {
|
||||
return buffer;
|
||||
}
|
||||
const skip = new Set(["tEXt", "iTXt", "zTXt", "eXIf", "tIME"]);
|
||||
const out: Buffer[] = [buffer.subarray(0, 8)];
|
||||
let i = 8;
|
||||
while (i + 12 <= buffer.length) {
|
||||
const length = buffer.readUInt32BE(i);
|
||||
const type = buffer.subarray(i + 4, i + 8).toString("ascii");
|
||||
const chunkEnd = i + 12 + length;
|
||||
if (chunkEnd > buffer.length) {
|
||||
out.push(buffer.subarray(i));
|
||||
break;
|
||||
}
|
||||
if (!skip.has(type)) {
|
||||
out.push(buffer.subarray(i, chunkEnd));
|
||||
}
|
||||
i = chunkEnd;
|
||||
if (type === "IEND") break;
|
||||
}
|
||||
return Buffer.concat(out);
|
||||
}
|
||||
Reference in new issue
Block a user