Add plugin API for handling uploads with specific file extensions

This commit is contained in:
David Taylor 2018-08-15 17:19:08 +01:00
parent a83f662492
commit e7e8f4ef4c
2 changed files with 39 additions and 1 deletions

View File

@ -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" };

View File

@ -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;