From 303ccaa2f7bc6bdb52feef8c5f405f5b8fdda004 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 8 Dec 2017 19:42:02 +0900 Subject: Refactor --- src/api/common/add-file-to-drive.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 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 2a649788af..dea02eeca6 100644 --- a/src/api/common/add-file-to-drive.ts +++ b/src/api/common/add-file-to-drive.ts @@ -106,8 +106,8 @@ const addFile = async ( } } - const [properties, folder] = await Promise.all([ - // properties + const [wh, folder] = await Promise.all([ + // Width and height (when image) (async () => { // 画像かどうか if (!/^image\/.*$/.test(mime)) { @@ -116,22 +116,18 @@ const addFile = async ( const imageType = mime.split('/')[1]; - // 画像でもPNGかJPEGでないならスキップ - if (imageType != 'png' && imageType != 'jpeg') { + // 画像でもPNGかJPEGかGIFでないならスキップ + if (imageType != 'png' && imageType != 'jpeg' && imageType != 'gif') { return null; } - // If the file is an image, calculate width and height to save in property + // Calculate width and height const g = gm(fs.createReadStream(path), name); const size = await prominence(g).size(); - const properties = { - width: size.width, - height: size.height - }; log('image width and height is calculated'); - return properties; + return [size.width, size.height]; })(), // folder (async () => { @@ -181,6 +177,13 @@ const addFile = async ( const readable = fs.createReadStream(path); + const properties = {}; + + if (wh) { + properties['width'] = wh[0]; + properties['height'] = wh[1]; + } + return addToGridFS(detectedName, readable, mime, { user_id: user._id, folder_id: folder !== null ? folder._id : null, -- cgit v1.2.3-freya From 37edacce44cb7d63c35fee88e0b43028a1c7765c Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 9 Dec 2017 22:35:26 +0900 Subject: Use ImageMagick instead of GraphicsMagick --- docs/setup.en.md | 2 +- docs/setup.ja.md | 2 +- src/api/common/add-file-to-drive.ts | 6 +++++- src/file/server.ts | 7 ++++++- src/utils/dependencyInfo.ts | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) (limited to 'src/api/common') diff --git a/docs/setup.en.md b/docs/setup.en.md index 9c31e4f177..b81245d892 100644 --- a/docs/setup.en.md +++ b/docs/setup.en.md @@ -53,7 +53,7 @@ Please install and setup these softwares: * *Node.js* and *npm* * **[MongoDB](https://www.mongodb.com/)** * **[Redis](https://redis.io/)** -* **[GraphicsMagick](http://www.graphicsmagick.org/)** +* **[ImageMagick](http://www.imagemagick.org/script/index.php)** ##### Optional * [Elasticsearch](https://www.elastic.co/) - used to provide searching feature instead of MongoDB diff --git a/docs/setup.ja.md b/docs/setup.ja.md index 1e8bb553fa..1662d1ee5a 100644 --- a/docs/setup.ja.md +++ b/docs/setup.ja.md @@ -54,7 +54,7 @@ web-push generate-vapid-keys * *Node.js* と *npm* * **[MongoDB](https://www.mongodb.com/)** * **[Redis](https://redis.io/)** -* **[GraphicsMagick](http://www.graphicsmagick.org/)** +* **[ImageMagick](http://www.imagemagick.org/script/index.php)** ##### オプション * [Elasticsearch](https://www.elastic.co/) - 検索機能を向上させるために用います。 diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts index dea02eeca6..109e886106 100644 --- a/src/api/common/add-file-to-drive.ts +++ b/src/api/common/add-file-to-drive.ts @@ -5,7 +5,7 @@ import * as stream from 'stream'; import * as mongodb from 'mongodb'; import * as crypto from 'crypto'; -import * as gm from 'gm'; +import * as _gm from 'gm'; import * as debug from 'debug'; import fileType = require('file-type'); import prominence = require('prominence'); @@ -16,6 +16,10 @@ import serialize from '../serializers/drive-file'; import event, { publishDriveStream } from '../event'; import config from '../../conf'; +const gm = _gm.subClass({ + imageMagick: true +}); + const log = debug('misskey:register-drive-file'); const tmpFile = (): Promise => new Promise((resolve, reject) => { diff --git a/src/file/server.ts b/src/file/server.ts index 1f8d21b80d..187ce75c20 100644 --- a/src/file/server.ts +++ b/src/file/server.ts @@ -7,11 +7,15 @@ import * as express from 'express'; import * as bodyParser from 'body-parser'; import * as cors from 'cors'; import * as mongodb from 'mongodb'; -import * as gm from 'gm'; +import * as _gm from 'gm'; import * as stream from 'stream'; import DriveFile, { getGridFSBucket } from '../api/models/drive-file'; +const gm = _gm.subClass({ + imageMagick: true +}); + /** * Init app */ @@ -78,6 +82,7 @@ function thumbnail(data: stream.Readable, type: string, resize: number): ISend { const stream = g .compress('jpeg') .quality(80) + .noProfile() // Remove EXIF .stream(); return { diff --git a/src/utils/dependencyInfo.ts b/src/utils/dependencyInfo.ts index 818fa3136c..89af0d20fb 100644 --- a/src/utils/dependencyInfo.ts +++ b/src/utils/dependencyInfo.ts @@ -11,7 +11,7 @@ export default class { public showAll(): void { this.show('MongoDB', 'mongo --version', x => x.match(/^MongoDB shell version:? (.*)\r?\n/)); this.show('Redis', 'redis-server --version', x => x.match(/v=([0-9\.]*)/)); - this.show('GraphicsMagick', 'gm -version', x => x.match(/^GraphicsMagick ([0-9\.]*) .*/)); + this.show('ImageMagick', 'magick -version', x => x.match(/^Version: ImageMagick (.+?)\r?\n/)); } public show(serviceName: string, command: string, transform: (x: string) => RegExpMatchArray): void { -- cgit v1.2.3-freya 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 +++++++++++++++- 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 (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, diff --git a/src/api/endpoints/drive/files/create.ts b/src/api/endpoints/drive/files/create.ts index 7546eca309..437348a1ef 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 0f019d95bf..8b3d36b3f3 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 ce67d26a9f..5e4be481dc 100644 --- a/src/web/app/desktop/tags/images.tag +++ b/src/web/app/desktop/tags/images.tag @@ -53,7 +53,13 @@ - +