2017-11-06 02:57:03 -06: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')
|
|
|
|
|
|
|
|
const writeToGridFS = (bucket, buffer, ...rest) => new Promise((resolve, reject) => {
|
|
|
|
const writeStream = bucket.openUploadStreamWithId(...rest)
|
|
|
|
|
|
|
|
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
|
|
|
|
const buffer = doc.data.buffer
|
|
|
|
const created_at = doc.created_at
|
2017-11-06 18:30:51 -06:00
|
|
|
const name = doc.name
|
2017-11-06 02:57:03 -06:00
|
|
|
|
|
|
|
delete doc._id
|
|
|
|
delete doc.created_at
|
|
|
|
delete doc.datasize
|
|
|
|
delete doc.hash
|
|
|
|
delete doc.data
|
2017-11-06 18:30:51 -06:00
|
|
|
delete doc.name
|
2017-11-06 02:57:03 -06:00
|
|
|
|
|
|
|
const bucket = await getGridFSBucket()
|
2017-11-06 18:30:51 -06:00
|
|
|
const added = await writeToGridFS(bucket, buffer, id, name, { metadata: doc })
|
2017-11-06 02:57:03 -06:00
|
|
|
|
|
|
|
const result = await DriveFile.update(id, {
|
|
|
|
$set: {
|
|
|
|
uploadDate: created_at
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return added && result.ok === 1
|
|
|
|
}
|
|
|
|
|
|
|
|
const main = async () => {
|
|
|
|
const docs = await db.get('drive_files').find()
|
|
|
|
const all = await Promise.all(docs.map(migrateToGridFS))
|
|
|
|
return all
|
|
|
|
}
|
|
|
|
|
|
|
|
main().then(console.dir).catch(console.error)
|