From e36a7081324b9e538ae40918072edd93ebc9b2cb Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 11 Dec 2017 13:33:33 +0900 Subject: [PATCH] #986 --- src/api/common/add-file-to-drive.ts | 39 ++++++++++- src/api/endpoints/drive/files/create.ts | 14 ++-- src/web/app/desktop/tags/drive/file.tag | 16 ++++- src/web/app/desktop/tags/images.tag | 13 +++- src/web/app/mobile/tags/drive/file-viewer.tag | 7 +- src/web/app/mobile/tags/drive/file.tag | 6 +- src/web/app/mobile/tags/images.tag | 6 +- tools/migration/node.2017-12-11.js | 67 +++++++++++++++++++ 8 files changed, 157 insertions(+), 11 deletions(-) create mode 100644 tools/migration/node.2017-12-11.js diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts index 109e88610..427b54d72 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, diff --git a/src/api/endpoints/drive/files/create.ts b/src/api/endpoints/drive/files/create.ts index 7546eca30..437348a1e 100644 --- a/src/api/endpoints/drive/files/create.ts +++ b/src/api/endpoints/drive/files/create.ts @@ -38,9 +38,15 @@ module.exports = async (file, params, user): Promise => { const [folderId = null, folderIdErr] = $(params.folder_id).optional.nullable.id().$; if (folderIdErr) throw 'invalid folder_id param'; - // Create file - const driveFile = await create(user, file.path, name, null, folderId); + try { + // Create file + const driveFile = await create(user, file.path, name, null, folderId); - // Serialize - return serialize(driveFile); + // Serialize + return serialize(driveFile); + } catch (e) { + console.error(e); + + throw e; + } }; diff --git a/src/web/app/desktop/tags/drive/file.tag b/src/web/app/desktop/tags/drive/file.tag index 0f019d95b..8b3d36b3f 100644 --- a/src/web/app/desktop/tags/drive/file.tag +++ b/src/web/app/desktop/tags/drive/file.tag @@ -5,7 +5,9 @@

%i18n:desktop.tags.mk-drive-browser-file.banner%

-
+
+ +

{ file.name.lastIndexOf('.') != -1 ? file.name.substr(0, file.name.lastIndexOf('.')) : file.name }{ file.name.substr(file.name.lastIndexOf('.')) }

diff --git a/src/web/app/desktop/tags/images.tag b/src/web/app/desktop/tags/images.tag index ce67d26a9..5e4be481d 100644 --- a/src/web/app/desktop/tags/images.tag +++ b/src/web/app/desktop/tags/images.tag @@ -53,7 +53,13 @@ - +