fix custom emoji
This commit is contained in:
@@ -17,59 +17,17 @@ module.exports = async function (context) {
|
|||||||
while (await cursor.hasNext()) {
|
while (await cursor.hasNext()) {
|
||||||
const result = await cursor.next()
|
const result = await cursor.next()
|
||||||
|
|
||||||
// Prepare destination file
|
|
||||||
const filename = `${result.name}.${result.extension}`
|
const filename = `${result.name}.${result.extension}`
|
||||||
const dest = `${context.config.target.filesPath}/custom_emoji/${filename}`
|
const dest = `${context.config.target.filesPath}/custom_emoji/${filename}`
|
||||||
// Download emoji file
|
|
||||||
if (result.store.startsWith('FileSystem:')) {
|
const srcFilename = Utils.srcPath(context.config.source.customEmojiPath, filename)
|
||||||
const src = context.config.source.customEmojiPath
|
if (srcFilename) {
|
||||||
const srcFilename = Utils.srcPath(src, filename)
|
|
||||||
if (!srcFilename) {
|
|
||||||
return new Error(`source file "${filename}" not found`)
|
|
||||||
}
|
|
||||||
// Copy to output dir
|
|
||||||
Utils.copyFile(srcFilename, dest)
|
Utils.copyFile(srcFilename, dest)
|
||||||
} else if (result.store.startsWith('GridFS:')) {
|
|
||||||
await Utils.downloadGridFS(context, fileCollection, filename, dest)
|
|
||||||
} else {
|
} 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
|
log.info(`... copied ${result.name}`)
|
||||||
let emoji = {
|
|
||||||
name: result.name,
|
|
||||||
image: dest,
|
|
||||||
}
|
|
||||||
log.info(`... writing ${emoji.name}`)
|
|
||||||
context.output.write(
|
|
||||||
Factory.emoji(emoji)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
return context
|
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
|
|
||||||
}
|
|
||||||
@@ -96,6 +96,9 @@ utils.processFileAttachment = async function (context, message, attachment) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
utils.srcPath = function (srcDir, filename) {
|
utils.srcPath = function (srcDir, filename) {
|
||||||
|
if (!srcDir || !fs.existsSync(srcDir)) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
const files = fs.readdirSync(srcDir).filter((fn) => fn.startsWith(filename));
|
const files = fs.readdirSync(srcDir).filter((fn) => fn.startsWith(filename));
|
||||||
if (files.length !== 1) {
|
if (files.length !== 1) {
|
||||||
return false
|
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) {
|
utils.downloadGridFS = async function (context, collection, id, dest) {
|
||||||
if (!fs.existsSync(path.dirname(dest))) {
|
if (!fs.existsSync(path.dirname(dest))) {
|
||||||
fs.mkdirSync(path.dirname(dest), { recursive: true })
|
fs.mkdirSync(path.dirname(dest), { recursive: true })
|
||||||
|
|||||||
Reference in New Issue
Block a user