Skip to content

Instantly share code, notes, and snippets.

@botic
Created August 18, 2011 11:01
Show Gist options
  • Save botic/1153841 to your computer and use it in GitHub Desktop.
Save botic/1153841 to your computer and use it in GitHub Desktop.
Code Snippets
var fullRound = function(from, to) {
var startVal = Math.ceil(from);
var dir = startVal < 0 ? -1 : +1;
while (startVal % to !== 0) {
startVal += dir;
}
return startVal;
}
fullRound(16, 9); // 18
fullRound(-16, 9); // -18
// RingoJS String Utils #ftw
var strings = require("ringo/utils/strings");
var encryptPassword = function(password, salt) {
return strings.digest(strings.digest(salt, "SHA-1") + password, "SHA-512");
};
var checkPassword = function(password, salt, digest) {
return digest === encryptPassword(password, salt);
};
/**
* Requires:
* Stick master@af8ffe397dca4ca81d29
* RingoJS 0.8
*/
var {Application} = require("stick");
var {ZipFile} = require("ringo/zip");
var {mimeType} = require("ringo/mime");
var log = require("ringo/logging").getLogger(module.id);
var fs = require("fs");
var fileUtils = require("ringo/utils/files");
var response = require("ringo/jsgi/response");
var config = require("config");
var app = exports.app = new Application();
app.configure("params", "upload", "route");
app.post("/uploadImage", function(req) {
if (req.postParams.formFileInput && req.postParams.formFileInput.value.length > 0) {
log.info("Writing file: " + req.postParams.formFileInput.value.length
+ " To: " + module.resolve("files/" + req.postParams.formFileInput.filename));
var tempFile = fileUtils.createTempFile(req.postParams.formFileInput.filename);
fs.write(tempFile, req.postParams.formFileInput.value);
if (req.postParams.formFileInput.contentType === "application/zip") {
var zip = new ZipFile(tempFile);
zip.entries.forEach(function(entry) {
var destFile = fs.openRaw(config.fileDir + fileUtils.separator + entry, {write: true});
zip.open(entry).copy(destFile).close();
});
}
return response.redirect(req.scriptName + "/form");
}
return response.html("<html><body><h1>Upload failed.</h1></body></html>");
});
app.get("/form", function(req) {
return response.html("<html><head><title>File Upload Demo</title></head> \
<body><form method='POST' action='uploadImage' enctype='multipart/form-data'> \
<h1>Upload:</h1> \
<input type='file' name='formFileInput' /><hr/><input type='submit' value='Upload' /> \
</form></body> \
</html>");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment