diff options
author | Houssam Haidar <houssam@sdelements.com> | 2015-02-05 21:25:53 -0500 |
---|---|---|
committer | Houssam Haidar <houssam@sdelements.com> | 2015-02-05 21:25:53 -0500 |
commit | ac9de369d880af9d1ef2c27a6842f43b6690e8bf (patch) | |
tree | 04a5527796f3a79b8b4a91cac1e86eceffd94f8d /app/core/files.js | |
parent | 946a9d5234fe085e9f87805e6eb63224cf8c935c (diff) | |
parent | b68edc02004b5a4ef90d6a19bcee3aaf348a85c4 (diff) | |
download | lets-chat-ac9de369d880af9d1ef2c27a6842f43b6690e8bf.zip lets-chat-ac9de369d880af9d1ef2c27a6842f43b6690e8bf.tar.gz lets-chat-ac9de369d880af9d1ef2c27a6842f43b6690e8bf.tar.bz2 |
Merge branch 'release/0.3.0' of github.com:sdelements/lets-chat into release/0.3.0-uploads
Diffstat (limited to 'app/core/files.js')
-rw-r--r-- | app/core/files.js | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/app/core/files.js b/app/core/files.js index 90584ef..61df225 100644 --- a/app/core/files.js +++ b/app/core/files.js @@ -3,6 +3,7 @@ var fs = require('fs'), _ = require('lodash'), mongoose = require('mongoose'), + helpers = require('./helpers'), settings = require('./../config').files, enabled = settings.enable, provider = _.find([ @@ -42,7 +43,7 @@ FileManager.prototype.create = function(options, cb) { if (room.archived) { return cb('Room is archived.'); } - + new File({ owner: options.owner, name: options.file.originalname, @@ -75,8 +76,18 @@ FileManager.prototype.create = function(options, cb) { }; FileManager.prototype.list = function(options, cb) { + options = options || {}; + + options = helpers.sanitizeQuery(options, { + defaults: { + reverse: true, + take: 500 + }, + maxTake: 5000 + }); + var File = mongoose.model('File'), - User = mongoose.model('User'); + User = mongoose.model('User'); var find = File.find(); @@ -84,10 +95,34 @@ FileManager.prototype.list = function(options, cb) { find.where('room', options.room); } + if (options.from) { + find.where('uploaded').gt(options.from); + } + + if (options.to) { + find.where('uploaded').lte(options.to); + } + + if (options.expand && _.isArray(options.include)) { + var includes = options.expand.split(','); + + if (_.includes(includes, 'owner')) { + find.populate('owner', 'id username displayName email avatar'); + } + } + + if (options.skip) { + find.skip(options.skip); + } + + if (options.reverse) { + find.sort({ 'uploaded': -1 }); + } else { + find.sort({ 'uploaded': 1 }); + } + find - .populate('owner', 'id username displayName email avatar') - .limit(options.limit || 500) - .sort({ 'uploaded': -1 }) + .limit(options.limit) .exec(function(err, files) { if (err) { console.error(err); |