Skip to content

Instantly share code, notes, and snippets.

@fiveisprime
Last active January 3, 2016 05:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fiveisprime/8419342 to your computer and use it in GitHub Desktop.
Save fiveisprime/8419342 to your computer and use it in GitHub Desktop.
Cloud storage.
var express = require('express')
, formidable = require('formidable')
, fs = require('fs')
, path = require('path')
, app = express()
, storedFiles = [];
//
// Serve the cloud storage contents.
//
app.use('/files', express.static(process.env.CLOUD_DIR));
//
// Render the link to the /files route and an upload form.
//
app.get('/', function(req, res) {
res.send([
'<p><a href="/files">Show Files</a></p>'
, '<form method="post" enctype="multipart/form-data">'
, '<p>Image: <input type="file" name="image" /></p>'
, '<p><input type="submit" value="Upload" /></p>'
, '</form>'
].join(''));
});
app.post('/', function(req, res, next) {
var form = new formidable.IncomingForm();
// Upload files to the CLOUD_DIR.
form.uploadDir = process.env.CLOUD_DIR;
form.keepExtensions = true;
form.parse(req, function(err, fields, files) {
//
// Grab the temp name of the file. Just store it in an array, but this
// could be persisted in a db or the directory should be parsed and the
// array populated with existing files on module load.
//
var bits = files.image.path.split('/')
, name = bits[bits.length - 1];
storedFiles.push(name);
res.redirect('/files');
});
});
//
// Show a list of uploaded files.
//
app.get('/files', function(req, res) {
var out = '<p><a href="/">Home</a></p>';
for (var i = 0; i < storedFiles.length; i++) {
out += '<p><a href="/files/' + storedFiles[i] + '">' + storedFiles[i] + '</a></p>';
}
res.send(out);
});
app.listen(process.env.PORT);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment