diff options
author | Romulus Urakagi Tsai <urakagi@gmail.com> | 2020-05-05 14:08:36 +0800 |
---|---|---|
committer | Romulus Urakagi Tsai <urakagi@gmail.com> | 2020-05-05 14:08:36 +0800 |
commit | 0a1bfd37b3b1b2f4108a734c0449c1bd5a1b691f (patch) | |
tree | b1c21bbad460aca64808dd5ede107e4d32c5bb3f /server/migrations.js | |
parent | 5899b9366c23f31149e9de8ed006a7e30b4830d5 (diff) | |
download | wekan-0a1bfd37b3b1b2f4108a734c0449c1bd5a1b691f.tar.gz wekan-0a1bfd37b3b1b2f4108a734c0449c1bd5a1b691f.tar.bz2 wekan-0a1bfd37b3b1b2f4108a734c0449c1bd5a1b691f.zip |
Migrating attachments
Diffstat (limited to 'server/migrations.js')
-rw-r--r-- | server/migrations.js | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/server/migrations.js b/server/migrations.js index 3861f227..33f061f5 100644 --- a/server/migrations.js +++ b/server/migrations.js @@ -1039,9 +1039,41 @@ Migrations.add('fix-incorrect-dates', () => { console.log('cas', cas); }); +import { MongoInternals } from 'meteor/mongo'; + Migrations.add('change-attachment-library', () => { - console.log('migration called here'); - Migrations.rollback('change-attachment-library'); - console.log('migration rollbacked'); + const http = require('http'); + const fs = require('fs'); + CFSAttachments.find().forEach(file => { + const bucket = new MongoInternals.NpmModule.GridFSBucket(MongoInternals.defaultRemoteCollectionDriver().mongo.db, {bucketName: 'cfs_gridfs.attachments'}); + const gfsId = new MongoInternals.NpmModule.ObjectID(file.copies.attachments.key); + const reader = bucket.openDownloadStream(gfsId); + const path = `/var/attachments/${file.name()}`; + const fd = fs.createWriteStream(path); + reader.pipe(fd); + let opts = { + fileName: file.name(), + type: file.type(), + fileId: file._id, + meta: { + userId: file.userId, + boardId: file.boardId, + cardId: file.cardId + } + }; + if (file.listId) { + opts.meta.listId = file.listId; + } + if (file.swimlaneId) { + opts.meta.swimlaneId = file.swimlaneId; + } + Attachments.addFile(path, opts, (err, fileRef) => { + if (err) { + console.log('error when migrating ', fileName, err); + } else { + file.remove(); + } + }); + }); }); |