diff options
Diffstat (limited to 'app/relay/runner.js')
| -rw-r--r-- | app/relay/runner.js | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/app/relay/runner.js b/app/relay/runner.js index fa86d59..1e90fca 100644 --- a/app/relay/runner.js +++ b/app/relay/runner.js @@ -8,10 +8,15 @@ import uuidv1 from 'uuid/v1' import * as fs from 'fs' import * as path from 'path' import readdir from 'fs-readdir-promise' +import sharp from 'sharp' import * as q from './queue' -const MAX_TRANSFER_SIZE = 1024 * 1024 * 2.5 +const MEGABYTES = 1024 * 1024 +const MAX_TRANSFER_SIZE = 2.5 * MEGABYTES +const MAX_LOAD_SIZE = 108 * MEGABYTES +const THUMBNAIL_SIZE = 200 +const THUMBNAIL_QUALITY = 80 const idle_state = { status: 'IDLE', task: {} } export const state = { @@ -188,6 +193,36 @@ export function read_file(opt, cb) { }).catch(() => cb({ error: 'error reading file' })) } +export function thumbnail(opt, cb) { + const fn = module_dir(opt, opt.fn) + if (!fn) return cb([]) + stat_promise(fn).then(stat => { + if (stat.size > MAX_LOAD_SIZE) { + return cb({ error: 'file too large'}) + } + sharp(fn) + .resize(opt.size || THUMBNAIL_SIZE) + .jpeg({ quality: opt.quality || THUMBNAIL_QUALITY }) + .toBuffer() + .then(buf => cb({ + error: err, + name: opt.fn, + path: fn, + date: stat.ctime, + size: stat.size, + buf + })) + .catch(err => cb({ + error: err, + name: opt.fn, + path: fn, + date: stat.ctime, + size: stat.size, + buf: null, + })) + }) +} + export function list_directory(opt, cb) { const dir = module_dir(opt, opt.dir) if (!dir) return cb([]) |
