From 40cfe2f5a5e09bbe2ee9c1bc17fa16f0d822b97b Mon Sep 17 00:00:00 2001 From: Rodrigo Nascimento Date: Sat, 10 Sep 2016 22:04:58 -0300 Subject: [PATCH] Add method to get files in sync way --- packages/rocketchat-file/file.server.coffee | 37 +++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/packages/rocketchat-file/file.server.coffee b/packages/rocketchat-file/file.server.coffee index b974aff0ba4..75e80a2c0dd 100644 --- a/packages/rocketchat-file/file.server.coffee +++ b/packages/rocketchat-file/file.server.coffee @@ -91,6 +91,8 @@ RocketChatFile.GridFS = class this.findOneSync = Meteor.wrapAsync this.store.collection(this.name).findOne.bind this.store.collection(this.name) this.removeSync = Meteor.wrapAsync this.store.remove.bind this.store + this.getFileSync = Meteor.wrapAsync this.getFile.bind this + findOne: (fileName) -> return this.findOneSync {_id: fileName} @@ -143,6 +145,23 @@ RocketChatFile.GridFS = class uploadDate: file.uploadDate } + getFile: (fileName, cb) -> + file = this.getFileWithReadStream(fileName) + + if not file + return cb() + + data = [] + file.readStream.on 'data', Meteor.bindEnvironment (chunk) -> + data.push chunk + + file.readStream.on 'end', Meteor.bindEnvironment -> + cb null, + buffer: Buffer.concat(data) + contentType: file.contentType + length: file.length + uploadDate: file.uploadDate + deleteFile: (fileName) -> file = this.findOne fileName if not file? @@ -171,6 +190,8 @@ RocketChatFile.FileSystem = class this.statSync = Meteor.wrapAsync fs.stat.bind fs this.unlinkSync = Meteor.wrapAsync fs.unlink.bind fs + this.getFileSync = Meteor.wrapAsync this.getFile.bind this + createWriteStream: (fileName, contentType) -> self = this @@ -211,6 +232,22 @@ RocketChatFile.FileSystem = class catch e return undefined + getFile: (fileName, cb) -> + file = this.getFileWithReadStream(fileName) + + if not file + return cb() + + data = [] + file.readStream.on 'data', Meteor.bindEnvironment (chunk) -> + data.push chunk + + file.readStream.on 'end', Meteor.bindEnvironment -> + buffer: Buffer.concat(data) + contentType: file.contentType + length: file.length + uploadDate: file.uploadDate + deleteFile: (fileName) -> try stat = this.stat fileName