From e36a7081324b9e538ae40918072edd93ebc9b2cb Mon Sep 17 00:00:00 2001 From: syuilo Date: Mon, 11 Dec 2017 13:33:33 +0900 Subject: #986 --- tools/migration/node.2017-12-11.js | 67 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 tools/migration/node.2017-12-11.js (limited to 'tools/migration') diff --git a/tools/migration/node.2017-12-11.js b/tools/migration/node.2017-12-11.js new file mode 100644 index 0000000000..3a3fef0518 --- /dev/null +++ b/tools/migration/node.2017-12-11.js @@ -0,0 +1,67 @@ +// for Node.js interpret + +const { default: DriveFile, getGridFSBucket } = require('../../built/api/models/drive-file') +const { default: zip } = require('@prezzemolo/zip') + +const _gm = require('gm'); +const gm = _gm.subClass({ + imageMagick: true +}); + +const migrate = doc => new Promise(async (res, rej) => { + const bucket = await getGridFSBucket(); + + const readable = bucket.openDownloadStream(doc._id); + + gm(readable) + .setFormat('ppm') + .resize(1, 1) + .toBuffer(async (err, buffer) => { + if (err) rej(err); + const r = buffer.readUInt8(buffer.length - 3); + const g = buffer.readUInt8(buffer.length - 2); + const b = buffer.readUInt8(buffer.length - 1); + + const result = await DriveFile.update(doc._id, { + $set: { + 'metadata.properties.average_color': [r, g, b] + } + }) + + res(result.ok === 1); + }); +}); + +async function main() { + const query = { + contentType: { + $in: [ + 'image/png', + 'image/jpeg' + ] + } + } + + const count = await DriveFile.count(query); + + const dop = Number.parseInt(process.argv[2]) || 5 + const idop = ((count - (count % dop)) / dop) + 1 + + return zip( + 1, + async (time) => { + console.log(`${time} / ${idop}`) + const doc = await DriveFile.find(query, { + limit: dop, skip: time * dop + }) + return Promise.all(doc.map(migrate)) + }, + idop + ).then(a => { + const rv = [] + a.forEach(e => rv.push(...e)) + return rv + }) +} + +main().then(console.dir).catch(console.error) -- cgit v1.2.3-freya