UNPKG

ozserver

Version:

API for OZ

165 lines (148 loc) 5.63 kB
var EventEmitter, Upload, User, async, exports, gm, mkpath, mongoose, upload, _, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; _ = require('lodash'); gm = require('gm'); async = require('async'); mongoose = require('mongoose'); mkpath = require('mkpath'); EventEmitter = require('events').EventEmitter; upload = require('jquery-file-upload-middleware'); User = mongoose.model('User', require(global.home + '/script/views/db/user')); Upload = (function(_super) { __extends(Upload, _super); function Upload(req, res, next, options) { var _this = this; this.req = req; this.res = res; this.next = next; this.options = options; this.on('send', function() { _this.emit('response', { file: _this.mdl.model, aid: _this.req.query.aid != null ? _this.req.query.aid : null }); return _this.res.json(_this.mdl.model); }); this.on('userNotFound', function() { _this.mdl.emit('userNotFound'); return _this.emit('send'); }); this.on('upload', function() { _this.mdl = require(global.home + '/script/models/upload/file')(); return _this.findUser(function(user) { var uploadDir, uploadUrl; uploadDir = "" + _this.options.uploads_directory + "/" + user.email; uploadUrl = "" + _this.options.uploads_url + "/" + user.email; mkpath.sync("" + uploadDir); mkpath.sync("" + uploadDir + "/thumbnail"); mkpath.sync("" + uploadDir + "/preview"); upload.once('end', function(fileInfo) { var ObjectId, fileDir, fileUrl, previewDir, previewUrl, thumbDir, thumbUrl; fileDir = "" + uploadDir + "/" + fileInfo.name; fileUrl = "" + uploadUrl + "/" + fileInfo.name; ObjectId = mongoose.Types.ObjectId; fileInfo.id = new ObjectId; if (_this.options.uploads_url.toString().match(/^http/)) { fileInfo.url = "" + fileUrl; } fileInfo.delete_url = null; fileInfo.delete_type = null; if (_this.req.method === 'POST') { if (fileInfo.type.match(/^image/)) { thumbDir = "" + uploadDir + "/thumbnail/" + fileInfo.name; thumbUrl = "" + uploadUrl + "/thumbnail/" + fileInfo.name; previewDir = "" + uploadDir + "/preview/" + fileInfo.name; previewUrl = "" + uploadUrl + "/preview/" + fileInfo.name; fileInfo.thumbnail = "" + thumbUrl; fileInfo.preview = "" + previewUrl; } user.files.push(fileInfo); user.save(); _this.emit('response', { file: fileInfo, user: user, aid: _this.req.query.aid != null ? _this.req.query.aid : null }); if (fileInfo.type.match(/^image/)) { return gm("" + fileDir).orientation(function(err, orientation) { return gm("" + fileDir).size(function(err, resolution) { var previewRes; previewRes = _this.crop(resolution.width, resolution.height, 200, 200, orientation); gm("" + fileDir).autoOrient().resize(previewRes.width, previewRes.height).crop(200, 200, previewRes.x, previewRes.y).write("" + previewDir, function(err) { if (err) { throw err; } }); return gm("" + fileDir).autoOrient().thumb(640, 480, "" + thumbDir, 75, function(err) { if (err) { throw err; } }); }); }); } } }); return upload.fileHandler({ maxFileSize: _this.options.max_user_file_size_mb * 1024 * 1024, uploadDir: "" + uploadDir, uploadUrl: "" + uploadUrl })(_this.req, _this.res, _this.next); }); }); } Upload.prototype.crop = function(w, h, need_w, need_h, orientation) { var a, k, k_h, k_w, result; if (orientation == null) { orientation = 'TopLeft'; } result = {}; if (orientation.match(/^Left/ || orientation.match(/^Right/))) { a = w; w = h; h = a; } if (Number(w) > 0) { k_w = Number(need_w) / Number(w); } else { k_w = 0; } if (Number(h) > 0) { k_h = Number(need_h) / Number(h); } else { k_h = 0; } if (k_w >= k_h) { k = k_w; } else { k = k_h; } result.width = Math.ceil(k * w); result.height = Math.ceil(k * h); result.x = Math.ceil((result.width - Number(need_w)) / 2); result.y = Math.ceil((result.height - Number(need_h)) / 2); return result; }; Upload.prototype.findUser = function(callback) { var _this = this; return User.findOne({ id: this.req.query.id != null ? this.req.query.id : '', key: this.req.query.key != null ? this.req.query.key : '', disabled: false }, function(err, user) { if (err || (user == null)) { return _this.emit('userNotFound'); } else { if (callback != null) { return callback(user); } } }); }; return Upload; })(EventEmitter); exports = module.exports = function(req, res, next, options) { return new Upload(req, res, next); }; exports.Upload = Upload;