Skip to content

Commit

Permalink
Replaces tabs with spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Pablo Cantero committed Jan 19, 2013
1 parent 23ba0ba commit b0f7ce1
Show file tree
Hide file tree
Showing 14 changed files with 341 additions and 343 deletions.
4 changes: 2 additions & 2 deletions Makefile
Expand Up @@ -18,7 +18,7 @@ progress:
simple:
supervisor examples/simple/app.js

test-unit:
./node_modules/.bin/mocha $(TESTS) --reporter list --compilers coffee:coffee-script
test-unit:
./node_modules/.bin/mocha $(TESTS) --reporter list --compilers coffee:coffee-script

.PHONY: test-unit test compile progress simple
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -14,7 +14,7 @@ or
"node-upload-progress": "latest"
}
# ...

$ npm install

##Usage
Expand All @@ -23,15 +23,15 @@ or

var app = require('http');
var uploadProgress = require('node-upload-progress');

uploadHandler = new uploadProgress.UploadHandler;

The ```uploadHandler.uploadDir``` is the path where the files will be saved. Without this configuration the files will be saved in a path based on ```process.env.TMP```.

uploadHandler.configure(function() {
this.uploadDir = __dirname + '/uploads';
});

app.createServer(function(req, res) {
if (req.url === '/upload') {
uploadHandler.upload(req, res);
Expand All @@ -49,12 +49,12 @@ Configuring the ```uploadHandler.onEnd = customOnEndHandler```, you can write yo
this.uploadDir = "" + __dirname + "/uploads";
this.onEnd = customOnEndHandler;
});
function customOnEndHandler(req, res){

function customOnEndHandler(req, res){
res.writeHead 200, {'Content-Type': 'text/plain'}
res.end('Upload received');
}

app.createServer(function(req, res) {
if (req.url === '/upload') {
uploadHandler.upload(req, res);
Expand Down Expand Up @@ -152,15 +152,15 @@ The upload request is in progress:

$ make simple

Then
Then

open http://localhost:8080

###Progress example

$ make progress

Then
Then

open http://localhost:8080

Expand Down
21 changes: 10 additions & 11 deletions examples/progress/app.coffee
Expand Up @@ -4,19 +4,18 @@ uploadProgress = require '../../lib/node-upload-progress'
uploadHandler = new uploadProgress.UploadHandler

uploadHandler.configure ->
this.uploadDir = "#{__dirname}/uploads"
this.uploadDir = "#{__dirname}/uploads"

app.createServer((req, res) ->
if req.url.match /\/upload\?X\-Progress\-ID=.+/
uploadHandler.upload req, res
else if req.url.match /\/progress\?X\-Progress\-ID=.+/
uploadHandler.progress req, res
else
fs.readFile "#{__dirname}/index.html", (err, data) ->
res.writeHead 200, {'Content-Type': 'text/html'}
res.write data.toString()
res.end()
if req.url.match /\/upload\?X\-Progress\-ID=.+/
uploadHandler.upload req, res
else if req.url.match /\/progress\?X\-Progress\-ID=.+/
uploadHandler.progress req, res
else
fs.readFile "#{__dirname}/index.html", (err, data) ->
res.writeHead 200, {'Content-Type': 'text/html'}
res.write data.toString()
res.end()
).listen(8080)

console.log 'Server running at http://localhost:8080/'

112 changes: 56 additions & 56 deletions examples/progress/index.html
@@ -1,62 +1,62 @@
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Upload</title>
<meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!-- Date: 2012-06-12 -->
<script>
$(function(){
var uploadIntervalID;
$('#form_upload').submit(function(){
// Preventing multiples clicks on upload
clearInterval(uploadIntervalID);
var xProgressID = guidGenerator();
$(this).attr('action', '/upload?X-Progress-ID=' + xProgressID);
uploadIntervalID = setInterval(function(){
$.get('/progress?X-Progress-ID=' + xProgressID, function(data){
if(data.status === 'done'){
clearInterval(uploadIntervalID);
}
updateUploadStatus(data);
}).error(function(){clearInterval(uploadIntervalID)});
}, 250);
return true;
});
function updateUploadStatus(data){
$('#upload_percent').text(data.percent);
$('#upload_status').text(data.status);
$('#upload_filename').text(data.fileName);
$('#upload_filepath').text(data.filePath);
}
// http://stackoverflow.com/a/105074/464685
function guidGenerator() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}
function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
});
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Upload</title>
<meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!-- Date: 2012-06-12 -->
<script>
$(function(){
var uploadIntervalID;
$('#form_upload').submit(function(){
// Preventing multiples clicks on upload
clearInterval(uploadIntervalID);
var xProgressID = guidGenerator();
$(this).attr('action', '/upload?X-Progress-ID=' + xProgressID);
uploadIntervalID = setInterval(function(){
$.get('/progress?X-Progress-ID=' + xProgressID, function(data){
if(data.status === 'done'){
clearInterval(uploadIntervalID);
}
updateUploadStatus(data);
}).error(function(){clearInterval(uploadIntervalID)});
}, 250);
return true;
});

function updateUploadStatus(data){
$('#upload_percent').text(data.percent);
$('#upload_status').text(data.status);
$('#upload_filename').text(data.fileName);
$('#upload_filepath').text(data.filePath);
}

// http://stackoverflow.com/a/105074/464685
function guidGenerator() {
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4());
}

function S4() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1);
}
});
</script>
</head>
<body>
<h1>Super Upload</h1>
<form action="/upload?X-Progress-ID=1" enctype="multipart/form-data" method="post" id="form_upload" target="iframe_upload">
<p>
<label>File</label><br/>
<input type="file" name="upload" id="upload"><br>
<span id="upload_percent"></span><br/>
<span id="upload_status"></span><br/>
<span id="upload_filename"></span><br/>
<span id="upload_filepath"></span>
</p>
<p>
<input type="submit" value="Upload">
</p>
</form>
<iframe id="iframe_upload" name="iframe_upload"></iframe>
<h1>Super Upload</h1>
<form action="/upload?X-Progress-ID=1" enctype="multipart/form-data" method="post" id="form_upload" target="iframe_upload">
<p>
<label>File</label><br/>
<input type="file" name="upload" id="upload"><br>
<span id="upload_percent"></span><br/>
<span id="upload_status"></span><br/>
<span id="upload_filename"></span><br/>
<span id="upload_filepath"></span>
</p>
<p>
<input type="submit" value="Upload">
</p>
</form>
<iframe id="iframe_upload" name="iframe_upload"></iframe>
</body>
</html>
17 changes: 8 additions & 9 deletions examples/simple/app.coffee
Expand Up @@ -4,17 +4,16 @@ uploadProgress = require '../../lib/node-upload-progress'
uploadHandler = new uploadProgress.UploadHandler

uploadHandler.configure ->
this.uploadDir = "#{__dirname}/uploads"
this.uploadDir = "#{__dirname}/uploads"

app.createServer((req, res) ->
if req.url == '/upload'
uploadHandler.upload req, res
else
fs.readFile "#{__dirname}/index.html", (err, data) ->
res.writeHead 200, {'Content-Type': 'text/html'}
res.write data.toString()
res.end()
if req.url == '/upload'
uploadHandler.upload req, res
else
fs.readFile "#{__dirname}/index.html", (err, data) ->
res.writeHead 200, {'Content-Type': 'text/html'}
res.write data.toString()
res.end()
).listen(8080)

console.log 'Server running at http://localhost:8080/'

28 changes: 14 additions & 14 deletions examples/simple/index.html
@@ -1,20 +1,20 @@
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Upload</title>
<meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
<!-- Date: 2012-06-12 -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Test Upload</title>
<meta name="author" content="Pablo Cantero <pablo@pablocantero.com>">
<!-- Date: 2012-06-12 -->
</head>
<body>
<h1>Super Upload</h1>
<form action="/upload" enctype="multipart/form-data" method="post" id="form_upload">
<p>
<label>File</label><br/>
<input type="file" name="upload" id="upload">
</p>
<p>
<input type="submit" value="Upload">
</p>
</form>
<h1>Super Upload</h1>
<form action="/upload" enctype="multipart/form-data" method="post" id="form_upload">
<p>
<label>File</label><br/>
<input type="file" name="upload" id="upload">
</p>
<p>
<input type="submit" value="Upload">
</p>
</form>
</body>
</html>
84 changes: 42 additions & 42 deletions lib/node-upload-progress.coffee
Expand Up @@ -19,49 +19,49 @@ fs = require 'fs'
# new Object({ 'state' : 'uploading', 'received' : <size_received>, 'size' : <total_size>}) }

class UploadHandler
constructor: (@uploadDir=null, @onEnd=null) ->
@uploads = new Uploads

configure: (func) ->
func.call @

formOnFile: (upload, field, file) ->
upload.file = file
if @uploadDir
fs.rename file.path, "#{@uploadDir}/#{file.name}"
file.path = "#{@uploadDir}/#{file.name}"

formOnProgress: (upload, bytesReceived, bytesExpected) ->
upload.updateProgress bytesReceived, bytesExpected

_onEnd: (req, res) ->
res.writeHead 200, 'Content-type': 'text/plain'
res.end 'upload received'
constructor: (@uploadDir=null, @onEnd=null) ->
@uploads = new Uploads

upload: (req, res) ->
query = url.parse(req.url, true).query
form = new formidable.IncomingForm()

@uploads.add query['X-Progress-ID']

form.parse req, (err, fields, files) =>
@uploads.remove query['X-Progress-ID']
(@onEnd || @_onEnd)(req, res)

form.on 'file', (field, file) =>
@formOnFile @uploads.get(query['X-Progress-ID']), field, file

form.addListener 'progress' , (bytesReceived, bytesExpected) =>
@formOnProgress @uploads.get(query['X-Progress-ID']), bytesReceived, bytesExpected
configure: (func) ->
func.call @

progress: (req, res) ->
query = url.parse(req.url, true).query
upload = @uploads.get query['X-Progress-ID']
if upload
res.writeHead 200, 'Content-type': 'application/json'
res.end upload.toJSON()
else
res.writeHead 404, 'Content-type': 'text/plain'
res.end 'not found'
formOnFile: (upload, field, file) ->
upload.file = file
if @uploadDir
fs.rename file.path, "#{@uploadDir}/#{file.name}"
file.path = "#{@uploadDir}/#{file.name}"

formOnProgress: (upload, bytesReceived, bytesExpected) ->
upload.updateProgress bytesReceived, bytesExpected

_onEnd: (req, res) ->
res.writeHead 200, 'Content-type': 'text/plain'
res.end 'upload received'

upload: (req, res) ->
query = url.parse(req.url, true).query
form = new formidable.IncomingForm()

@uploads.add query['X-Progress-ID']

form.parse req, (err, fields, files) =>
@uploads.remove query['X-Progress-ID']
(@onEnd || @_onEnd)(req, res)

form.on 'file', (field, file) =>
@formOnFile @uploads.get(query['X-Progress-ID']), field, file

form.addListener 'progress' , (bytesReceived, bytesExpected) =>
@formOnProgress @uploads.get(query['X-Progress-ID']), bytesReceived, bytesExpected

progress: (req, res) ->
query = url.parse(req.url, true).query
upload = @uploads.get query['X-Progress-ID']
if upload
res.writeHead 200, 'Content-type': 'application/json'
res.end upload.toJSON()
else
res.writeHead 404, 'Content-type': 'text/plain'
res.end 'not found'

module.exports.UploadHandler = UploadHandler

0 comments on commit b0f7ce1

Please sign in to comment.