remove exif data from uploaded jpegs

This commit is contained in:
Alexander Cobleigh 2020-10-14 14:38:00 +02:00 committed by Henry
parent 680a894efd
commit 6d50e53604
2 changed files with 28 additions and 1 deletions

View File

@ -37,6 +37,7 @@
"lodash": "^4.17.11",
"markdown-it": "^10.0.0",
"open": "^7.0.3",
"piexifjs": "^1.0.4",
"pretty-ms": "^6.0.0",
"pull-paramap": "^1.2.2",
"pull-sort": "^1.0.2",

View File

@ -7,6 +7,7 @@ const path = require("path");
const envPaths = require("env-paths");
const cli = require("./cli");
const fs = require("fs");
const exif = require("piexifjs");
const defaultConfig = {};
const defaultConfigFile = path.join(
@ -151,7 +152,7 @@ const handleBlobUpload = async function (ctx) {
const ssb = await cooler.open();
const blobUpload = ctx.request.files.blob;
if (typeof blobUpload !== "undefined") {
const data = await fs.promises.readFile(blobUpload.path);
let data = await fs.promises.readFile(blobUpload.path);
if (data.length > 0) {
// 5 MiB check
const mebibyte = Math.pow(2, 20);
@ -160,6 +161,31 @@ const handleBlobUpload = async function (ctx) {
throw new Error("Blob file is too big, maximum size is 5 mebibytes");
}
try {
const dataString = data.toString("binary");
// implementation borrowed from ssb-blob-files
// (which operates on a slightly different data structure, sadly)
// https://github.com/ssbc/ssb-blob-files/blob/master/async/image-process.js
data = Buffer.from(removeExif(dataString), "binary")
function removeExif (fileData) {
const exifOrientation = exif.load(fileData);
const orientation = exifOrientation['0th'][exif.ImageIFD.Orientation];
const clean = exif.remove(fileData);
if (orientation !== undefined) { // preserve img orientation
const exifData = { '0th': {} }
exifData['0th'][exif.ImageIFD.Orientation] = orientation;
const exifStr = exif.dump(exifData);
return exif.insert(exifStr, clean);
} else {
return clean;
}
}
} catch (e) {
console.warn(e)
console.warn("blob was likely not a jpeg -- no exif data to remove. proceeding with blob upload");
}
const addBlob = new Promise((resolve, reject) => {
pull(
pull.values([data]),