diff --git a/lib/rocketchat/emoji.js b/lib/rocketchat/emoji.js index 8cded58..fea96b0 100644 --- a/lib/rocketchat/emoji.js +++ b/lib/rocketchat/emoji.js @@ -17,59 +17,17 @@ module.exports = async function (context) { while (await cursor.hasNext()) { const result = await cursor.next() - // Prepare destination file const filename = `${result.name}.${result.extension}` const dest = `${context.config.target.filesPath}/custom_emoji/${filename}` - // Download emoji file - if (result.store.startsWith('FileSystem:')) { - const src = context.config.source.customEmojiPath - const srcFilename = Utils.srcPath(src, filename) - if (!srcFilename) { - return new Error(`source file "${filename}" not found`) - } - // Copy to output dir + + const srcFilename = Utils.srcPath(context.config.source.customEmojiPath, filename) + if (srcFilename) { Utils.copyFile(srcFilename, dest) - } else if (result.store.startsWith('GridFS:')) { - await Utils.downloadGridFS(context, fileCollection, filename, dest) } else { - throw new Error(`file system ${file.store} is not supported. Migrate to FileSystem first, see readme.`) + await Utils.downloadGridFSByName(context, fileCollection, filename, dest) } - // Export custom emoji - let emoji = { - name: result.name, - image: dest, - } - log.info(`... writing ${emoji.name}`) - context.output.write( - Factory.emoji(emoji) - ) + log.info(`... copied ${result.name}`) } return context } - -const exportFile = async function (context, collection, fileId) { - const type = collection.collectionName - const file = await collection.findOne({ _id: fileId }) - if (!file) { - throw new Error(`file ${fileId} from collection ${type} is missing`) - } - const dest = context.config.target.filesPath - const destFilename = utils.destPath(dest, collection.collectionName, file) - - if (file.store.startsWith('FileSystem:')) { - const src = context.config.source.uploadsPath - const srcFilename = utils.srcPath(src, file._id) - if (!srcFilename) { - return new Error(`source file "${file._id}" not found`) - } - // Copy to output dir - utils.copyFile(srcFilename, destFilename) - } else if (file.store.startsWith('GridFS:')) { - await utils.downloadGridFS(context, collection, file._id, destFilename) - } else { - throw new Error(`file system ${file.store} is not supported. Migrate to FileSystem first, see readme.`) - } - - return destFilename -} \ No newline at end of file diff --git a/lib/rocketchat/utils.js b/lib/rocketchat/utils.js index 3d2a181..beff8e3 100644 --- a/lib/rocketchat/utils.js +++ b/lib/rocketchat/utils.js @@ -96,6 +96,9 @@ utils.processFileAttachment = async function (context, message, attachment) { } utils.srcPath = function (srcDir, filename) { + if (!srcDir || !fs.existsSync(srcDir)) { + return false + } const files = fs.readdirSync(srcDir).filter((fn) => fn.startsWith(filename)); if (files.length !== 1) { return false @@ -151,6 +154,23 @@ utils.copyFile = function (src, dest) { } } +utils.downloadGridFSByName = async function (context, collection, filename, dest) { + if (!fs.existsSync(path.dirname(dest))) { + fs.mkdirSync(path.dirname(dest), { recursive: true }) + } + if (fs.existsSync(dest)) { + fs.unlinkSync(dest) + } + + const bucket = context.rocketchat.gridFsBucket(collection.collectionName) + const destStream = fs.createWriteStream(dest) + bucket.openDownloadStreamByName(filename).pipe(destStream) + return new Promise((resolve, reject) => { + destStream.on('finish', resolve) + destStream.on('error', reject) + }) +} + utils.downloadGridFS = async function (context, collection, id, dest) { if (!fs.existsSync(path.dirname(dest))) { fs.mkdirSync(path.dirname(dest), { recursive: true })