-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Clicking the area now opens a file dialog to enable non-drag-and-drop uploads. - Rewrote the parts that were using jQuery and removed jQuery from the project. - Simplified the look and feel and made it small-screen-friendly.
- Loading branch information
Showing
17 changed files
with
431 additions
and
315 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
root = true | ||
|
||
[*] | ||
end_of_line = lf | ||
indent_size = 2 | ||
indent_style = space | ||
insert_final_newline = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.DS_Store | ||
node_modules/ | ||
uploads/ | ||
npm-debug.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,21 @@ | ||
Copyright (c) 2012 Mert Yazicioglu | ||
The MIT License (MIT) | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
Copyright (c) 2012 Mert Yazıcıoğlu | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,34 @@ | ||
Simple File Uploader | ||
=== | ||
# Simple File Uploader | ||
|
||
[![Code Climate](https://codeclimate.com/github/merty/simple-file-uploader/badges/gpa.svg)](https://codeclimate.com/github/merty/simple-file-uploader) | ||
|
||
Simple File Uploader is a file uploader written using HTML5 and Node.js. It can upload both to a local directory on the server or to an AWS S3 server. | ||
|
||
Clearly, it is not a revolutionary file uploader that will change the way mankind upload their files. Seeing that many people are actually interested in both HTML5 File API and Node.js, decided to write a simple file uploader application as many of the examples out there are not clear enough for beginners. Hope this helps! | ||
Seeing that many people are actually interested in both HTML5 File API and Node.js, decided to write a simple file uploader application as many of the examples out there are a bit complicated for beginners. Hope this helps! | ||
|
||
Usage | ||
--- | ||
## Usage | ||
|
||
1. Clone the repository or download and extract the files. | ||
2. Install Node.js if you haven't already. | ||
3. Go to the directory where index.js etc. are. | ||
4. Edit config.js if you wish to change the upload directory or the port number. | ||
5. Run the application using `node index.js` | ||
3. Go to the project directory. | ||
4. Run the command `npm install` to install the dependencies. | ||
4. Edit `config.js` if you wish to change the upload directory or use AWS S3. | ||
5. Run the application using `npm start`. | ||
6. Go to `http://<IP_ADDRESS>:<PORT>` where `<IP_ADDRESS>` is the IP address of the machine where the application is running and the `<PORT>` is the port number defined in `config.js` which is `8000` by default. | ||
7. Drag and drop files to the marked area to upload the files to the `upload_dir` defined in `config.js`. | ||
7. Drag and drop files to the marked area or click the text and select files to upload the files. | ||
|
||
**To use with AWS S3:** | ||
## Changelog | ||
|
||
1. Install knox using `npm install knox`. | ||
2. Edit config.js to fill in the values for the keys `key`, `secret` and `bucket`, and replace the last line with `exports.s3_enabled = true;`. | ||
**0.2.0** | ||
|
||
License | ||
--- | ||
* Clicking the area now opens a file dialog to enable non-drag-and-drop uploads. | ||
* Rewrote the parts that were using jQuery and removed jQuery from the project. | ||
* Simplified the look and feel and made it small-screen-friendly. | ||
|
||
**0.1.0** | ||
|
||
* Initial release. | ||
|
||
## License | ||
|
||
This application is released under the MIT License. See the `LICENSE` file for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
exports.port = 8000; | ||
exports.upload_dir = './uploads'; | ||
|
||
exports.s3 = { | ||
key: '', | ||
secret: '', | ||
bucket: '' | ||
module.exports = { | ||
port: 8000, | ||
s3: { | ||
key: '', | ||
secret: '', | ||
bucket: '' | ||
}, | ||
s3_enabled: false, | ||
upload_dir: './uploads' | ||
}; | ||
|
||
exports.s3_enabled = false; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,75 @@ | ||
var config = require('./config'), | ||
fs = require('fs'); | ||
var config = require('./config'); | ||
var fs = require('fs'); | ||
|
||
function home(response, postData) { | ||
response.writeHead(200, {'Content-Type': 'text/html'}); | ||
response.end(fs.readFileSync('./static/index.html')); | ||
response.writeHead(200, {'Content-Type': 'text/html'}); | ||
response.end(fs.readFileSync('./static/index.html')); | ||
} | ||
|
||
function upload(response, postData) { | ||
|
||
var file = JSON.parse(postData), | ||
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, function (err, res) { | ||
|
||
if (typeof res !== "undefined" && 200 === res.statusCode) { | ||
console.log('Uploaded to: %s', res.client._httpMessage.url); | ||
response.statusCode = 200; | ||
} else { | ||
console.log('Upload failed!'); | ||
response.statusCode = 500; | ||
} | ||
|
||
response.end(); | ||
}); | ||
|
||
} else { | ||
fs.writeFileSync(filePath, fileBuffer); | ||
|
||
var file = JSON.parse(postData); | ||
var fileRootName = file.name.split('.').shift(); | ||
var fileExtension = file.name.split('.').pop(); | ||
var filePathBase = config.upload_dir + '/'; | ||
var fileRootNameWithBase = filePathBase + fileRootName; | ||
var filePath = fileRootNameWithBase + '.' + fileExtension; | ||
var fileID = 2; | ||
var 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'); | ||
var client = knox.createClient(config.s3); | ||
var headers = {'Content-Type': file.type}; | ||
|
||
client.putBuffer(fileBuffer, fileRootName, headers, function (err, res) { | ||
|
||
if ( typeof res !== "undefined" && 200 === res.statusCode ) { | ||
console.log('Uploaded to: %s', res.client._httpMessage.url); | ||
response.statusCode = 200; | ||
response.end(); | ||
} | ||
} else { | ||
console.log('Upload failed!'); | ||
response.statusCode = 500; | ||
} | ||
|
||
response.end(); | ||
}); | ||
|
||
} else { | ||
fs.writeFileSync(filePath, fileBuffer); | ||
response.statusCode = 200; | ||
response.end(); | ||
} | ||
} | ||
|
||
function serveStatic(response, pathname, postData) { | ||
|
||
var extension = pathname.split('.').pop(), | ||
extensionTypes = { | ||
'css' : 'text/css', | ||
'gif' : 'image/gif', | ||
'jpg' : 'image/jpeg', | ||
'jpeg': 'image/jpeg', | ||
'js' : 'application/javascript', | ||
'png' : 'image/png' | ||
}; | ||
response.writeHead(200, {'Content-Type': extensionTypes[extension]}); | ||
response.end(fs.readFileSync('./static' + pathname)); | ||
var extension = pathname.split('.').pop(); | ||
var extensionTypes = { | ||
css: 'text/css', | ||
gif: 'image/gif', | ||
jpg: 'image/jpeg', | ||
jpeg: 'image/jpeg', | ||
js: 'application/javascript', | ||
png: 'image/png' | ||
}; | ||
|
||
response.writeHead(200, {'Content-Type': extensionTypes[extension]}); | ||
response.end(fs.readFileSync('./static' + pathname)); | ||
} | ||
|
||
exports.home = home; | ||
exports.upload = upload; | ||
exports.serveStatic = serveStatic; | ||
module.exports = { | ||
home: home, | ||
serveStatic: serveStatic, | ||
upload: upload | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
var server = require('./server'), | ||
handlers = require('./handlers'), | ||
router = require('./router'), | ||
handle = {}; | ||
var handlers = require('./handlers'); | ||
var router = require('./router'); | ||
var server = require('./server'); | ||
|
||
handle["/"] = handlers.home; | ||
handle["/home"] = handlers.home; | ||
handle["/upload"] = handlers.upload; | ||
handle._static = handlers.serveStatic; | ||
var routes = { | ||
'/': handlers.home, | ||
'/home': handlers.home, | ||
'/upload': handlers.upload, | ||
'_static': handlers.serveStatic | ||
}; | ||
|
||
server.start(router.route, handle); | ||
server.start(router.route, routes); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "simple-file-uploader", | ||
"version": "0.2.0", | ||
"description": "A file uploader written using HTML5 and Node.js. It can upload both to a local directory on the server or to an AWS S3 server.", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"start": "node server.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/merty/simple-file-uploader.git" | ||
}, | ||
"keywords": [ | ||
"aws", | ||
"file", | ||
"html5", | ||
"node", | ||
"nodejs", | ||
"s3", | ||
"upload" | ||
], | ||
"author": "Mert Yazicioglu", | ||
"license": "MIT", | ||
"private": true, | ||
"bugs": { | ||
"url": "https://github.com/merty/simple-file-uploader/issues" | ||
}, | ||
"homepage": "https://github.com/merty/simple-file-uploader#readme", | ||
"dependencies": { | ||
"knox": "^0.9.2" | ||
}, | ||
"scripts": { | ||
"start": "node index" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,21 @@ | ||
function respondWithHTTPCode(response, code) { | ||
response.writeHead(code, {'Content-Type': 'text/plain'}); | ||
response.end(); | ||
response.writeHead(code, {'Content-Type': 'text/plain'}); | ||
response.end(); | ||
} | ||
|
||
function route(handle, pathname, response, postData) { | ||
function route(routes, pathname, response, postData) { | ||
|
||
var extension = pathname.split('.').pop(); | ||
var extension = pathname.split('.').pop(); | ||
|
||
if ('function' === typeof handle[pathname]) { | ||
handle[pathname](response, postData); | ||
} else if ('css' === extension || 'js' === extension) { | ||
handle._static(response, pathname, postData); | ||
} else { | ||
respondWithHTTPCode(response, 404); | ||
} | ||
if ( 'function' === typeof routes[pathname] ) { | ||
routes[pathname](response, postData); | ||
} else if ( 'css' === extension || 'js' === extension ) { | ||
routes._static(response, pathname, postData); | ||
} else { | ||
respondWithHTTPCode(response, 404); | ||
} | ||
} | ||
|
||
exports.route = route; | ||
module.exports = { | ||
route: route | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
var config = require('./config'), | ||
http = require('http'), | ||
url = require('url'); | ||
var config = require('./config'); | ||
var http = require('http'); | ||
var url = require('url'); | ||
|
||
function start(route, handle) { | ||
function start(route, routes) { | ||
|
||
function onRequest(request, response) { | ||
function onRequest(request, response) { | ||
|
||
var pathname = url.parse(request.url).pathname, | ||
postData = ''; | ||
var pathname = url.parse(request.url).pathname; | ||
var postData = ''; | ||
|
||
request.setEncoding('utf8'); | ||
request.setEncoding('utf8'); | ||
|
||
request.addListener('data', function (postDataChunk) { | ||
postData += postDataChunk; | ||
}); | ||
request.addListener('data', function (postDataChunk) { | ||
postData += postDataChunk; | ||
}); | ||
|
||
request.addListener('end', function () { | ||
route(handle, pathname, response, postData); | ||
}); | ||
} | ||
request.addListener('end', function () { | ||
route(routes, pathname, response, postData); | ||
}); | ||
} | ||
|
||
http.createServer(onRequest).listen(config.port); | ||
http.createServer(onRequest).listen(config.port); | ||
console.log('Started HTTP server on port ' + config.port + '...'); | ||
} | ||
|
||
exports.start = start; | ||
module.exports = { | ||
start: start | ||
}; |
Oops, something went wrong.