feat: Strip EXIF GPS data from files during upload

Supported filetypes are: JPG, PNG, TIF, MOV, MP4. Using [@xoi/gps-metadata-remover](https://www.npmjs.com/package/@xoi/gps-metadata-remover).

Closes #91
pull/160/head
tycrek 2 years ago
parent 9a0b4078a8
commit f36acef1cc
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

1201
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -45,6 +45,7 @@
"@tycrek/isprod": "^2.0.2",
"@tycrek/log": "^0.6.0-7",
"@tycrek/papito": "^0.3.4",
"@xoi/gps-metadata-remover": "^1.1.1",
"any-shell-escape": "^0.1.1",
"autoprefixer": "^10.4.4",
"aws-sdk": "^2.1115.0",

@ -0,0 +1,22 @@
/**
* This strips GPS EXIF data from files
*/
import { removeLocation } from '@xoi/gps-metadata-remover';
import fs from 'fs-extra';
export const removeGPS = (file: string): Promise<boolean> => {
return new Promise((resolve, reject) =>
fs.open(file, 'r+')
.then((fd) => removeLocation(file,
// Read function
(size: number, offset: number): Promise<Buffer> =>
fs.read(fd, Buffer.alloc(size), 0, size, offset)
.then(({ buffer }) => Promise.resolve(buffer)),
// Write function
(val: string, offset: number, enc: any): Promise<void> =>
fs.write(fd, Buffer.alloc(val.length, val, enc), 0, val.length, offset)
.then(() => Promise.resolve())))
.then(resolve)
.catch(reject));
}

@ -11,6 +11,7 @@ import Hash from './hash';
import { path, generateId, log } from './utils';
import { SkynetUpload } from './skynet';
import { Request, Response } from 'express';
import { removeGPS } from './nightmare';
const { s3enabled, s3endpoint, s3bucket, s3usePathStyle, s3accessKey, s3secretKey, diskFilePath, saveAsOriginal, saveWithDate, mediaStrict, maxUploadSize, useSia }: Config = fs.readJsonSync(path('config.json'));
const { CODE_UNSUPPORTED_MEDIA_TYPE }: MagicNumbers = fs.readJsonSync(path('MagicNumbers.json'));
@ -108,6 +109,11 @@ export function processUploaded(req: Request, res: Response, next: Function) { /
// Check if file size is too big
.then(() => { if (req.file.size / Math.pow(1024, 2) > maxUploadSize) throw new Error('LIMIT_FILE_SIZE'); })
// Strip EXIF data
.then(() => removeGPS(req.file.path))
.then((result) => log.debug('EXIF GPS data', result ? 'removed' : 'not removed'))
.catch((err) => log.debug('!! EXIF GPS data could not be removed', err))
// Save file
.then(() => log.debug('Saving file', req.file.originalname, s3enabled ? 'in S3' : useSia ? 'on Sia blockchain' : 'on disk'))
.then(() =>

@ -0,0 +1,9 @@
type ReadFunction = (size: number, offset: number) => Promise<Buffer>
type WriteFunction = (val: string, offset: number, enc: any) => Promise<void>
type Options = {
skipXMPRemoval?: boolean
}
declare module '@xoi/gps-metadata-remover' {
export async function removeLocation(path: string, read: ReadFunction, write: WriteFunction, options: Options = {}): Promise<boolean>
}
Loading…
Cancel
Save