Permalink
Browse files

bug(server): upload solved

  • Loading branch information...
1 parent afb880a commit df238ece2a216f26e5ed61b574884e55e3e5891a @namjul committed Jun 1, 2011
Showing with 76 additions and 80 deletions.
  1. +57 −79 app.js
  2. +14 −1 controllers/websockets.js
  3. +5 −0 test/test.upload.html
View
136 app.js
@@ -40,7 +40,7 @@ app.configure('test', function() {
});
app.configure('development', function(){
- app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
+ app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
app.set('db-uri', 'mongodb://localhost/sharedesk-development');
app.model = new model('sharedesk-development', function() {});
});
@@ -142,106 +142,84 @@ app.post('/upload/:deskname/:filesgroupid', function(req, res) {
var rcvd_bytes_complete = 0;
var basedir = './uploads/';
- // check directory, create if it not exists
+ var form = new formidable.IncomingForm(),
+ files = [],
+ fields = [];
+
+ var oldProgressPercentage = 0;
var dir = basedir + req.params.deskname;
- fs.stat(dir, function(error, stats) {
- if(typeof stats=='undefined' || !stats.isDirectory()) {
- // create desk dir
- fs.mkdir(dir, 448, function(error) {
- // if error, create first upload dir
- if (error) {
- fs.mkdir(basedir, 448, function(error) {
-
- if(error) {
- console.log('could not create upload folder');
- }
- else {
-
- fs.mkdir(dir, 448, function(error) {
- if (error) {
- console.log(error);
- }
- else {
- run('upload/desk');
- }
- });
- }
- });
- }
- else {
- run('desk');
+
+ form.uploadDir = dir;
+
+ // send progress, minimum one percent
+ form.on('progress', function(bytesReceived, bytesExpected) {
+ console.log('process');
+ var newProgressPercentage = (bytesReceived / bytesExpected) * 100 | 0;
+ if(oldProgressPercentage < newProgressPercentage) {
+ var msg = {
+ action: 'progress',
+ data: {
+ filesgroupid: filesgroupid,
+ bytesReceived: bytesReceived,
+ bytesExpected: bytesExpected
}
- });
- }
- else {
- run('none');
+ }
+ rooms.broadcast_room(req.params.deskname, msg);
+ oldProgressPercentage = newProgressPercentage;
}
});
- var run = function(msg) {
-
- console.log(msg);
- var form = new formidable.IncomingForm(),
- files = [],
- fields = [];
-
- var oldProgressPercentage = 0;
-
- form.uploadDir = dir;
+ form.on('file', function(name, file) {
+ console.log('file');
+ var fileModel = {
+ name: file.name,
+ location: file.path,
+ x: -1,
+ y: -1,
+ format: file.type
+ }
- // send progress, minimum one percent
- form.on('progress', function(bytesReceived, bytesExpected) {
- var newProgressPercentage = (bytesReceived / bytesExpected) * 100 | 0;
- if(oldProgressPercentage < newProgressPercentage) {
+ app.model.createFile(req.params.deskname, fileModel, function(error, db_file) {
+ if (error) console.log(error);
+ else {
var msg = {
- action: 'progress',
+ action: 'createFile',
data: {
filesgroupid: filesgroupid,
- bytesReceived: bytesReceived,
- bytesExpected: bytesExpected
+ file: fileModel
}
}
rooms.broadcast_room(req.params.deskname, msg);
- oldProgressPercentage = newProgressPercentage;
}
});
+ });
- form.on('file', function(name, file) {
- var fileModel = {
- name: file.name,
- location: file.path,
- x: -1,
- y: -1,
- format: file.type
- }
+ form.parse(req, function(error, fields, files) {
- app.model.createFile(req.params.deskname, fileModel, function(error, db_file) {
- if (error) console.log(error);
- else {
- var msg = {
- action: 'createFile',
- data: {
- filesgroupid: filesgroupid,
- file: fileModel
- }
- }
- rooms.broadcast_room(req.params.deskname, msg);
- }
- });
- });
+ res.writeHead(200, {'content-type': 'text/plain'});
+ res.write('received upload:\n\n');
+ res.end(util.inspect({fields: fields, files: files}));
+
+ });
- form.parse(req, function(error, fields, files) {
- res.writeHead(200, {'content-type': 'text/plain'});
- console.log("file received");
- });
- };
});
-// start websockets controller
-require('./controllers/websockets')(app);
+//create Upload folder if not exists
+var uploadFolder = './uploads/';
+app.uploadFolder = uploadFolder;
+fs.stat(uploadFolder, function(error, stats) {
+ if(typeof stats=='undefined' || !stats.isDirectory()) {
+ fs.mkdir(uploadFolder, 448, function(error) {
+ if (error) throw new Error('could not create ' + uploadFolder + ' folder');
+ });
+ }
+});
+
+// start websockets controller
+require('./controllers/websockets')(app);
// Only listen on $ node app.js
if (!module.parent) {
View
15 controllers/websockets.js
@@ -4,7 +4,9 @@
///////////////////////////////////////////
var io = require('socket.io'),
- util = require('util');
+ util = require('util'),
+ fs = require('fs');
+
module.exports = function(app) {
@@ -103,6 +105,17 @@ module.exports = function(app) {
msg.data = { sid: client.sessionId, user_name: client.user_name };
rooms.add_to_room_and_announce(client, room, msg);
+
+ //create desk upload folder
+ var dir = app.uploadFolder + '/' + room;
+ fs.stat(dir, function(error, stats) {
+ if(typeof stats=='undefined' || !stats.isDirectory()) {
+ fs.mkdir(dir, 448, function(error) {
+ if (error) throw new Error('could not create ' + app.uploadFolder + ' folder');
+ });
+ }
+ });
+
successFunction();
}
View
5 test/test.upload.html
@@ -0,0 +1,5 @@
+<form action="http://localhost:8081/upload/test/2342" enctype="multipart/form-data" method="post">
+ <input type="file" name="upload" multiple="multiple"><br>
+ <input type="submit" value="Upload">
+</form>'
+

0 comments on commit df238ec

Please sign in to comment.