|
1 | 1 | 'use strict'; |
2 | 2 |
|
| 3 | +const {_handleNewLines, _isValid, uploadFile} = require('ep_image_upload/static/js/toolbar'); |
| 4 | + |
3 | 5 | // When an image is detected give it a lineAttribute |
4 | 6 | // of Image with the URL to the iamge |
5 | 7 | exports.collectContentImage = (hookName, {node, state: {lineAttributes}, tname}) => { |
6 | 8 | if (tname === 'div' || tname === 'p') delete lineAttributes.img; |
7 | 9 | if (tname !== 'img') return; |
8 | | - lineAttributes.img = |
| 10 | + const imageData = |
9 | 11 | // Client-side. This will also be used for server-side HTML imports once jsdom adds support |
10 | 12 | // for HTMLImageElement.currentSrc. |
11 | 13 | node.currentSrc || |
12 | 14 | // Server-side HTML imports using jsdom v16.6.0 (Etherpad v1.8.15). |
13 | 15 | node.src || |
14 | 16 | // Server-side HTML imports using cheerio (Etherpad <= v1.8.14). |
15 | 17 | (node.attribs && node.attribs.src); |
| 18 | + |
| 19 | + if (typeof window !== 'undefined' && clientVars.ep_image_upload.storageType === 'local') { |
| 20 | + if (/^http/.test(imageData)) { |
| 21 | + // an uploaded image is copied, place a copy in the desired line |
| 22 | + lineAttributes.img = imageData; |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const padeditor = require('ep_etherpad-lite/static/js/pad_editor').padeditor; |
| 27 | + |
| 28 | + const match = imageData.match(/data:(?<mime>[^;]+);base64,(?<data>.*)/); |
| 29 | + if (!match || !match.groups.data || !match.groups.mime) return; |
| 30 | + |
| 31 | + // decode from internal base64 rep |
| 32 | + const decodedData = Uint8Array.from(window.atob(match.groups.data), (c) => c.charCodeAt(0)); |
| 33 | + |
| 34 | + // check if size is within limits and mime type is supported |
| 35 | + const extension = _isValid({size: decodedData.length, type: match.groups.mime}); |
| 36 | + if (!extension) return; |
| 37 | + |
| 38 | + const blob = new Blob([decodedData], {type: match.groups.mime}); |
| 39 | + |
| 40 | + // image.* is a temporary name not used on the server |
| 41 | + uploadFile(padeditor, blob, `image.${extension}`); |
| 42 | + } else { |
| 43 | + lineAttributes.img = imageData; |
| 44 | + } |
16 | 45 | }; |
17 | 46 |
|
18 | 47 | exports.collectContentPre = (name, context) => { |
|
0 commit comments