From 934debd68e5943ccf360a0a11011cb23c540ef99 Mon Sep 17 00:00:00 2001 From: syuilo Date: Tue, 7 Nov 2017 21:04:32 +0900 Subject: [PATCH] #882 --- src/api/common/add-file-to-drive.ts | 7 ++--- src/api/serializers/drive-file.ts | 1 + tools/migration/issue_882.js | 44 +++++++++++++++++++++++++++++ tools/migration/use-gridfs.js | 4 ++- 4 files changed, 51 insertions(+), 5 deletions(-) create mode 100644 tools/migration/issue_882.js diff --git a/src/api/common/add-file-to-drive.ts b/src/api/common/add-file-to-drive.ts index e1baf0819..a96906d29 100644 --- a/src/api/common/add-file-to-drive.ts +++ b/src/api/common/add-file-to-drive.ts @@ -13,13 +13,13 @@ import { Duplex } from 'stream'; const log = debug('misskey:register-drive-file'); -const addToGridFS = (name, binary, metadata): Promise => new Promise(async (resolve, reject) => { +const addToGridFS = (name, binary, type, metadata): Promise => new Promise(async (resolve, reject) => { const dataStream = new Duplex(); dataStream.push(binary); dataStream.push(null); const bucket = await getGridFSBucket(); - const writeStream = bucket.openUploadStream(name, { metadata }); + const writeStream = bucket.openUploadStream(name, { contentType: type, metadata }); writeStream.once('finish', (doc) => { resolve(doc); }); writeStream.on('error', reject); dataStream.pipe(writeStream); @@ -144,10 +144,9 @@ export default ( } // Create DriveFile document - const file = await addToGridFS(name, data, { + const file = await addToGridFS(name, data, mime, { user_id: user._id, folder_id: folder !== null ? folder._id : null, - type: mime, comment: comment, properties: properties }); diff --git a/src/api/serializers/drive-file.ts b/src/api/serializers/drive-file.ts index 57b74cd97..3b76979a4 100644 --- a/src/api/serializers/drive-file.ts +++ b/src/api/serializers/drive-file.ts @@ -48,6 +48,7 @@ export default ( _target.id = _file._id; _target.created_at = _file.uploadDate; _target.name = _file.filename; + _target.type = _file.contentType; _target = Object.assign(_target, _file.metadata); diff --git a/tools/migration/issue_882.js b/tools/migration/issue_882.js new file mode 100644 index 000000000..67a1551e0 --- /dev/null +++ b/tools/migration/issue_882.js @@ -0,0 +1,44 @@ +// for Node.js interpret + +const { default: DriveFile } = require('../../built/api/models/drive-file') + +const migrate = async (doc) => { + const result = await DriveFile.update(doc._id, { + $set: { + contentType: doc.metadata.type + }, + $unset: { + 'metadata.type': '' + } + }) + return result.ok === 1 +} + +async function main() { + let i = 0; + + const count = await db.get('drive_files').count({}); + + const iterate = async () => { + if (i == count) return true; + console.log(`${i} / ${count}`); + const doc = (await db.get('drive_files').find({}, { limit: 1, skip: i }))[0] + const res = await migrate(doc); + if (!res) { + return false; + } else { + i++ + return await iterate(); + } + } + + const res = await iterate(); + + if (res) { + return 'ok'; + } else { + throw 'something happened'; + } +} + +main().then(console.dir).catch(console.error) diff --git a/tools/migration/use-gridfs.js b/tools/migration/use-gridfs.js index c5883e456..106cbd388 100644 --- a/tools/migration/use-gridfs.js +++ b/tools/migration/use-gridfs.js @@ -22,6 +22,7 @@ const migrateToGridFS = async (doc) => { const buffer = doc.data ? doc.data.buffer : Buffer.from([0x00]) // アップロードのバグなのか知らないけどなぜか data が存在しない drive_file ドキュメントがまれにあることがわかったので const created_at = doc.created_at const name = doc.name + const type = doc.type delete doc._id delete doc.created_at @@ -29,9 +30,10 @@ const migrateToGridFS = async (doc) => { delete doc.hash delete doc.data delete doc.name + delete doc.type const bucket = await getGridFSBucket() - const added = await writeToGridFS(bucket, buffer, id, name, { metadata: doc }) + const added = await writeToGridFS(bucket, buffer, id, name, { contentType: type, metadata: doc }) const result = await DriveFile.update(id, { $set: {