yumechi-no-kuni/tools/migration/node.2017-12-11.js

72 lines
1.4 KiB
JavaScript
Raw Normal View History

2017-12-10 22:33:33 -06:00
// 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) => {
2017-12-10 22:52:06 -06:00
if (err) {
console.error(err);
res(false);
return;
}
2017-12-10 22:33:33 -06:00
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)