From dea6f875d3d9634e908371cce05546f2556b5486 Mon Sep 17 00:00:00 2001 From: GCHQ 77703 Date: Thu, 30 Aug 2018 00:54:44 +0100 Subject: [PATCH] Add "Play Audio" Operation --- src/core/Utils.mjs | 25 +++++++++++++ src/core/config/Categories.json | 3 +- src/core/operations/PlayAudio.mjs | 60 +++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/PlayAudio.mjs diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index ab3d4281..23a355bf 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -1065,6 +1065,31 @@ class Utils { }[token]; } + + /** + * Turns a dataURI into a byte array. + * Credit to sanddune (https://jsfiddle.net/uubnnr0w/380/) + * + * @param {String} dataURI + * @returns {Array} + */ + static convertDataURIToBinary(dataURI) { + const BASE64_MARKER = ";base64,"; + const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length; + const base64 = dataURI.substring(base64Index); + + const raw = atob(base64); + const rawLength = raw.length; + + const array = new Uint8Array(new ArrayBuffer(rawLength)); + + for (let i = 0; i < rawLength; i++) { + array[i] = raw.charCodeAt(i); + } + + return array; + } + } export default Utils; diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index ab3bc486..6c53f19f 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -204,7 +204,8 @@ "Escape string", "Unescape string", "Pseudo-Random Number Generator", - "Sleep" + "Sleep", + "Play Audio" ] }, { diff --git a/src/core/operations/PlayAudio.mjs b/src/core/operations/PlayAudio.mjs new file mode 100644 index 00000000..6b950129 --- /dev/null +++ b/src/core/operations/PlayAudio.mjs @@ -0,0 +1,60 @@ +/** + * @author gchq77703 [] + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ + +import Operation from "../Operation"; +import Utils from "../Utils"; + +/** + * Play Audio operation + */ +class PlayAudio extends Operation { + + /** + * PlayAudio constructor + */ + constructor() { + super(); + + this.name = "Play Audio"; + this.module = "Default"; + this.description = "Plays an audio file from a base 64 encoded data URI. URI must contain a valid audio mediatype."; + this.infoURL = "https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs"; + this.inputType = "string"; + this.outputType = "string"; + this.presentType = "html"; + this.args = []; + } + + /** + * Presents a given Data URI as an audio element. + * + * @param {String} input + * @returns {String} + */ + present(input) { + const type = input.split(";")[0].split(":")[1]; + const binary = Utils.convertDataURIToBinary(input); + const blob = new Blob([binary], { type: type}); + const src = URL.createObjectURL(blob); + + + return ``; + } + + /** + * @param {String} input + * @param {Object[]} args + * @returns {String} + */ + run(input, args) { + return input; + } + +} + +export default PlayAudio;