From e7e8f4ef4cf5f86d60a58ec11b0610811656bc5f Mon Sep 17 00:00:00 2001 From: David Taylor Date: Wed, 15 Aug 2018 17:19:08 +0100 Subject: [PATCH] Add plugin API for handling uploads with specific file extensions --- .../components/composer-editor.js.es6 | 21 +++++++++++++++++++ .../discourse/lib/plugin-api.js.es6 | 19 ++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6 index a93267c6693..6218dbcfbc2 100644 --- a/app/assets/javascripts/discourse/components/composer-editor.js.es6 +++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6 @@ -38,6 +38,14 @@ import { const REBUILD_SCROLL_MAP_EVENTS = ["composer:resized", "composer:typed-reply"]; +const uploadHandlers = []; +export function addComposerUploadHandler(extensions, method) { + uploadHandlers.push({ + extensions, + method + }); +} + export default Ember.Component.extend({ classNameBindings: ["showToolbar:toolbar-visible", ":wmd-controls"], @@ -587,6 +595,19 @@ export default Ember.Component.extend({ }); $element.on("fileuploadsubmit", (e, data) => { + // Look for a matching file upload handler contributed from a plugin + const matcher = handler => { + const ext = handler.extensions.join("|"); + const regex = new RegExp(`\\.(${ext})$`, "i"); + return regex.test(data.files[0].name); + }; + const matchingHandler = uploadHandlers.find(matcher); + if (data.files.length === 1 && matchingHandler) { + matchingHandler.method(data.files[0]); + return false; + } + + // If no plugin, continue as normal const isPrivateMessage = this.get("composer.privateMessage"); data.formData = { type: "composer" }; diff --git a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 index 6c20b968f56..7f3b9e43898 100644 --- a/app/assets/javascripts/discourse/lib/plugin-api.js.es6 +++ b/app/assets/javascripts/discourse/lib/plugin-api.js.es6 @@ -35,9 +35,10 @@ import { registerCustomAvatarHelper } from "discourse/helpers/user-avatar"; import { disableNameSuppression } from "discourse/widgets/poster-name"; import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1 } from "discourse/controllers/topic"; import Sharing from "discourse/lib/sharing"; +import { addComposerUploadHandler } from "discourse/components/composer-editor"; // If you add any methods to the API ensure you bump up this number -const PLUGIN_API_VERSION = "0.8.23"; +const PLUGIN_API_VERSION = "0.8.24"; class PluginApi { constructor(version, container) { @@ -753,6 +754,22 @@ class PluginApi { Sharing.addSharingId(options.id); Sharing.addSource(options); } + + /** + * + * Registers a function to handle uploads for specified file types + * The normal uploading functionality will be bypassed + * This only for uploads of individual files + * + * Example: + * + * addComposerUploadHandler(["mp4", "mov"], (file) => { + * console.log("Handling upload for", file.name); + * }) + */ + addComposerUploadHandler(extensions, method) { + addComposerUploadHandler(extensions, method); + } } let _pluginv01;