Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Parse Data URI scheme for attachement fields |
| 3 | * Afterwards the object has the parameters |
| 4 | * - contentType (defaults to text/plain) |
| 5 | * - base64 (if the data was base64 encoded) |
| 6 | * - isLink (if the contentType is application/x.korap-link) |
| 7 | * - param (as a map of arbitrary parameters) |
| 8 | * - payload (the URI decoded data) |
| 9 | * |
| 10 | * @author Nils Diewald |
| 11 | */ |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 12 | "use strict"; |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 13 | |
Akron | e51eaa3 | 2020-11-10 09:35:53 +0100 | [diff] [blame] | 14 | define(function () { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 15 | |
Akron | d3bb85b | 2019-02-08 10:15:13 +0100 | [diff] [blame] | 16 | const uriRE = new RegExp("^data: *([^;,]*?(?: *; *[^,;]+?)*) *, *(.+)$"); |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 17 | const mapRE = new RegExp("^ *([^=]+?) *= *(.+?) *$"); |
| 18 | |
| 19 | return { |
| 20 | |
| 21 | /** |
| 22 | * Constructor |
| 23 | */ |
| 24 | create : function (url) { |
| 25 | return Object.create(this)._init(url); |
| 26 | }, |
| 27 | |
| 28 | // Parse URI scheme |
| 29 | _init : function (url) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 30 | const t = this; |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 31 | |
Akron | d7d3ceb | 2022-02-07 20:13:09 +0100 | [diff] [blame^] | 32 | t.base64 = false; |
| 33 | t.isLink = false; |
| 34 | t.contentType = "text/plain"; |
| 35 | |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 36 | // Decode |
Akron | d7d3ceb | 2022-02-07 20:13:09 +0100 | [diff] [blame^] | 37 | var url; |
| 38 | try { |
| 39 | url = decodeURIComponent(url); |
| 40 | } catch (e) { |
| 41 | t.payload = '[INVALID URI]'; |
| 42 | return t; |
| 43 | } |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 44 | |
| 45 | if (!uriRE.exec(url)) |
| 46 | return; |
| 47 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 48 | t.payload = RegExp.$2; |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 49 | |
| 50 | let map = {}; |
| 51 | let start = 0; |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 52 | |
| 53 | // Split parameter map |
| 54 | RegExp.$1.split(/ *; */).map(function (item) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 55 | const t = this; |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 56 | |
| 57 | // Check first parameter |
| 58 | if (!start++ && item.match(/^[-a-z0-9]+?\/.+$/)) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 59 | t.contentType = item; |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 60 | |
| 61 | if (item === "application/x.korap-link") |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 62 | t.isLink = true; |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | // Decode b64 |
| 66 | else if (item.toLowerCase() == "base64") { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 67 | t.base64 = true; |
| 68 | t.payload = window.atob(t.payload); |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | // Parse arbitrary metadata |
| 72 | else if (mapRE.exec(item)) { |
| 73 | map[RegExp.$1] = RegExp.$2; |
| 74 | }; |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 75 | }.bind(t)); |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 76 | |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 77 | t.param = map; |
| 78 | return t; |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 79 | }, |
| 80 | |
| 81 | /** |
| 82 | * Inline the attachement |
| 83 | * This should optimally be plugin-treatable |
| 84 | */ |
| 85 | inline : function () { |
| 86 | if (this.isLink) { |
Akron | ff1b1f3 | 2020-10-18 11:41:29 +0200 | [diff] [blame] | 87 | const title = this.param["title"] || this.payload; |
| 88 | const a = document.createElement('a'); |
Akron | a0ea3c3 | 2018-12-14 18:33:48 +0100 | [diff] [blame] | 89 | a.setAttribute('href', this.payload); |
| 90 | a.setAttribute('rel', 'noopener noreferrer'); |
| 91 | a.addT(title); |
| 92 | return a; |
| 93 | }; |
| 94 | |
| 95 | return document.createTextNode(this.payload); |
| 96 | } |
| 97 | } |
| 98 | }); |