|
| 1 | +/** |
| 2 | + * @author anthony-arnold [[email protected]] |
| 3 | + * @copyright Crown Copyright 2018 |
| 4 | + * @license Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import { fromBase64, toBase64 } from "../lib/Base64"; |
| 8 | +import { fromHex } from "../lib/Hex"; |
| 9 | +import Operation from "../Operation"; |
| 10 | +import OperationError from "../errors/OperationError"; |
| 11 | +import Utils from "../Utils"; |
| 12 | +import Magic from "../lib/Magic"; |
| 13 | + |
| 14 | +/** |
| 15 | + * PlayMedia operation |
| 16 | + */ |
| 17 | +class PlayMedia extends Operation { |
| 18 | + |
| 19 | + /** |
| 20 | + * PlayMedia constructor |
| 21 | + */ |
| 22 | + constructor() { |
| 23 | + super(); |
| 24 | + |
| 25 | + this.name = "Play Media"; |
| 26 | + this.module = "Media"; |
| 27 | + this.description = "Plays the input as sound or video depending on the type."; |
| 28 | + this.infoURL = ""; |
| 29 | + this.inputType = "string"; |
| 30 | + this.outputType = "byteArray"; |
| 31 | + this.presentType = "html"; |
| 32 | + this.args = [ |
| 33 | + { |
| 34 | + "name": "Input format", |
| 35 | + "type": "option", |
| 36 | + "value": ["Raw", "Base64", "Hex"] |
| 37 | + } |
| 38 | + ]; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * @param {string} input |
| 43 | + * @param {Object[]} args |
| 44 | + * @returns {byteArray} The multimedia data as bytes. |
| 45 | + */ |
| 46 | + run(input, args) { |
| 47 | + const inputFormat = args[0]; |
| 48 | + |
| 49 | + if (!input.length) return []; |
| 50 | + |
| 51 | + // Convert input to raw bytes |
| 52 | + switch (inputFormat) { |
| 53 | + case "Hex": |
| 54 | + input = fromHex(input); |
| 55 | + break; |
| 56 | + case "Base64": |
| 57 | + // Don't trust the Base64 entered by the user. |
| 58 | + // Unwrap it first, then re-encode later. |
| 59 | + input = fromBase64(input, undefined, "byteArray"); |
| 60 | + break; |
| 61 | + case "Raw": |
| 62 | + default: |
| 63 | + input = Utils.strToByteArray(input); |
| 64 | + break; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + // Determine file type |
| 69 | + const type = Magic.magicFileType(input); |
| 70 | + if (!(type && /^audio|video/.test(type.mime))) { |
| 71 | + throw new OperationError("Invalid file type"); |
| 72 | + } |
| 73 | + |
| 74 | + return input; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Displays an audio or video element that may be able to play the media |
| 79 | + * file. |
| 80 | + * @param data {byteArray} Data containing an audio or video file. |
| 81 | + * @returns {string} Markup to display a media player. |
| 82 | + */ |
| 83 | + async present(data) { |
| 84 | + if (!data.length) return ""; |
| 85 | + |
| 86 | + const type = Magic.magicFileType(data); |
| 87 | + const matches = /^audio|video/.exec(type.mime); |
| 88 | + if (!matches) { |
| 89 | + throw new OperationError("Invalid file type"); |
| 90 | + } |
| 91 | + const dataURI = `data:${type.mime};base64,${toBase64(data)}`; |
| 92 | + const element = matches[0]; |
| 93 | + |
| 94 | + let html = `<${element} src='${dataURI}' type='${type.mime}' controls>`; |
| 95 | + html += "<p>Unsupported media type.</p>"; |
| 96 | + html += `</${element}>`; |
| 97 | + return html; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +export default PlayMedia; |
0 commit comments