This commit is contained in:
syuilo 2017-11-07 21:04:32 +09:00
parent 823a3d765e
commit 934debd68e
4 changed files with 51 additions and 5 deletions

View File

@ -13,13 +13,13 @@ import { Duplex } from 'stream';
const log = debug('misskey:register-drive-file'); const log = debug('misskey:register-drive-file');
const addToGridFS = (name, binary, metadata): Promise<any> => new Promise(async (resolve, reject) => { const addToGridFS = (name, binary, type, metadata): Promise<any> => new Promise(async (resolve, reject) => {
const dataStream = new Duplex(); const dataStream = new Duplex();
dataStream.push(binary); dataStream.push(binary);
dataStream.push(null); dataStream.push(null);
const bucket = await getGridFSBucket(); 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.once('finish', (doc) => { resolve(doc); });
writeStream.on('error', reject); writeStream.on('error', reject);
dataStream.pipe(writeStream); dataStream.pipe(writeStream);
@ -144,10 +144,9 @@ export default (
} }
// Create DriveFile document // Create DriveFile document
const file = await addToGridFS(name, data, { const file = await addToGridFS(name, data, mime, {
user_id: user._id, user_id: user._id,
folder_id: folder !== null ? folder._id : null, folder_id: folder !== null ? folder._id : null,
type: mime,
comment: comment, comment: comment,
properties: properties properties: properties
}); });

View File

@ -48,6 +48,7 @@ export default (
_target.id = _file._id; _target.id = _file._id;
_target.created_at = _file.uploadDate; _target.created_at = _file.uploadDate;
_target.name = _file.filename; _target.name = _file.filename;
_target.type = _file.contentType;
_target = Object.assign(_target, _file.metadata); _target = Object.assign(_target, _file.metadata);

View File

@ -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)

View File

@ -22,6 +22,7 @@ const migrateToGridFS = async (doc) => {
const buffer = doc.data ? doc.data.buffer : Buffer.from([0x00]) // アップロードのバグなのか知らないけどなぜか data が存在しない drive_file ドキュメントがまれにあることがわかったので const buffer = doc.data ? doc.data.buffer : Buffer.from([0x00]) // アップロードのバグなのか知らないけどなぜか data が存在しない drive_file ドキュメントがまれにあることがわかったので
const created_at = doc.created_at const created_at = doc.created_at
const name = doc.name const name = doc.name
const type = doc.type
delete doc._id delete doc._id
delete doc.created_at delete doc.created_at
@ -29,9 +30,10 @@ const migrateToGridFS = async (doc) => {
delete doc.hash delete doc.hash
delete doc.data delete doc.data
delete doc.name delete doc.name
delete doc.type
const bucket = await getGridFSBucket() 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, { const result = await DriveFile.update(id, {
$set: { $set: {