summaryrefslogtreecommitdiff
path: root/src/api/common
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-12-11 13:33:33 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-12-11 13:33:33 +0900
commite36a7081324b9e538ae40918072edd93ebc9b2cb (patch)
tree98983f6f3d37f55627f2fedcb6ebb7cde9b902a6 /src/api/common
parentFix bug (diff)
downloadsharkey-e36a7081324b9e538ae40918072edd93ebc9b2cb.tar.gz
sharkey-e36a7081324b9e538ae40918072edd93ebc9b2cb.tar.bz2
sharkey-e36a7081324b9e538ae40918072edd93ebc9b2cb.zip
#986
Diffstat (limited to 'src/api/common')
-rw-r--r--src/api/common/add-file-to-drive.ts39
1 files changed, 37 insertions, 2 deletions
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,