Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

removed formidable dependency, throw errors when needed

  • Loading branch information...
commit babfd89d7f080747e0e8bd605c6ffb07fc4ad44b 1 parent b3e2cea
@bradleyg bradleyg authored
View
7 assets/css/styles.css
@@ -85,4 +85,11 @@ a:hover {
#files .new {
background: url('../img/messages.png') top left repeat-x;
+}
+
+#github {
+ position: absolute;
+ top: 0;
+ right: 0;
+ border: 0;
}
View
132 lib/routes.js
@@ -1,77 +1,103 @@
-var formidable = require('formidable'),
- googl = require('goo.gl'),
- fs = require('fs');
+var googl = require('goo.gl');
+var fs = require('fs');
module.exports = function(app, fileModel, client, config){
- app.get('/:secret?', function(req, res){
- if(typeof req.params.secret === 'undefined'){
- var secret = "root";
- }
- else {
- var secret = req.params.secret;
- }
+ // get files
+ var getFiles = function(req, res) {
+ var secret = req.params.secret ? req.params.secret : 'root';
+
fileModel.find({ secret: secret }, {}, { sort: {date: -1} }, function (err, files) {
+ if(err) {
+ return res.send(err, 500);
+ }
+
res.render('index.jade', {
files: files,
domain: config.s3.domain,
host: req.headers.host
});
});
- });
+ }
- app.post('/delete', function(req, res){
+ // upload file, shorten url and save to db
+ var addFile = function(req, res) {
+ var files = req.files.files;
+ var safe = {};
+
+ safe.secret = req.params.secret ? req.params.secret : 'root';
+ safe.name = files.name.replace(/ /g, "-").replace(/[?\[\]/\\=<>:;,''""&$#*()|~`!{}]/g, '');
+ safe.path = new Date().getTime() + '/' + safe.name;
+
+ client.putFile(files.path, safe.path, function(err, response){
+ if(err) {
+ return res.send(err, 500);
+ }
+
+ googl.shorten(config.s3.domain + "/" + safe.path, function (url) {
+ fs.unlink(files.path, function(err){
+ if(err) {
+ return res.send(err, 500);
+ }
+
+ save(safe, url, function(err, data){
+ if(err) {
+ return res.send(err, 500);
+ }
+
+ res.json(data, 200);
+ })
+ });
+ });
+ });
+ }
+
+ // delete file
+ var removeFile = function(req, res){
var id = req.body.id;
- fileModel.findById(id, function(err, files){
+ fileModel.findById(id, function(err, files){
+ if(err) {
+ return res.send(err, 500);
+ }
+
if(files !== null){
client.deleteFile(files.url, function(){
files.remove();
});
}
+
var data = {
success: "file deleted"
}
+
res.json(data);
});
- });
-
- app.post('/:secret?', function(req, res){
- if(typeof req.params.secret === 'undefined'){
- var secret = "root";
- }
- else {
- var secret = req.params.secret;
- }
- var form = new formidable.IncomingForm();
- form.keepExtensions = true;
- form.parse(req);
- form.on('file', function(name, file){
- var safeName = file.filename.replace(/ /g, "-").replace(/[?\[\]/\\=<>:;,''""&$#*()|~`!{}]/g, '');
- var safePath = new Date().getTime() + '/' + safeName;
- client.putFile(file.path, safePath, function(err, response){
- googl.shorten(config.s3.domain + "/" + safePath, function (shortUrl) {
- fs.unlink(file.path, function(err){
- var file = new fileModel();
- file.name = safeName;
- file.shortUrl = shortUrl.id;
- file.url = safePath;
- file.secret = secret;
- file.save(function (err) {
- var data = {
- status: {
- message: 'success',
- url: safePath,
- name: safeName,
- id: file._id,
- shortUrl: shortUrl.id
- }
- }
- res.json(data);
- });
- });
- });
- });
- });
- });
+ }
+
+ // save to the db
+ var save = function(safe, url, cb) {
+ var file = new fileModel();
+ file.name = safe.name;
+ file.shortUrl = url.id;
+ file.url = safe.path;
+ file.secret = safe.secret;
+ file.save(function(err) {
+ var data = {
+ status: {
+ message: 'success',
+ url: safe.path,
+ name: safe.name,
+ id: file._id,
+ shortUrl: url.id
+ }
+ }
+ cb(err, data);
+ });
+ }
+
+ // routes
+ app.post('/delete', removeFile);
+ app.post('/:secret?', addFile);
+ app.get('/:secret?', getFiles);
}
View
19 package.json
@@ -1,12 +1,16 @@
{
"name": "uploader",
- "version": "0.0.3",
+ "version": "0.0.3-2",
"description": "File sharing platform built on Nodejs and Amazon S3",
- "keywords": ["file", "uploads", "sharing", "AWS"],
+ "keywords": [
+ "file",
+ "uploads",
+ "sharing",
+ "AWS"
+ ],
"author": "Bradley Griffiths <bradley.griffiths@gmail.com>",
"dependencies": {
- "express": "2.5.0",
- "formidable": "1.0.8",
+ "express": "2.5.4",
"knox": "0.0.9",
"mongoose": "2.4.0",
"jade": "0.18.0",
@@ -16,7 +20,8 @@
"type": "git",
"url": "http://github.com/bradleyg/uploader.git"
},
- "engines": {
- "node": ">= 0.4.12"
+ "subdomain": "uploader",
+ "scripts": {
+ "start": "server.js"
}
-}
+}
View
26 server.js
@@ -1,12 +1,11 @@
+// requires
+var express = require("express");
+var knox = require('knox');
+var mongoose = require('mongoose');
+var config = require('./config/config.js');
+var app = express.createServer();
-var express = require("express"),
- knox = require('knox'),
- mongoose = require('mongoose'),
- config = require('./config/config.js'),
- app = express.createServer();
-
-mongoose.connect(config.mongo.db);
-
+// express
app.configure(function(){
app.use(express.methodOverride());
app.use(express.bodyParser());
@@ -16,14 +15,17 @@ app.configure(function(){
app.set("view options", { layout: false });
});
+// knox
var client = knox.createClient({
key: config.s3.key,
secret: config.s3.secret,
bucket: config.s3.bucket
});
-var Schema = mongoose.Schema,
- ObjectId = Schema.ObjectId;
+// mongoose
+mongoose.connect(config.mongo.db);
+var Schema = mongoose.Schema;
+var ObjectId = Schema.ObjectId;
var FileEntry = new Schema({
url: { type: String },
@@ -35,6 +37,8 @@ var FileEntry = new Schema({
var fileModel = mongoose.model('FileEntry', FileEntry);
-require('./lib/routes')(app, fileModel, client, config, googl);
+// routes
+require('./lib/routes')(app, fileModel, client, config);
+// server
app.listen(process.env.PORT || 3000);
View
7 views/index.jade
@@ -1,11 +1,18 @@
!!! 5
+
html
+
head
title Uploader
meta(name="robots", content="noindex, nofollow")
link(rel="stylesheet", href="/assets/css/reset.css", type="text/css")
link(rel="stylesheet", href="/assets/css/styles.css", type="text/css")
+
body
+
+ a(href='http://github.com/bradleyg/uploader')
+ img#github(src='https://a248.e.akamai.net/assets.github.com/img/30f550e0d38ceb6ef5b81500c64d970b7fb0f028/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6f72616e67655f6666373630302e706e67', alt='Fork me on GitHub')
+
#wrapper
#message
Please sign in to comment.
Something went wrong with that request. Please try again.