Skip to content
This repository
Browse code

Implement server-side 'Upload Pictures' functions.

This commit contains server-side functions for uploading pictures
except image processing or socket emits.
  • Loading branch information...
commit a1a9de2f57d395fc1845b3f4b4f210ad59e0958f 1 parent 115598d
Hyeonje Alex Jun authored November 13, 2012
2  .gitignore
@@ -15,3 +15,5 @@ node_modules
15 15
 npm-debug.log
16 16
 
17 17
 .DS_Store
  18
+
  19
+uploads
57  server/handlers.js
... ...
@@ -1,4 +1,6 @@
1 1
 var settings = require('../settings'),
  2
+    path = require('path'),
  3
+    fs = require('fs'),
2 4
     mongoose = require('mongoose');
3 5
 
4 6
 // mongoose models
@@ -26,9 +28,58 @@ function Handlers(web) {
26 28
             });
27 29
         },
28 30
         upload: function(req, res) {
29  
-            // TODO: upload a picture
30  
-            res.set('Content-Type', 'text/plain');
31  
-            res.send('success');
  31
+            var image = req.files.picture_uploaded,
  32
+                upload_dir = path.join(__dirname, '../uploads'),
  33
+                channel = req.params.name;
  34
+
  35
+            var response = function(text) {
  36
+                res.set('Content-Type', 'text/plain');
  37
+                res.send(text);
  38
+            };
  39
+
  40
+            fs.readFile(image.path, function(err, data) {
  41
+                if(err) {
  42
+                    response('read_error:'+err);
  43
+                    return;
  44
+                }
  45
+
  46
+                // create new picture
  47
+                picture = new PictureModel({channel: channel});
  48
+
  49
+                var picture_dir = path.join(upload_dir, ''+picture._id);
  50
+                fs.mkdir(picture_dir, '0775', function(err) {
  51
+                    if(err) {
  52
+                        response('mkdir_error:'+err);
  53
+                        return;
  54
+                    }
  55
+
  56
+                    var picture_file = path.join(picture_dir, image.name);
  57
+                    fs.writeFile(picture_file, data, function(err) {
  58
+                        if(err) {
  59
+                            response('write_error:'+err);
  60
+                            return;
  61
+                        }
  62
+
  63
+                        // remove tmp file
  64
+                        fs.unlink(image.path, function(err) {
  65
+                            if(err) {
  66
+                                response('unlink_error:'+err);
  67
+                                return;
  68
+                            }
  69
+
  70
+                            picture.original = picture_file;
  71
+                            picture.uploaded = Date.now();
  72
+                            picture.save(function(err) {
  73
+                                if(err) {
  74
+                                    response('save_error:'+err);
  75
+                                }
  76
+
  77
+                                response('success');
  78
+                            });
  79
+                        });
  80
+                    });
  81
+                });
  82
+            });
32 83
         }
33 84
     };
34 85
 }
1  server/models.js
@@ -3,6 +3,7 @@ var mongoose = require('mongoose'),
3 3
 
4 4
 module.exports = function() {
5 5
     var Pictures = new Schema({
  6
+        original: String,
6 7
         channel: String,
7 8
         width: Number,
8 9
         height: Number,
1  server/web.js
@@ -13,6 +13,7 @@ function Web() {
13 13
 
14 14
     app.configure(function() {
15 15
         var basePath = path.join(__dirname, '..');
  16
+        app.use(express.bodyParser({uploadDir: path.join(basePath, './uploads')}));
16 17
         app.use(require('connect-assets')({build: false, src: path.join(basePath, 'client')}));
17 18
         app.use('/images', express['static'](path.join(basePath, 'client/images')));
18 19
         app.set('views', path.join(basePath, 'views'));

0 notes on commit a1a9de2

Please sign in to comment.
Something went wrong with that request. Please try again.