From 979fcf21525ac99192fd01973f2590afeb2a2e9f Mon Sep 17 00:00:00 2001 From: catarak Date: Thu, 21 Jul 2016 14:18:38 -0400 Subject: [PATCH] add support for audio files --- client/modules/IDE/actions/files.js | 34 +++++++++++++++---- client/modules/IDE/components/FileUploader.js | 2 +- client/modules/IDE/components/PreviewFrame.js | 2 +- package.json | 4 ++- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/client/modules/IDE/actions/files.js b/client/modules/IDE/actions/files.js index afdb1b36..a4db7961 100644 --- a/client/modules/IDE/actions/files.js +++ b/client/modules/IDE/actions/files.js @@ -1,6 +1,8 @@ import * as ActionTypes from '../../../constants'; import axios from 'axios'; import blobUtil from 'blob-util'; +import xhr from 'xhr'; +import fileType from 'file-type'; const ROOT_URL = location.href.indexOf('localhost') > 0 ? 'http://localhost:8000/api' : '/api'; @@ -33,15 +35,33 @@ export function updateFileContent(name, content) { export function getBlobUrl(file) { return (dispatch) => { - blobUtil.imgSrcToBlob(file.url, undefined, { crossOrigin: 'Anonymous' }) - .then(blobUtil.createObjectURL) - .then(objectURL => { - dispatch({ - type: ActionTypes.SET_BLOB_URL, - name: file.name, - blobURL: objectURL + xhr({ + uri: file.url, + responseType: 'arraybuffer', + useXDR: true + }, (err, body, res) => { + if (err) throw err; + const typeOfFile = fileType(new Uint8Array(res)); + blobUtil.arrayBufferToBlob(res, typeOfFile.mime) + .then(blobUtil.createObjectURL) + .then(objectURL => { + dispatch({ + type: ActionTypes.SET_BLOB_URL, + name: file.name, + blobURL: objectURL + }); }); }); + + // blobUtil.imgSrcToBlob(file.url, undefined, { crossOrigin: 'Anonymous' }) + // .then(blobUtil.createObjectURL) + // .then(objectURL => { + // dispatch({ + // type: ActionTypes.SET_BLOB_URL, + // name: file.name, + // blobURL: objectURL + // }); + // }); }; } diff --git a/client/modules/IDE/components/FileUploader.js b/client/modules/IDE/components/FileUploader.js index 7b886ac1..817dd4ee 100644 --- a/client/modules/IDE/components/FileUploader.js +++ b/client/modules/IDE/components/FileUploader.js @@ -23,7 +23,7 @@ class FileUploader extends React.Component { maxThumbnailFilesize: 8, // 3MB thumbnailWidth: 200, thumbnailHeight: 200, - acceptedMimeTypes: 'image/bmp,image/gif,image/jpg,image/jpeg,image/png', + acceptedFiles: 'image/bmp,image/gif,image/jpg,image/jpeg,image/png,audio/*', dictDefaultMessage: 'Drop files here to upload or click to use the file browser', accept: this.props.dropzoneAcceptCallback, sending: this.props.dropzoneSendingCallback, diff --git a/client/modules/IDE/components/PreviewFrame.js b/client/modules/IDE/components/PreviewFrame.js index 1279890c..9eb33041 100644 --- a/client/modules/IDE/components/PreviewFrame.js +++ b/client/modules/IDE/components/PreviewFrame.js @@ -97,7 +97,7 @@ class PreviewFrame extends React.Component { let jsFileStrings = newJSFile.content.match(/(['"])((\\\1|.)*?)\1/gm); jsFileStrings = jsFileStrings || []; jsFileStrings.forEach(jsFileString => { - if (jsFileString.match(/^('|")(?!(http:\/\/|https:\/\/)).*\.(png|jpg|jpeg|gif|bmp)('|")$/)) { + if (jsFileString.match(/^('|")(?!(http:\/\/|https:\/\/)).*\.(png|jpg|jpeg|gif|bmp|mp3|wav|aiff|ogg)('|")$/)) { const filePath = jsFileString.substr(1, jsFileString.length - 2); let fileName = filePath; if (fileName.match(/^\.\//)) { diff --git a/package.json b/package.json index 211ea9ed..4a367d2a 100644 --- a/package.json +++ b/package.json @@ -75,6 +75,7 @@ "express": "^4.13.4", "express-session": "^1.13.0", "file-saver": "^1.3.2", + "file-type": "^3.8.0", "htmlhint": "^0.9.13", "jshint": "^2.9.2", "jszip": "^3.0.0", @@ -96,6 +97,7 @@ "s3-policy": "^0.2.0", "shortid": "^2.2.6", "srcdoc-polyfill": "^0.2.0", - "throttle-debounce": "^1.0.1" + "throttle-debounce": "^1.0.1", + "xhr": "^2.2.1" } }