source: frontend/node_modules/axios/lib/helpers/fromDataURI.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 13 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 1.8 KB
Line 
1'use strict';
2
3import AxiosError from '../core/AxiosError.js';
4import parseProtocol from './parseProtocol.js';
5import platform from '../platform/index.js';
6
7// RFC 2397: data:[<mediatype>][;base64],<data>
8// mediatype = type/subtype followed by optional ;name=value parameters
9const DATA_URL_PATTERN = /^([^,;]+\/[^,;]+)?((?:;[^,;=]+=[^,;]+)*)(;base64)?,([\s\S]*)$/;
10
11/**
12 * Parse data uri to a Buffer or Blob
13 *
14 * @param {String} uri
15 * @param {?Boolean} asBlob
16 * @param {?Object} options
17 * @param {?Function} options.Blob
18 *
19 * @returns {Buffer|Blob}
20 */
21export default function fromDataURI(uri, asBlob, options) {
22 const _Blob = (options && options.Blob) || platform.classes.Blob;
23 const protocol = parseProtocol(uri);
24
25 if (asBlob === undefined && _Blob) {
26 asBlob = true;
27 }
28
29 if (protocol === 'data') {
30 uri = protocol.length ? uri.slice(protocol.length + 1) : uri;
31
32 const match = DATA_URL_PATTERN.exec(uri);
33
34 if (!match) {
35 throw new AxiosError('Invalid URL', AxiosError.ERR_INVALID_URL);
36 }
37
38 const type = match[1];
39 const params = match[2];
40 const encoding = match[3] ? 'base64' : 'utf8';
41 const body = match[4];
42
43 // RFC 2397 section 3: default mediatype is text/plain;charset=US-ASCII
44 // Bare `data:,` leaves mime undefined; Blob normalises that to "" per spec.
45 let mime;
46 if (type) {
47 mime = params ? type + params : type;
48 } else if (params) {
49 mime = 'text/plain' + params;
50 }
51
52 const buffer = Buffer.from(decodeURIComponent(body), encoding);
53
54 if (asBlob) {
55 if (!_Blob) {
56 throw new AxiosError('Blob is not supported', AxiosError.ERR_NOT_SUPPORT);
57 }
58
59 return new _Blob([buffer], { type: mime });
60 }
61
62 return buffer;
63 }
64
65 throw new AxiosError('Unsupported protocol ' + protocol, AxiosError.ERR_NOT_SUPPORT);
66}
Note: See TracBrowser for help on using the repository browser.