Return metadata as JSON body instead of response header
Fly.io proxy was stripping the custom X-Removed-Metadata header.
Now the web UI requests Accept: application/json and the /strip
endpoint returns {image, metadata} as JSON. Raw JPEG clients
still get the old behavior.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
61
server.js
61
server.js
@@ -394,6 +394,7 @@ app.get("/", (_req, res) => {
|
|||||||
headers: {
|
headers: {
|
||||||
"Authorization": "Bearer " + token,
|
"Authorization": "Bearer " + token,
|
||||||
"Content-Type": file.type || "image/jpeg",
|
"Content-Type": file.type || "image/jpeg",
|
||||||
|
"Accept": "application/json",
|
||||||
},
|
},
|
||||||
body: file,
|
body: file,
|
||||||
});
|
});
|
||||||
@@ -410,30 +411,33 @@ app.get("/", (_req, res) => {
|
|||||||
throw new Error(err.message || "Strip failed");
|
throw new Error(err.message || "Strip failed");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const data = await res.json();
|
||||||
|
|
||||||
// Show removed metadata
|
// Show removed metadata
|
||||||
const metaHeader = res.headers.get("X-Removed-Metadata");
|
const meta = data.metadata;
|
||||||
if (metaHeader) {
|
if (meta) {
|
||||||
try {
|
const keys = Object.keys(meta);
|
||||||
const meta = JSON.parse(metaHeader);
|
if (keys.length > 0) {
|
||||||
const keys = Object.keys(meta);
|
keys.forEach(function(k) {
|
||||||
if (keys.length > 0) {
|
const tr = document.createElement("tr");
|
||||||
keys.forEach(function(k) {
|
const td1 = document.createElement("td");
|
||||||
const tr = document.createElement("tr");
|
td1.textContent = k;
|
||||||
const td1 = document.createElement("td");
|
const td2 = document.createElement("td");
|
||||||
td1.textContent = k;
|
td2.textContent = meta[k];
|
||||||
const td2 = document.createElement("td");
|
if (meta[k].includes("⚠")) td2.className = "warn";
|
||||||
td2.textContent = meta[k];
|
tr.appendChild(td1);
|
||||||
if (meta[k].includes("⚠")) td2.className = "warn";
|
tr.appendChild(td2);
|
||||||
tr.appendChild(td1);
|
removedBody.appendChild(tr);
|
||||||
tr.appendChild(td2);
|
});
|
||||||
removedBody.appendChild(tr);
|
removedDiv.style.display = "";
|
||||||
});
|
}
|
||||||
removedDiv.style.display = "";
|
|
||||||
}
|
|
||||||
} catch {}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = await res.blob();
|
// Download clean image
|
||||||
|
const byteString = atob(data.image);
|
||||||
|
const bytes = new Uint8Array(byteString.length);
|
||||||
|
for (let i = 0; i < byteString.length; i++) bytes[i] = byteString.charCodeAt(i);
|
||||||
|
const blob = new Blob([bytes], { type: "image/jpeg" });
|
||||||
const url = URL.createObjectURL(blob);
|
const url = URL.createObjectURL(blob);
|
||||||
const a = document.createElement("a");
|
const a = document.createElement("a");
|
||||||
const name = (file.name || "photo").replace(/\\.[^.]+$/, "") + "_clean.jpg";
|
const name = (file.name || "photo").replace(/\\.[^.]+$/, "") + "_clean.jpg";
|
||||||
@@ -536,11 +540,18 @@ app.post("/strip", async (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isAborted(req, res)) return;
|
if (isAborted(req, res)) return;
|
||||||
|
|
||||||
|
// If client wants JSON, return image + metadata together
|
||||||
|
const wantsJson = String(req.headers["accept"] || "").includes("application/json");
|
||||||
|
if (wantsJson) {
|
||||||
|
res.setHeader("Content-Type", "application/json");
|
||||||
|
return res.json({
|
||||||
|
image: jpeg.toString("base64"),
|
||||||
|
metadata: removed,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
res.setHeader("Content-Type", "image/jpeg");
|
res.setHeader("Content-Type", "image/jpeg");
|
||||||
try {
|
|
||||||
res.setHeader("X-Removed-Metadata", JSON.stringify(removed));
|
|
||||||
res.setHeader("Access-Control-Expose-Headers", "X-Removed-Metadata");
|
|
||||||
} catch {}
|
|
||||||
return res.send(jpeg);
|
return res.send(jpeg);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const status = e?.statusCode || 500;
|
const status = e?.statusCode || 500;
|
||||||
|
|||||||
Reference in New Issue
Block a user