2017-11-06 09:57:03 +01:00
|
|
|
// for Node.js interpret
|
|
|
|
|
|
|
|
const { default: db } = require('../../built/db/mongodb')
|
|
|
|
const { default: DriveFile, getGridFSBucket } = require('../../built/api/models/drive-file')
|
|
|
|
const { Duplex } = require('stream')
|
2017-11-07 14:08:21 +01:00
|
|
|
const { default: zip } = require('@prezzemolo/zip')
|
2017-11-06 09:57:03 +01:00
|
|
|
|
|
|
|
const writeToGridFS = (bucket, buffer, ...rest) => new Promise((resolve, reject) => {
|
|
|
|
const writeStream = bucket.openUploadStreamWithId(...rest)
|
2017-11-07 11:17:42 +01:00
|
|
|
|
2017-11-06 09:57:03 +01:00
|
|
|
const dataStream = new Duplex()
|
|
|
|
dataStream.push(buffer)
|
|
|
|
dataStream.push(null)
|
|
|
|
|
|
|
|
writeStream.once('finish', resolve)
|
|
|
|
writeStream.on('error', reject)
|
|
|
|
|
|
|
|
dataStream.pipe(writeStream)
|
|
|
|
})
|
|
|
|
|
|
|
|
const migrateToGridFS = async (doc) => {
|
|
|
|
const id = doc._id
|
2017-11-07 10:47:23 +01:00
|
|
|
const buffer = doc.data ? doc.data.buffer : Buffer.from([0x00]) // アップロードのバグなのか知らないけどなぜか data が存在しない drive_file ドキュメントがまれにあることがわかったので
|
2017-11-06 09:57:03 +01:00
|
|
|
const created_at = doc.created_at
|
2017-11-07 01:30:51 +01:00
|
|
|
const name = doc.name
|
2017-11-07 13:04:32 +01:00
|
|
|
const type = doc.type
|
2017-11-06 09:57:03 +01:00
|
|
|
|
|
|
|
delete doc._id
|
|
|
|
delete doc.created_at
|
|
|
|
delete doc.datasize
|
|
|
|
delete doc.hash
|
|
|
|
delete doc.data
|
2017-11-07 01:30:51 +01:00
|
|
|
delete doc.name
|
2017-11-07 13:04:32 +01:00
|
|
|
delete doc.type
|
2017-11-06 09:57:03 +01:00
|
|
|
|
|
|
|
const bucket = await getGridFSBucket()
|
2017-11-07 13:04:32 +01:00
|
|
|
const added = await writeToGridFS(bucket, buffer, id, name, { contentType: type, metadata: doc })
|
2017-11-06 09:57:03 +01:00
|
|
|
|
|
|
|
const result = await DriveFile.update(id, {
|
|
|
|
$set: {
|
|
|
|
uploadDate: created_at
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return added && result.ok === 1
|
|
|
|
}
|
|
|
|
|
2017-11-07 11:17:42 +01:00
|
|
|
async function main() {
|
2017-11-07 14:57:48 +01:00
|
|
|
const count = await db.get('drive_files').count({});
|
2017-11-07 14:08:21 +01:00
|
|
|
|
2017-11-07 14:39:17 +01:00
|
|
|
console.log(`there are ${count} files.`)
|
|
|
|
|
2017-11-07 14:08:21 +01:00
|
|
|
const dop = Number.parseInt(process.argv[2]) || 5
|
2017-11-07 14:39:17 +01:00
|
|
|
const idop = ((count - (count % dop)) / dop) + 1
|
2017-11-07 14:08:21 +01:00
|
|
|
|
|
|
|
return zip(
|
|
|
|
1,
|
|
|
|
async (time) => {
|
2017-11-07 14:39:17 +01:00
|
|
|
console.log(`${time} / ${idop}`)
|
|
|
|
const doc = await db.get('drive_files').find({}, { limit: dop, skip: time * dop })
|
2017-11-07 14:08:21 +01:00
|
|
|
return Promise.all(doc.map(migrateToGridFS))
|
|
|
|
},
|
2017-11-07 14:39:17 +01:00
|
|
|
idop
|
|
|
|
).then(a => {
|
|
|
|
const rv = []
|
|
|
|
a.forEach(e => rv.push(...e))
|
|
|
|
return rv
|
|
|
|
})
|
2017-11-06 09:57:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
main().then(console.dir).catch(console.error)
|