diff --git a/app/apps/lib/misc/determineFileType.js b/app/apps/lib/misc/determineFileType.js new file mode 100644 index 000000000000..8cc15ebfcf3b --- /dev/null +++ b/app/apps/lib/misc/determineFileType.js @@ -0,0 +1,19 @@ +import fileType from 'file-type'; + +import { mime as MIME } from '../../../utils/lib/mimeTypes'; + +export function determineFileType(buffer, details) { + const mime = MIME.lookup(details.name); + + if (mime) { + return Array.isArray(mime) ? mime[0] : mime; + } + + const detectedType = fileType(buffer); + + if (detectedType) { + return detectedType.mime; + } + + return 'application/octet-stream'; +} diff --git a/app/apps/server/bridges/uploads.js b/app/apps/server/bridges/uploads.js index fced605713f9..59e2fca88a7c 100644 --- a/app/apps/server/bridges/uploads.js +++ b/app/apps/server/bridges/uploads.js @@ -1,4 +1,7 @@ +import { Meteor } from 'meteor/meteor'; + import { FileUpload } from '../../../file-upload/server'; +import { determineFileType } from '../../lib/misc/determineFileType'; export class AppUploadBridge { constructor(orch) { @@ -26,4 +29,21 @@ export class AppUploadBridge { }); }); } + + createUpload(details, buffer, appId) { + this.orch.debugLog(`The App ${ appId } is creating an upload "${ details.name }"`); + + details.type = determineFileType(buffer, details); + + const fileStore = FileUpload.getStore('Uploads'); + const uploadedFile = fileStore.insertSync(details, buffer); + + if (details.userId) { + Meteor.runAsUser(details.userId, () => { + Meteor.call('sendFileMessage', details.rid, null, uploadedFile); + }); + } + + return this.orch.getConverters().get('uploads').convertToApp(uploadedFile); + } } diff --git a/app/file-upload/server/lib/FileUpload.js b/app/file-upload/server/lib/FileUpload.js index b03ea21f337a..1eab825ec6fe 100644 --- a/app/file-upload/server/lib/FileUpload.js +++ b/app/file-upload/server/lib/FileUpload.js @@ -66,7 +66,7 @@ export const FileUpload = { const room = Rooms.findOneById(file.rid); const directMessageAllowed = settings.get('FileUpload_Enabled_Direct'); const fileUploadAllowed = settings.get('FileUpload_Enabled'); - if (canAccessRoom(room, user, file) !== true) { + if (user.type !== 'app' && canAccessRoom(room, user, file) !== true) { return false; } const language = user ? user.language : 'en'; diff --git a/package-lock.json b/package-lock.json index d5b3f78003aa..bb1c770e9e85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5028,9 +5028,9 @@ } }, "@rocket.chat/apps-engine": { - "version": "1.18.0-beta.3837", - "resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-1.18.0-beta.3837.tgz", - "integrity": "sha512-cAdyBDW7TQNlTsMF/imgySXdGxh3RJdbeYV8Crzl9trHzq0Mu5PPOyZBcro/iutoNE6NxG3ZHS6/5ejf8Qg4BA==", + "version": "1.18.0-beta.3848", + "resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-1.18.0-beta.3848.tgz", + "integrity": "sha512-hGlsvrPalh8OszLFhXDZn2IJ8zBmsMF8SNEEVceZgb6hUvg/j4Ebqm+5O0AanRDurda+HzS29ZJP107guKtlHQ==", "requires": { "adm-zip": "^0.4.9", "cryptiles": "^4.1.3", @@ -5042,22 +5042,6 @@ "uuid": "^3.2.1" }, "dependencies": { - "adm-zip": { - "version": "0.4.16", - "resolved": "https://registry.npmjs.org/adm-zip/-/adm-zip-0.4.16.tgz", - "integrity": "sha512-TFi4HBKSGfIKsK5YCkKaaFG2m4PEDyViZmEwof3MTIgzimHLto6muaHVpbrljdIvIrFZzEq/p4nafOeLcYegrg==" - }, - "file-type": { - "version": "15.0.1", - "resolved": "https://registry.npmjs.org/file-type/-/file-type-15.0.1.tgz", - "integrity": "sha512-0LieQlSA3bWUdErNrxzxfI4rhsvNAVPBO06R8pTc1hp9SE6nhqlVyvhcaXoMmtXkBTPnQenbMPLW9X76hH76oQ==", - "requires": { - "readable-web-to-node-stream": "^2.0.0", - "strtok3": "^6.0.3", - "token-types": "^2.0.0", - "typedarray-to-buffer": "^3.1.5" - } - }, "typescript": { "version": "2.9.2", "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.9.2.tgz", @@ -17925,7 +17909,7 @@ }, "chownr": { "version": "1.1.1", - "resolved": false, + "resolved": "", "integrity": "sha512-j38EvO5+LHX84jlo6h4UzmOwi0UgW61WRyPtJz4qaadK5eY3BTS5TY/S1Stc3Uk2lIM6TPevAlULiEJwie860g==", "dev": true, "optional": true @@ -17960,7 +17944,7 @@ }, "debug": { "version": "4.1.1", - "resolved": false, + "resolved": "", "integrity": "sha512-pYAIzeRo8J6KPEaJ0VWOh5Pzkbw/RetuzehGM7QRRX5he4fPHx2rdKMB256ehJCkX+XRQm16eZLqLNS8RSZXZw==", "dev": true, "optional": true, @@ -17991,7 +17975,7 @@ }, "fs-minipass": { "version": "1.2.5", - "resolved": false, + "resolved": "", "integrity": "sha512-JhBl0skXjUPCFH7x6x61gQxrKyXsxB5gcgePLZCwfyCGGsTISMoIeObbrvVeP6Xmyaudw4TT43qV2Gz+iyd2oQ==", "dev": true, "optional": true, @@ -18025,7 +18009,7 @@ }, "glob": { "version": "7.1.3", - "resolved": false, + "resolved": "", "integrity": "sha512-vcfuiIxogLV4DlGBHIUOwI0IbrJ8HWPc4MU7HzviGeNho/UJDfi6B5p3sHeWIQ0KGIU0Jpxi5ZHxemQfLkkAwQ==", "dev": true, "optional": true, @@ -18057,7 +18041,7 @@ }, "ignore-walk": { "version": "3.0.1", - "resolved": false, + "resolved": "", "integrity": "sha512-DTVlMx3IYPe0/JJcYP7Gxg7ttZZu3IInhuEhbchuqneY9wWe5Ojy2mXLBaQFUQmo0AW2r3qG7m1mg86js+gnlQ==", "dev": true, "optional": true, @@ -18078,7 +18062,7 @@ }, "inherits": { "version": "2.0.3", - "resolved": false, + "resolved": "", "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", "dev": true, "optional": true @@ -18126,7 +18110,7 @@ }, "minipass": { "version": "2.3.5", - "resolved": false, + "resolved": "", "integrity": "sha512-Gi1W4k059gyRbyVUZQ4mEqLm0YIUiGYfvxhF6SIlk3ui1WVxMTGfGdQ2SInh3PDrRTVvPKgULkpJtT4RH10+VA==", "dev": true, "optional": true, @@ -18137,7 +18121,7 @@ }, "minizlib": { "version": "1.2.1", - "resolved": false, + "resolved": "", "integrity": "sha512-7+4oTUOWKg7AuL3vloEWekXY2/D20cevzsrNT2kGWm+39J9hGTCBv8VI5Pm5lXZ/o3/mdR4f8rflAPhnQb8mPA==", "dev": true, "optional": true, @@ -18157,7 +18141,7 @@ }, "ms": { "version": "2.1.1", - "resolved": false, + "resolved": "", "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==", "dev": true, "optional": true @@ -18171,7 +18155,7 @@ }, "needle": { "version": "2.3.0", - "resolved": false, + "resolved": "", "integrity": "sha512-QBZu7aAFR0522EyaXZM0FZ9GLpq6lvQ3uq8gteiDUp7wKdy0lSd2hPlgFwVuW1CBkfEs9PfDQsQzZghLs/psdg==", "dev": true, "optional": true, @@ -18183,7 +18167,7 @@ }, "node-pre-gyp": { "version": "0.12.0", - "resolved": false, + "resolved": "", "integrity": "sha512-4KghwV8vH5k+g2ylT+sLTjy5wmUOb9vPhnM8NHvRf9dHmnW/CndrFXy2aRPaPST6dugXSdHXfeaHQm77PIz/1A==", "dev": true, "optional": true, @@ -18213,14 +18197,14 @@ }, "npm-bundled": { "version": "1.0.6", - "resolved": false, + "resolved": "", "integrity": "sha512-8/JCaftHwbd//k6y2rEWp6k1wxVfpFzB6t1p825+cUb7Ym2XQfhwIC5KwhrvzZRJu+LtDE585zVaS32+CGtf0g==", "dev": true, "optional": true }, "npm-packlist": { "version": "1.4.1", - "resolved": false, + "resolved": "", "integrity": "sha512-+TcdO7HJJ8peiiYhvPxsEDhF3PJFGUGRcFsGve3vxvxdcpO2Z4Z7rkosRM0kWj6LfbK/P0gu3dzk5RU1ffvFcw==", "dev": true, "optional": true, @@ -18300,7 +18284,7 @@ }, "process-nextick-args": { "version": "2.0.0", - "resolved": false, + "resolved": "", "integrity": "sha512-MtEC1TqN0EU5nephaJ4rAtThHtC86dNN9qCuEhtshvpVBkAW5ZO7BASN9REnF9eoXGcRub+pFuKEpOHE+HbEMw==", "dev": true, "optional": true @@ -18345,7 +18329,7 @@ }, "rimraf": { "version": "2.6.3", - "resolved": false, + "resolved": "", "integrity": "sha512-mwqeW5XsA2qAejG46gYdENaxXjx9onRNCfn7L0duuP4hCuTIi/QO7PDK07KJfp1d+izWPrzEJDcSqBa0OZQriA==", "dev": true, "optional": true, @@ -18376,7 +18360,7 @@ }, "semver": { "version": "5.7.0", - "resolved": false, + "resolved": "", "integrity": "sha512-Ya52jSX2u7QKghxeoFGpLwCtGlt7j0oY9DYb5apt9nPlJ42ID+ulTXESnt/qAQcoSERyZ5sl3LDIOw0nAn/5DA==", "dev": true, "optional": true @@ -18436,7 +18420,7 @@ }, "tar": { "version": "4.4.8", - "resolved": false, + "resolved": "", "integrity": "sha512-LzHF64s5chPQQS0IYBn9IN5h3i98c12bo4NCO7e0sGM2llXQ3p2FGC5sdENN4cTW48O915Sh+x+EXx7XW96xYQ==", "dev": true, "optional": true, @@ -18476,7 +18460,7 @@ }, "yallist": { "version": "3.0.3", - "resolved": false, + "resolved": "", "integrity": "sha512-S+Zk8DEWE6oKpV+vI3qWkaK+jSbIK86pCwe2IF/xwIpQ8jEuxpw9NyaGjmp9+BoJv5FV2piqCDcoCtStppiq2A==", "dev": true, "optional": true diff --git a/package.json b/package.json index daf94c8c4eff..ff4d16e57a5a 100644 --- a/package.json +++ b/package.json @@ -129,7 +129,7 @@ "@nivo/heatmap": "^0.61.0", "@nivo/line": "^0.61.1", "@nivo/pie": "^0.61.1", - "@rocket.chat/apps-engine": "1.18.0-beta.3837", + "@rocket.chat/apps-engine": "1.18.0-beta.3848", "@rocket.chat/css-in-js": "^0.14.1", "@rocket.chat/fuselage": "^0.14.1", "@rocket.chat/fuselage-hooks": "^0.14.1",