From e36a7081324b9e538ae40918072edd93ebc9b2cb Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 11 Dec 2017 13:33:33 +0900 Subject: #986 --- src/api/common/add-file-to-drive.ts | 39 +++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src/api/common') diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts index 109e886106..427b54d72b 100644 --- a/src/api/common/add-file-to-drive.ts +++ b/src/api/common/add-file-to-drive.ts @@ -110,7 +110,7 @@ const addFile = async ( } } - const [wh, folder] = await Promise.all([ + const [wh, averageColor, folder] = await Promise.all([ // Width and height (when image) (async () => { // 画像かどうか @@ -125,14 +125,45 @@ const addFile = async ( return null; } + log('calculate image width and height...'); + // Calculate width and height const g = gm(fs.createReadStream(path), name); const size = await prominence(g).size(); - log('image width and height is calculated'); + log(`image width and height is calculated: ${size.width}, ${size.height}`); return [size.width, size.height]; })(), + // average color (when image) + (async () => { + // 画像かどうか + if (!/^image\/.*$/.test(mime)) { + return null; + } + + const imageType = mime.split('/')[1]; + + // 画像でもPNGかJPEGでないならスキップ + if (imageType != 'png' && imageType != 'jpeg') { + return null; + } + + log('calculate average color...'); + + const buffer = await prominence(gm(fs.createReadStream(path), name) + .setFormat('ppm') + .resize(1, 1)) // 1pxのサイズに縮小して平均色を取得するというハック + .toBuffer(); + + const r = buffer.readUInt8(buffer.length - 3); + const g = buffer.readUInt8(buffer.length - 2); + const b = buffer.readUInt8(buffer.length - 1); + + log(`average color is calculated: ${r}, ${g}, ${b}`); + + return [r, g, b]; + })(), // folder (async () => { if (!folderId) { @@ -188,6 +219,10 @@ const addFile = async ( properties['height'] = wh[1]; } + if (averageColor) { + properties['average_color'] = averageColor; + } + return addToGridFS(detectedName, readable, mime, { user_id: user._id, folder_id: folder !== null ? folder._id : null, -- cgit v1.2.3-freya