Skip to content

Instantly share code, notes, and snippets.

@tkh44
Last active March 10, 2020 09:04

Revisions

  1. tkh44 revised this gist Jan 3, 2014. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions test.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    <form id="uploadForm"
    enctype="multipart/form-data"
    action="/upload/upload"
    method="post">
    <input type="file" id="userPhotoInput" name="userPhoto" />
    </form>
  2. tkh44 renamed this gist Jan 2, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. tkh44 renamed this gist Jan 2, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. tkh44 revised this gist Jan 2, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion UploadController.js
    Original file line number Diff line number Diff line change
    @@ -26,7 +26,8 @@ function fileExtension(fileName) {
    return fileName.split('.').slice(-1);
    }


    // Where you would do your processing, etc
    // Stubbed out for now
    function processImage(id, name, path, cb) {
    console.log('Processing image');

  5. tkh44 revised this gist Jan 2, 2014. 3 changed files with 23 additions and 6 deletions.
    9 changes: 9 additions & 0 deletions FileController.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    module.exports = {
    get: function (req, res) {
    res.sendfile(req.path.substr(1));
    },
    _config: {
    rest: false,
    shortcuts: false
    }
    };
    10 changes: 4 additions & 6 deletions UploadController.js
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,8 @@ var fs = require('fs');
    var mkdirp = require('mkdirp');
    //var io = require('socket.io');

    var UPLOAD_PATH = 'public/images';

    // Setup id generator
    sid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
    sid.seed(42);
    @@ -42,8 +44,7 @@ module.exports = {
    var file = req.files.userPhoto,
    id = sid.generate(),
    fileName = id + "." + fileExtension(safeFilename(file.name)),
    size = parseInt(req.headers['content-length'], 10),
    dirPath = 'public/gifs/' + id,
    dirPath = UPLOAD_PATH + '/' + id,
    filePath = dirPath + '/' + fileName;

    try {
    @@ -76,8 +77,5 @@ module.exports = {
    * Overrides for the settings in `config/controllers.js`
    * (specific to GifController)
    */
    _config: {
    rest: false,
    shortcuts: false
    }
    _config: {}
    };
    10 changes: 10 additions & 0 deletions routes.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    module.exports.routes = {
    '/': {
    view: 'home/index'
    },

    'get /public/images/*': {
    controller: 'FileController',
    action: 'get'
    }
    };
  6. tkh44 revised this gist Jan 2, 2014. 2 changed files with 10 additions and 2 deletions.
    5 changes: 4 additions & 1 deletion controller.js → UploadController.js
    Original file line number Diff line number Diff line change
    @@ -76,5 +76,8 @@ module.exports = {
    * Overrides for the settings in `config/controllers.js`
    * (specific to GifController)
    */
    _config: {}
    _config: {
    rest: false,
    shortcuts: false
    }
    };
    7 changes: 6 additions & 1 deletion setup.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,9 @@
    Setup
    -----

    cd /your/project/dir

    sails generate controller upload upload

    sails generate file get

    npm install shortid mkdirp -D
  7. tkh44 revised this gist Jan 2, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion setup.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    setup
    Setup
    -----

    npm install shortid mkdirp -D
  8. tkh44 revised this gist Jan 2, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion setup.md
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    setup
    -----

    npm install shortid mkdirp -D
    npm install shortid mkdirp -D
  9. tkh44 revised this gist Jan 2, 2014. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions setup.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    setup
    -----

    npm install shortid mkdirp -D
  10. tkh44 revised this gist Jan 2, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions controller.js
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,5 @@
    var sid = require('shortid');

    var fs = require('fs');
    var path = require('path');
    var mkdirp = require('mkdirp');
    //var io = require('socket.io');

  11. tkh44 revised this gist Jan 2, 2014. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions controller.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,4 @@
    var sid = require('shortid');
    var _ = require('underscore');
    _.str = require('underscore.string');

    var fs = require('fs');
    var path = require('path');
  12. tkh44 created this gist Jan 2, 2014.
    84 changes: 84 additions & 0 deletions controller.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,84 @@
    var sid = require('shortid');
    var _ = require('underscore');
    _.str = require('underscore.string');

    var fs = require('fs');
    var path = require('path');
    var mkdirp = require('mkdirp');
    //var io = require('socket.io');

    // Setup id generator
    sid.characters('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ$@');
    sid.seed(42);

    function safeFilename(name) {
    name = name.replace(/ /g, '-');
    name = name.replace(/[^A-Za-z0-9-_\.]/g, '');
    name = name.replace(/\.+/g, '.');
    name = name.replace(/-+/g, '-');
    name = name.replace(/_+/g, '_');
    return name;
    }

    function fileMinusExt(fileName) {
    return fileName.split('.').slice(0, -1).join('.');
    }

    function fileExtension(fileName) {
    return fileName.split('.').slice(-1);
    }


    function processImage(id, name, path, cb) {
    console.log('Processing image');

    cb(null, {
    'result': 'success',
    'id': id,
    'name': name,
    'path': path
    });
    }


    module.exports = {
    upload: function (req, res) {
    var file = req.files.userPhoto,
    id = sid.generate(),
    fileName = id + "." + fileExtension(safeFilename(file.name)),
    size = parseInt(req.headers['content-length'], 10),
    dirPath = 'public/gifs/' + id,
    filePath = dirPath + '/' + fileName;

    try {
    mkdirp.sync(dirPath, 0755);
    } catch (e) {
    console.log(e);
    }

    fs.readFile(file.path, function (err, data) {
    if (err) {
    res.json({'error': 'could not read file'});
    } else {
    fs.writeFile(filePath, data, function (err) {
    if (err) {
    res.json({'error': 'could not write file to storage'});
    } else {
    processImage(id, fileName, filePath, function (err, data) {
    if (err) {
    res.json(err);
    } else {
    res.json(data);
    }
    });
    }
    })
    }
    });
    },
    /**
    * Overrides for the settings in `config/controllers.js`
    * (specific to GifController)
    */
    _config: {}
    };