summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-09-22 16:58:16 +0200
committerJules Laplace <julescarbon@gmail.com>2018-09-22 16:58:16 +0200
commit0abd557635d4fb3bde6cd4ab7ead659a9a85b3c8 (patch)
treee5f213b1ea4ffc67c80d059964bc2dc59c2edb0a
parent8d1c19852fcce52e369978dfc195d5da4f12180a (diff)
thumbnail method, uses sharp
-rw-r--r--app/relay/runner.js37
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([])