record-audio
Version:
Audio recording using RecordRTC. RecordRTC is a client-side audio/video/gif/html recording library.
85 lines (67 loc) • 2.31 kB
JavaScript
// Muaz Khan - www.MuazKhan.com
// MIT License - www.WebRTC-Experiment.com/licence
// Experiments - github.com/muaz-khan/WebRTC-Experiment
var config = require('./config'),
fs = require('fs'),
sys = require('sys'),
exec = require('child_process').exec;
function home(response, postData) {
response.writeHead(200, {
'Content-Type': 'text/html'
});
response.end(fs.readFileSync('./static/index.html'));
}
// this function uploads files
function upload(response, postData) {
var files = JSON.parse(postData);
// writing audio file to disk
_upload(response, files.audio);
response.statusCode = 200;
response.writeHead(200, {
'Content-Type': 'application/json'
});
response.end(files.audio.name);
}
function _upload(response, file) {
var fileRootName = file.name.split('.').shift(),
fileExtension = file.name.split('.').pop(),
filePathBase = config.upload_dir + '/',
fileRootNameWithBase = filePathBase + fileRootName,
filePath = fileRootNameWithBase + '.' + fileExtension,
fileID = 2,
fileBuffer;
while (fs.existsSync(filePath)) {
filePath = fileRootNameWithBase + '(' + fileID + ').' + fileExtension;
fileID += 1;
}
file.contents = file.contents.split(',').pop();
fileBuffer = new Buffer(file.contents, "base64");
if (config.s3_enabled) {
var knox = require('knox'),
client = knox.createClient(config.s3),
headers = {
'Content-Type': file.type
};
client.putBuffer(fileBuffer, fileRootName, headers);
} else {
fs.writeFileSync(filePath, fileBuffer);
}
}
function serveStatic(response, pathname, postData) {
var extension = pathname.split('.').pop(),
extensionTypes = {
'js': 'application/javascript',
'wav': 'audio/wav',
'gif': 'image/gif'
};
response.writeHead(200, {
'Content-Type': extensionTypes[extension]
});
if (extensionTypes[extension] == 'audio/wav')
response.end(fs.readFileSync('.' + pathname));
else
response.end(fs.readFileSync('./static' + pathname));
}
exports.home = home;
exports.upload = upload;
exports.serveStatic = serveStatic;