summaryrefslogtreecommitdiff
path: root/tools/migration
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 /tools/migration
parentFix bug (diff)
downloadsharkey-e36a7081324b9e538ae40918072edd93ebc9b2cb.tar.gz
sharkey-e36a7081324b9e538ae40918072edd93ebc9b2cb.tar.bz2
sharkey-e36a7081324b9e538ae40918072edd93ebc9b2cb.zip
#986
Diffstat (limited to 'tools/migration')
-rw-r--r--tools/migration/node.2017-12-11.js67
1 files changed, 67 insertions, 0 deletions
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)