remove exif data from uploaded jpegs
This commit is contained in:
parent
680a894efd
commit
6d50e53604
|
@ -37,6 +37,7 @@
|
||||||
"lodash": "^4.17.11",
|
"lodash": "^4.17.11",
|
||||||
"markdown-it": "^10.0.0",
|
"markdown-it": "^10.0.0",
|
||||||
"open": "^7.0.3",
|
"open": "^7.0.3",
|
||||||
|
"piexifjs": "^1.0.4",
|
||||||
"pretty-ms": "^6.0.0",
|
"pretty-ms": "^6.0.0",
|
||||||
"pull-paramap": "^1.2.2",
|
"pull-paramap": "^1.2.2",
|
||||||
"pull-sort": "^1.0.2",
|
"pull-sort": "^1.0.2",
|
||||||
|
|
28
src/index.js
28
src/index.js
|
@ -7,6 +7,7 @@ const path = require("path");
|
||||||
const envPaths = require("env-paths");
|
const envPaths = require("env-paths");
|
||||||
const cli = require("./cli");
|
const cli = require("./cli");
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
|
const exif = require("piexifjs");
|
||||||
|
|
||||||
const defaultConfig = {};
|
const defaultConfig = {};
|
||||||
const defaultConfigFile = path.join(
|
const defaultConfigFile = path.join(
|
||||||
|
@ -151,7 +152,7 @@ const handleBlobUpload = async function (ctx) {
|
||||||
const ssb = await cooler.open();
|
const ssb = await cooler.open();
|
||||||
const blobUpload = ctx.request.files.blob;
|
const blobUpload = ctx.request.files.blob;
|
||||||
if (typeof blobUpload !== "undefined") {
|
if (typeof blobUpload !== "undefined") {
|
||||||
const data = await fs.promises.readFile(blobUpload.path);
|
let data = await fs.promises.readFile(blobUpload.path);
|
||||||
if (data.length > 0) {
|
if (data.length > 0) {
|
||||||
// 5 MiB check
|
// 5 MiB check
|
||||||
const mebibyte = Math.pow(2, 20);
|
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");
|
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) => {
|
const addBlob = new Promise((resolve, reject) => {
|
||||||
pull(
|
pull(
|
||||||
pull.values([data]),
|
pull.values([data]),
|
||||||
|
|
Loading…
Reference in New Issue