forked from jodosha/node-upload-progress
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
192 lines (156 loc) · 5.11 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/**
* Module dependencies.
*/
require.paths.unshift(__dirname + '/vendor/');
var express = require('express'),
keys = require('keys'),
fs = require('fs'),
formidable = require('formidable');
var util = require('util');
var app = module.exports = express.createServer();
app.store = new keys.Memory();
// Configuration
app.configure(function( ){
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function( ){
app.use(express.logger());
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function( ){
app.use(express.logger());
app.use(express.errorHandler());
});
// Routes
app.get('/', function( req, res ){
res.render('new');
});
app.post('/songs', function( req, res ){
res.render('create', {
path: req.body.song.path,
description: req.body.song.description
});
});
app.post('/upload', function( req, res ){
uploadFile(req, res);
});
app.get('/uploads/:uid', function( req, res ){
readSession(req, 'progress', function( err, buf ){
if ( buf ) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify({ progress: buf.toString() }));
res.end();
} else {
writeSession(req, 'progress', '0');
readSession(req, 'progress', function( err, buf ){
if ( buf ) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.write(JSON.stringify({ progress: buf.toString() }));
res.end();
} else {
res.send('Not Found', { 'Content-Type': 'text/plain' }, 404);
}
});
}
});
});
// Upload
function uploadFile( req, res ) {
util.debug('this is a form request');
//setup this form receiver
var form = new formidable.IncomingForm();
var bytesTotal = req.headers['content-length'],
bytesReceived = 0,
fileName = null,
fileStream = null;
form.encoding = 'utf-8';
form.keepExtensions = true; //keep extensions
form.maxFieldsSize = 0.5 * 1024 * 1024; //max memory allocated of a field in bytes
/*
form.on('field', function(name, value) {
util.debug('received field: ' + name + ' = ' + value);
});
form.on('fileBegin', function(name, file) {
util.debug('fileBegin');
});
form.on('error', function(err) {
util.debug('error: ' + err);
});
form.on('end', function() {
util.debug('stream end.');
});
*/
//handle every part
form.onPart = function(part) {
var dirName = createUploadDirectory(req);
fileName = part.filename.split(/\\/).reverse()[0];
fileName = dirName + '/' + fileName;
fileStream = fs.createWriteStream(fileName);
writeSession(req, 'path', fileName.replace('./public', ''));
fileStream.addListener("error", function(err) {
util.debug(err);
});
fileStream.addListener("drain", function() {
req.resume();
});
//handle chunks of files
part.on('data', function(chunk) {
//util.debug('received / expected: ' + form.bytesReceived + ' / ' + form.bytesExpected + ' (after chunk, size: ' + chunk.length);
req.pause();
fileStream.write(chunk, 'binary');
bytesReceived += chunk.length;
var progress = Math.round( (bytesReceived / bytesTotal * 100) ).toString();
writeSession(req, 'progress', progress);
});
//handle part end
part.on('end', function() {
util.debug('part end.');
fileStream.end();
req.resume();
writeSession(req, 'progress', '100');
uploadComplete(req, res);
});
//form.handlePart(part);
};
//start parsing this stream
form.parse(req);
}
function createUploadDirectory( req ) {
var uid = req.param('uid'),
dirName = "./public/songs/" + uid;
req.pause();
fs.mkdir(dirName, '744', function( ) { req.resume(); });
return dirName;
}
function writeSession( req, key, value ) {
app.store.set(sessionKeyFor(req, key), value);
}
function readSession( req, key, fn ) {
app.store.get(sessionKeyFor(req, key), fn);
}
function clearSession( req ) {
app.store.clear();
}
function sessionKeyFor( req, key ) {
var uid = req.param('uid');
return 'uploads.' + uid + '.' + key;
}
function uploadComplete( req, res ) {
readSession(req, 'path', function( err, buf ){
res.render('uploaded', {
layout: false,
path: buf.toString()
});
});
}
// Only listen on $ node app.js
if (!module.parent) {
app.listen(3000);
console.log("Express server listening on port %d", app.address().port);
}