diff --git a/lib/RegExp.coffee b/lib/RegExp.coffee deleted file mode 100644 index c068aefc58d..00000000000 --- a/lib/RegExp.coffee +++ /dev/null @@ -1,2 +0,0 @@ -RegExp.escape = (s) -> - return s.replace /[-\/\\^$*+?.()|[\]{}]/g, '\\$&' \ No newline at end of file diff --git a/lib/RegExp.js b/lib/RegExp.js new file mode 100644 index 00000000000..60d252508d3 --- /dev/null +++ b/lib/RegExp.js @@ -0,0 +1,3 @@ +RegExp.escape = function(s) { + return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); +}; diff --git a/lib/fileUpload.coffee b/lib/fileUpload.coffee deleted file mode 100644 index ee0f2dc7504..00000000000 --- a/lib/fileUpload.coffee +++ /dev/null @@ -1,69 +0,0 @@ -if UploadFS? - RocketChat.models.Uploads.allow - insert: (userId, doc) -> - return userId - - update: (userId, doc) -> - return userId is doc.userId - - remove: (userId, doc) -> - return userId is doc.userId - - initFileStore = -> - cookie = new Cookies() - if Meteor.isClient - document.cookie = 'rc_uid=' + escape(Meteor.userId()) + '; path=/' - document.cookie = 'rc_token=' + escape(Accounts._storedLoginToken()) + '; path=/' - - Meteor.fileStore = new UploadFS.store.GridFS - collection: RocketChat.models.Uploads.model - name: 'rocketchat_uploads' - collectionName: 'rocketchat_uploads' - filter: new UploadFS.Filter - onCheck: FileUpload.validateFileUpload - transformWrite: (readStream, writeStream, fileId, file) -> - if RocketChatFile.enabled is false or not /^image\/.+/.test(file.type) - return readStream.pipe writeStream - - stream = undefined - - identify = (err, data) -> - if err? - return stream.pipe writeStream - - file.identify = - format: data.format - size: data.size - - if data.Orientation? and data.Orientation not in ['', 'Unknown', 'Undefined'] - RocketChatFile.gm(stream).autoOrient().stream().pipe(writeStream) - else - stream.pipe writeStream - - stream = RocketChatFile.gm(readStream).identify(identify).stream() - - onRead: (fileId, file, req, res) -> - if RocketChat.settings.get 'FileUpload_ProtectFiles' - rawCookies = req.headers.cookie if req?.headers?.cookie? - uid = cookie.get('rc_uid', rawCookies) if rawCookies? - token = cookie.get('rc_token', rawCookies) if rawCookies? - - if not uid? - uid = req.query.rc_uid - token = req.query.rc_token - - unless uid and token and RocketChat.models.Users.findOneByIdAndLoginToken(uid, token) - res.writeHead 403 - return false - - res.setHeader 'content-disposition', "attachment; filename=\"#{ encodeURIComponent(file.name) }\"" - return true - - Meteor.startup -> - if Meteor.isServer - initFileStore() - else - Tracker.autorun (c) -> - if Meteor.userId() and RocketChat.settings.cachedCollection.ready.get() - initFileStore() - c.stop() diff --git a/lib/fileUpload.js b/lib/fileUpload.js new file mode 100644 index 00000000000..00cd4952289 --- /dev/null +++ b/lib/fileUpload.js @@ -0,0 +1,101 @@ +/* globals UploadFS, Cookies, FileUpload */ + +if (UploadFS) { + RocketChat.models.Uploads.allow({ + insert(userId/*, doc*/) { + return userId; + }, + + update(userId, doc) { + return userId === doc.userId; + }, + + remove(userId, doc) { + return userId === doc.userId; + } + }); + + const initFileStore = function() { + const cookie = new Cookies(); + if (Meteor.isClient) { + document.cookie = 'rc_uid=' + escape(Meteor.userId()) + '; path=/'; + document.cookie = 'rc_token=' + escape(Accounts._storedLoginToken()) + '; path=/'; + } + + Meteor.fileStore = new UploadFS.store.GridFS({ + collection: RocketChat.models.Uploads.model, + name: 'rocketchat_uploads', + collectionName: 'rocketchat_uploads', + filter: new UploadFS.Filter({ + onCheck: FileUpload.validateFileUpload + }), + transformWrite(readStream, writeStream, fileId, file) { + if (RocketChatFile.enabled === false || !/^image\/.+/.test(file.type)) { + return readStream.pipe(writeStream); + } + + let stream = undefined; + + const identify = function(err, data) { + if (err) { + return stream.pipe(writeStream); + } + + file.identify = { + format: data.format, + size: data.size + }; + + if (data.Orientation && !['', 'Unknown', 'Undefined'].includes(data.Orientation)) { + RocketChatFile.gm(stream).autoOrient().stream().pipe(writeStream); + } else { + stream.pipe(writeStream); + } + }; + + stream = RocketChatFile.gm(readStream).identify(identify).stream(); + }, + + onRead(fileId, file, req, res) { + if (RocketChat.settings.get('FileUpload_ProtectFiles')) { + let uid, token; + + if (req && req.headers && req.headers.cookie) { + const rawCookies = req.headers.cookie; + + if (rawCookies) { + uid = cookie.get('rc_uid', rawCookies) ; + token = cookie.get('rc_token', rawCookies); + } + } + + if (!uid) { + uid = req.query.rc_uid; + token = req.query.rc_token; + } + + if (!uid || !token || !RocketChat.models.Users.findOneByIdAndLoginToken(uid, token)) { + res.writeHead(403); + return false; + } + } + + res.setHeader('content-disposition', `attachment; filename="${ encodeURIComponent(file.name) }"`); + return true; + } + }); + }; + + Meteor.startup(function() { + if (Meteor.isServer) { + initFileStore(); + } else { + Tracker.autorun(function(c) { + if (Meteor.userId() && RocketChat.settings.cachedCollection.ready.get()) { + initFileStore(); + c.stop(); + } + }); + } + }); +} diff --git a/lib/francocatena_fix.coffee b/lib/francocatena_fix.coffee deleted file mode 100644 index a2ef678fb65..00000000000 --- a/lib/francocatena_fix.coffee +++ /dev/null @@ -1,2 +0,0 @@ -@i18n_status_func = (key,options) -> - return TAPi18n.__(key,options) diff --git a/lib/francocatena_fix.js b/lib/francocatena_fix.js new file mode 100644 index 00000000000..12125e91853 --- /dev/null +++ b/lib/francocatena_fix.js @@ -0,0 +1,3 @@ +this.i18n_status_func = function(key, options) { + return TAPi18n.__(key, options); +}; diff --git a/lib/underscore.string.coffee b/lib/underscore.string.coffee deleted file mode 100644 index f0845a77574..00000000000 --- a/lib/underscore.string.coffee +++ /dev/null @@ -1,15 +0,0 @@ -# This will add underscore.string methods to Underscore.js -# except for include, contains, reverse and join that are -# dropped because they collide with the functions already -# defined by Underscore.js. - -mixin = (obj) -> - _.each _.functions(obj), (name) -> - if not _[name] and not _.prototype[name]? - func = _[name] = obj[name] - _.prototype[name] = -> - args = [this._wrapped] - push.apply(args, arguments) - return result.call(this, func.apply(_, args)) - -mixin(s.exports()) \ No newline at end of file diff --git a/lib/underscore.string.js b/lib/underscore.string.js new file mode 100644 index 00000000000..f92095c585c --- /dev/null +++ b/lib/underscore.string.js @@ -0,0 +1,16 @@ +/* globals mixin */ + +// This will add underscore.string methods to Underscore.js +// except for include, contains, reverse and join that are +// dropped because they collide with the functions already +// defined by Underscore.js. + +mixin = function(obj) { + _.each(_.functions(obj), function(name) { + if (!_[name] && !_.prototype[name]) { + _[name] = obj[name]; + } + }); +}; + +mixin(s.exports());