Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review (codex r2): bake PNG EXIF orientation before lossless strip (#1763)

[._.]/ Adam Eivyd3b9c9a1b789
diff --git a/server/lib/imageClean.js b/server/lib/imageClean.js
index a010067394..8497eeb47c 100644
--- a/server/lib/imageClean.js
+++ b/server/lib/imageClean.js
@@ -269,20 +269,35 @@ export async function cleanImageBuffer(buffer, options = {}) {
if (metadata) steps.push({ step: 'metadata', status: 'applied', lossless: false, detail: 'dropped via re-encode' });
steps.push({ step: 'denoise', status: 'applied', lossless: false, detail: 'median(3) + sharpen' });
} else if (metadata && format === 'png') {
- // Lossless PNG path: walk chunks and emit a new buffer minus the metadata
- // chunks. Pixels byte-identical, no decode/re-encode.
- const stripped = stripPngMetadataChunks(buffer);
- outData = stripped.data;
- c2paStripped = stripped.droppedTypes.includes('caBX');
const meta = await sharp(buffer, { limitInputPixels: MAX_PIXELS }).metadata();
- width = meta.width || null;
- height = meta.height || null;
- steps.push({
- step: 'metadata',
- status: stripped.stripped ? 'applied' : 'noop',
- lossless: true,
- detail: stripped.stripped ? `dropped ${stripped.droppedTypes.join(', ')}` : 'no metadata chunks found',
- });
+ if (meta.orientation && meta.orientation > 1) {
+ // The PNG carries a non-default EXIF Orientation (in its eXIf chunk). A
+ // pure chunk strip would drop that tag and visibly rotate/flip the image
+ // in viewers that honor PNG orientation. Bake the rotation into pixels
+ // (re-encode → lossy) before dropping metadata, exactly like the
+ // JPEG/WebP path — correctness wins over losslessness here.
+ const base = sharp(buffer, { limitInputPixels: MAX_PIXELS }).rotate();
+ const { data, info } = await applyEncoder(base, format).toBuffer({ resolveWithObject: true });
+ outData = data;
+ width = info.width || null;
+ height = info.height || null;
+ c2paStripped = hadC2PA;
+ steps.push({ step: 'metadata', status: 'applied', lossless: false, detail: 'orientation baked + dropped via re-encode (lossy)' });
+ } else {
+ // Lossless PNG path: walk chunks and emit a new buffer minus the
+ // metadata chunks. Pixels byte-identical, no decode/re-encode.
+ const stripped = stripPngMetadataChunks(buffer);
+ outData = stripped.data;
+ c2paStripped = stripped.droppedTypes.includes('caBX');
+ width = meta.width || null;
+ height = meta.height || null;
+ steps.push({
+ step: 'metadata',
+ status: stripped.stripped ? 'applied' : 'noop',
+ lossless: true,
+ detail: stripped.stripped ? `dropped ${stripped.droppedTypes.join(', ')}` : 'no metadata chunks found',
+ });
+ }
} else if (metadata) {
// JPEG/WebP have no cheap lossless chunk-walk; re-encode through sharp,
// which drops metadata by default. .rotate() bakes EXIF orientation into
diff --git a/server/lib/imageClean.test.js b/server/lib/imageClean.test.js
index 6170fb0f60..28b3321f83 100644
--- a/server/lib/imageClean.test.js
+++ b/server/lib/imageClean.test.js
@@ -347,6 +347,25 @@ describe('cleanImageBuffer (composable steps)', () => {
expect(result.height).toBe(32);
});
+ it('bakes orientation into pixels (lossy) when a PNG carries a non-default EXIF orientation', async () => {
+ // 4×8 PNG re-emitted with EXIF Orientation=6 (rotate 90° CW). A pure chunk
+ // strip would drop the tag and visibly rotate it, so the metadata-only path
+ // must bake the rotation → output is 8×4 and flagged lossy.
+ const raw = await sharp({
+ create: { width: 4, height: 8, channels: 3, background: { r: 10, g: 20, b: 30 } },
+ }).png().toBuffer();
+ const oriented = await sharp(raw).withMetadata({ orientation: 6 }).png().toBuffer();
+
+ const result = await cleanImageBuffer(oriented, { metadata: true, denoise: false });
+ expect(result.width).toBe(8);
+ expect(result.height).toBe(4);
+ const meta = result.steps.find((s) => s.step === 'metadata');
+ expect(meta.lossless).toBe(false);
+ // The baked output no longer carries an active orientation tag.
+ const outOrientation = (await sharp(result.data).metadata()).orientation;
+ expect(outOrientation === undefined || outOrientation === 1).toBe(true);
+ });
+
it('no steps selected returns the input untouched with an empty report', async () => {
const result = await cleanImageBuffer(pngFixture, { metadata: false, denoise: false });
expect(result.steps).toEqual([]);