mirror of
https://github.com/jsbin/jsbin.git
synced 2026-01-25 15:38:56 +00:00
Initial dropbox integration
This commit is contained in:
parent
4dd7cb9421
commit
5db27dbdb2
19
lib/dropbox/bin-template.hbs
Normal file
19
lib/dropbox/bin-template.hbs
Normal file
@ -0,0 +1,19 @@
|
||||
<style>
|
||||
{{bin.output.css}}
|
||||
</style>
|
||||
{{{bin.output.html}}}
|
||||
<script>
|
||||
{{bin.output.js}}
|
||||
</script>
|
||||
|
||||
<script id="jsbin-panel-html" type="{{jsbin.panel.html.type}}">
|
||||
{{{jsbin.panel.html.content}}}
|
||||
</script>
|
||||
|
||||
<script id="jsbin-panel-css" type="{{jsbin.panel.css.type}}">
|
||||
{{{jsbin.panel.css.content}}}
|
||||
</script>
|
||||
|
||||
<script id="jsbin-panel-js" type="{{jsbin.panel.js.type}}">
|
||||
{{{jsbin.panel.js.content}}}
|
||||
</script>
|
||||
79
lib/dropbox/bin-template.js
Normal file
79
lib/dropbox/bin-template.js
Normal file
@ -0,0 +1,79 @@
|
||||
(function (root) {
|
||||
'use strict';
|
||||
|
||||
var KEYS = ['html', 'css', 'javascript'];
|
||||
|
||||
function panelTypesForBin (bin) {
|
||||
var processors = bin.settings.processors || {};
|
||||
|
||||
return function panelType(type) {
|
||||
return processors[type] || type;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
function outputForBinFactory (bin) {
|
||||
var processors = bin.settings.processors || {};
|
||||
|
||||
return function outputForBin (type) {
|
||||
if (!processors[type]) {
|
||||
return bin[type];
|
||||
} else {
|
||||
// TODO: Actually wanna chuck it through preprocessor;
|
||||
return bin[type];
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
// Basic wrapper for KEYS.reduce(fn, {});
|
||||
function reduceKeys (fn) {
|
||||
return KEYS.reduce(function(o, i){
|
||||
fn.call(null, o, i);
|
||||
return o;
|
||||
}, {});
|
||||
}
|
||||
|
||||
function createOutputObjectForBin (bin) {
|
||||
var outputForBin = outputForBinFactory(bin);
|
||||
return reduceKeys(function(obj, key) {
|
||||
obj[key] = outputForBin(key);
|
||||
});
|
||||
}
|
||||
|
||||
function createPanelsObjectForBin (bin) {
|
||||
var panelType = panelTypesForBin(bin);
|
||||
|
||||
return reduceKeys(function(obj, key) {
|
||||
/*
|
||||
obj.html = {
|
||||
type: panelType('html'),
|
||||
content: bin.html
|
||||
}
|
||||
*/
|
||||
obj[key] = {
|
||||
type: panelType(key),
|
||||
content: bin[key]
|
||||
};
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function makeHTML() {
|
||||
|
||||
}
|
||||
|
||||
function templateFromBin (bin) {
|
||||
|
||||
var templateData = {
|
||||
output: createOutputObjectForBin(bin),
|
||||
panels: createPanelsObjectForBin(bin)
|
||||
};
|
||||
|
||||
makeHTML(templateData);
|
||||
|
||||
}
|
||||
|
||||
root.templateFromBin = templateFromBin;
|
||||
|
||||
}(module && module.exports || window));
|
||||
68
lib/dropbox/index.js
Normal file
68
lib/dropbox/index.js
Normal file
@ -0,0 +1,68 @@
|
||||
'use strict';
|
||||
process.on('message', addToDropboxQueue);
|
||||
|
||||
var Dropbox = require('dropbox');
|
||||
|
||||
// delayTime in seconds;
|
||||
var delayTime = 10;
|
||||
delayTime *= 1000;
|
||||
var queue = {};
|
||||
|
||||
function addToDropboxQueue (message) {
|
||||
var bin = message.bin;
|
||||
var user = message.user;
|
||||
var fileContent = fileFromBin(bin);
|
||||
var id = bin.url;
|
||||
var saveObject = {
|
||||
file: fileContent,
|
||||
token: user.dropbox_token, // jshint ignore:line
|
||||
timeout: setTimeout(function(){
|
||||
saveToDropBox(fileContent, fileNameFromBin(bin), user);
|
||||
}, delayTime)
|
||||
};
|
||||
if (queue[id]) {
|
||||
clearTimeout(queue[id].timeout);
|
||||
}
|
||||
log('adding to queue');
|
||||
queue[id] = saveObject;
|
||||
}
|
||||
|
||||
function saveToDropBox (file, name, user) {
|
||||
log('actually saving it now');
|
||||
var client = new Dropbox.Client({
|
||||
key: '89twf77glqma71r',
|
||||
secret: '0ncbr1qrcv9ek38',
|
||||
token: user.dropbox_token // jshint ignore:line
|
||||
});
|
||||
|
||||
client.writeFile(name, file, function (err) {
|
||||
if (err) {
|
||||
process.send({
|
||||
error: err,
|
||||
user: user
|
||||
});
|
||||
}
|
||||
log('saved!');
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
function fileNameFromBin (bin) {
|
||||
return bin.url + '.html';
|
||||
}
|
||||
|
||||
function fileFromBin (bin) {
|
||||
return bin.html;
|
||||
}
|
||||
|
||||
function log (msg) {
|
||||
process.send({log: msg});
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
|
||||
fileFromBin: fileFromBin,
|
||||
|
||||
saveBin: addToDropboxQueue
|
||||
|
||||
};
|
||||
27
lib/dropbox/parent.js
Normal file
27
lib/dropbox/parent.js
Normal file
@ -0,0 +1,27 @@
|
||||
'use strict';
|
||||
var child_process = require('child_process');
|
||||
var child = child_process.fork(__dirname + '/index.js');
|
||||
|
||||
var binToSave = {
|
||||
url: 'basecannon2',
|
||||
html: '<p> Let the basecannon2 kick it </p>'
|
||||
};
|
||||
|
||||
var user = {
|
||||
dropbox_token: 'ti7z0APpzVYAAAAAAAAAAdKu6CAnmH9Tsq-aPsqAAr5nmx7TFeuQRq1_URZuh5RJ'
|
||||
};
|
||||
|
||||
child.on('message', function(message) {
|
||||
message.log && console.log(message.log); // jshint ignore:line
|
||||
});
|
||||
|
||||
function sendTheBin () {
|
||||
child.send({
|
||||
user: user,
|
||||
bin: binToSave
|
||||
});
|
||||
}
|
||||
|
||||
sendTheBin();
|
||||
|
||||
setTimeout(sendTheBin, 4000);
|
||||
Loading…
x
Reference in New Issue
Block a user