From af2b1413760af1d9f5f578adb9b53ee75f8e5dd1 Mon Sep 17 00:00:00 2001 From: webzwo0i Date: Sun, 28 Nov 2021 00:26:40 +0100 Subject: [PATCH] respect storageType on copy&paste Instead of observing the clipboard, we first put the data URI into the pad text, so it is uploaded by contentCollector. When Etherpad starts using the Clipboard API we should use it too. --- static/js/contentCollection.js | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/static/js/contentCollection.js b/static/js/contentCollection.js index 36a555b..d4726e8 100644 --- a/static/js/contentCollection.js +++ b/static/js/contentCollection.js @@ -1,11 +1,13 @@ 'use strict'; +const {_isValid, uploadFile} = require('ep_image_upload/static/js/toolbar'); + // When an image is detected give it a lineAttribute // of Image with the URL to the iamge exports.collectContentImage = (hookName, {node, state: {lineAttributes}, tname}) => { if (tname === 'div' || tname === 'p') delete lineAttributes.img; if (tname !== 'img') return; - lineAttributes.img = + const imageData = // Client-side. This will also be used for server-side HTML imports once jsdom adds support // for HTMLImageElement.currentSrc. node.currentSrc || @@ -13,6 +15,33 @@ exports.collectContentImage = (hookName, {node, state: {lineAttributes}, tname}) node.src || // Server-side HTML imports using cheerio (Etherpad <= v1.8.14). (node.attribs && node.attribs.src); + + if (typeof window !== 'undefined' && clientVars.ep_image_upload.storageType === 'local') { + if (/^http/.test(imageData)) { + // an uploaded image is copied, place a copy in the desired line + lineAttributes.img = imageData; + return; + } + + const padeditor = require('ep_etherpad-lite/static/js/pad_editor').padeditor; + + const match = imageData.match(/data:([^;]+);base64,(.*)/); + if (!match || !match[1] || !match[2]) return; + + // decode from internal base64 rep + const decodedData = Uint8Array.from(window.atob(match[2]), (c) => c.charCodeAt(0)); + + // check if size is within limits and mime type is supported + const extension = _isValid({size: decodedData.length, type: match[1]}); + if (!extension) return; + + const blob = new Blob([decodedData], {type: match[1]}); + + // image.* is a temporary name not used on the server + uploadFile(padeditor, blob, `image.${extension}`); + } else { + lineAttributes.img = imageData; + } }; exports.collectContentPre = (name, context) => {