Skip to content

Commit

Permalink
Extract prepost functionality to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
creynders committed Mar 14, 2015
1 parent 23d5ff2 commit c5c274f
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 172 deletions.
29 changes: 5 additions & 24 deletions fields/types/azurefile/AzureFileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
var _ = require('underscore'),
moment = require('moment'),
keystone = require('../../../'),
async = require('async'),
util = require('util'),
azure = require('azure'),
utils = require('keystone-utils'),
prepost = require("../../../lib/prepost"),
super_ = require('../Type');


Expand All @@ -19,15 +19,12 @@ var _ = require('underscore'),
*/

function azurefile(list, path, options) {
prepost.mixin(this)
.register("pre:upload");

this._underscoreMethods = ['format', 'uploadFile'];
this._fixedSize = 'full';

// event queues
this._pre = {
upload: []
};

// TODO: implement filtering, usage disabled for now
options.nofilter = true;

Expand Down Expand Up @@ -56,7 +53,7 @@ function azurefile(list, path, options) {

// Could be more pre- hooks, just upload for now
if (options.pre && options.pre.upload) {
this._pre.upload = this._pre.upload.concat(options.pre.upload);
this.pre("upload", options.pre.upload);
}

}
Expand All @@ -76,22 +73,6 @@ Object.defineProperty(azurefile.prototype, 'azurefileconfig', { get: function()
}});


/**
* Allows you to add pre middleware after the field has been initialised
*
* @api public
*/

azurefile.prototype.pre = function(event, fn) {
if (!this._pre[event]) {
throw new Error('AzureFile (' + this.list.key + '.' + this.path + ') error: azurefile.pre()\n\n' +
'Event ' + event + ' is not supported.\n');
}
this._pre[event].push(fn);
return this;
};


/**
* Registers the field on the List's Mongoose Schema.
*
Expand Down Expand Up @@ -287,7 +268,7 @@ azurefile.prototype.uploadFile = function(item, file, update, callback) {
});
};

async.eachSeries(this._pre.upload, function(fn, next) {
this.hooks("pre:upload", function(fn, next) {
fn(item, file, next);
}, function(err) {
if (err) return callback(err);
Expand Down
54 changes: 7 additions & 47 deletions fields/types/localfile/LocalFileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var fs = require('fs-extra'),
path = require('path'),
_ = require('underscore'),
moment = require('moment'),
async = require('async'),
prepost = require('../../../lib/prepost'),
util = require('util'),
utils = require('keystone-utils'),
super_ = require('../Type');
Expand All @@ -18,19 +18,11 @@ var fs = require('fs-extra'),
*/

function localfile(list, path, options) {

prepost.mixin(this)
.register("pre:move", "post:move");
this._underscoreMethods = ['format', 'uploadFile'];
this._fixedSize = 'full';

// event queues
this._pre = {
move: [] // Before file is moved into final destination
};

this._post = {
move: [] // After file is moved into final destination
};

// TODO: implement filtering, usage disabled for now
options.nofilter = true;

Expand All @@ -51,14 +43,13 @@ function localfile(list, path, options) {
throw new Error('Invalid Configuration\n\n' +
'localfile fields (' + list.key + '.' + path + ') require the "dest" option to be set.');
}

// Allow hook into before and after
if (options.pre && options.pre.move) {
this._pre.move = this._pre.move.concat(options.pre.move);
this.pre("move", options.pre.move);
}

if (options.post && options.post.move) {
this._post.move = this._post.move.concat(options.post.move);
this.post("move", options.post.move);
}

}
Expand All @@ -70,37 +61,6 @@ function localfile(list, path, options) {
util.inherits(localfile, super_);


/**
* Allows you to add pre middleware after the field has been initialised
*
* @api public
*/

localfile.prototype.pre = function(event, fn) {
if (!this._pre[event]) {
throw new Error('localfile (' + this.list.key + '.' + this.path + ') error: localfile.pre()\n\n' +
'Event ' + event + ' is not supported.\n');
}
this._pre[event].push(fn);
return this;
};


/**
* Allows you to add post middleware after the field has been initialised
*
* @api public
*/

localfile.prototype.post = function(event, fn) {
if (!this._post[event]) {
throw new Error('localfile (' + this.list.key + '.' + this.path + ') error: localfile.post()\n\n' +
'Event ' + event + ' is not supported.\n');
}
this._post[event].push(fn);
return this;
};


/**
* Registers the field on the List's Mongoose Schema.
Expand Down Expand Up @@ -331,7 +291,7 @@ localfile.prototype.uploadFile = function(item, file, update, callback) {
});
};

async.eachSeries(this._pre.move, function(fn, next) {
field.hooks("pre:move", function(fn, next) {
fn(item, file, next);
}, function(err) {

Expand All @@ -340,7 +300,7 @@ localfile.prototype.uploadFile = function(item, file, update, callback) {
doMove(function(err, fileData) {
if (err) return callback(err);

async.eachSeries(field._post.move, function(fn, next) {
field.hooks("post:move", function(fn, next) {
fn(item, file, fileData, next);
}, function(err) {
if (err) return callback(err);
Expand Down
55 changes: 8 additions & 47 deletions fields/types/localfiles/LocalFilesType.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var fs = require('fs-extra'),
util = require('util'),
utils = require('keystone-utils'),
super_ = require('../Type'),
async = require('async');
async = require('async'),
prepost = require('../../../lib/prepost');

/**
* localfiles FieldType Constructor
Expand All @@ -19,19 +20,11 @@ var fs = require('fs-extra'),
*/

function localfiles(list, path, options) {

prepost.mixin(this)
.register("pre:move", "post:move");
this._underscoreMethods = ['format', 'uploadFiles'];
this._fixedSize = 'full';

// event queues
this._pre = {
move: [] // Before file is moved into final destination
};

this._post = {
move: [] // After file is moved into final destination
};

// TODO: implement filtering, usage disabled for now
options.nofilter = true;

Expand All @@ -55,11 +48,11 @@ function localfiles(list, path, options) {

// Allow hook into before and after
if (options.pre && options.pre.move) {
this._pre.move = this._pre.move.concat(options.pre.move);
this.pre("move", options.pre.move);
}

if (options.post && options.post.move) {
this._post.move = this._post.move.concat(options.post.move);
this.post("move", options.post.move);
}

}
Expand All @@ -71,38 +64,6 @@ function localfiles(list, path, options) {
util.inherits(localfiles, super_);


/**
* Allows you to add pre middleware after the field has been initialised
*
* @api public
*/

localfiles.prototype.pre = function(event, fn) {
if (!this._pre[event]) {
throw new Error('localfiles (' + this.list.key + '.' + this.path + ') error: localfiles.pre()\n\n' +
'Event ' + event + ' is not supported.\n');
}
this._pre[event].push(fn);
return this;
};


/**
* Allows you to add post middleware after the field has been initialised
*
* @api public
*/

localfiles.prototype.post = function(event, fn) {
if (!this._post[event]) {
throw new Error('localfiles (' + this.list.key + '.' + this.path + ') error: localfiles.post()\n\n' +
'Event ' + event + ' is not supported.\n');
}
this._post[event].push(fn);
return this;
};


/**
* Registers the field on the List's Mongoose Schema.
*
Expand Down Expand Up @@ -354,15 +315,15 @@ localfiles.prototype.uploadFiles = function(item, files, update, callback) {

};

async.eachSeries(field._pre.move, function(fn, next) {
field.hooks("pre:move", function(fn, next) {
fn(item, file, next);
}, function(err) {
if (err) return processedFile(err);

doMove(function(err, fileData) {
if (err) return processedFile(err);

async.eachSeries(field._post.move, function(fn, next) {
field.hooks("post:move", function(fn, next) {
fn(item, file, fileData, next);
}, function(err) {
return processedFile(err, fileData);
Expand Down
30 changes: 5 additions & 25 deletions fields/types/s3file/S3FileType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
var _ = require('underscore'),
moment = require('moment'),
keystone = require('../../../'),
async = require('async'),
util = require('util'),
knox = require('knox'),
// s3 = require('s3'),
utils = require('keystone-utils'),
prepost = require('../../../lib/prepost'),
super_ = require('../Type');

/**
Expand All @@ -19,15 +19,11 @@ var _ = require('underscore'),
*/

function s3file(list, path, options) {

prepost.mixin(this)
.register("pre:upload");
this._underscoreMethods = ['format', 'uploadFile'];
this._fixedSize = 'full';

// event queues
this._pre = {
upload: []
};

// TODO: implement filtering, usage disabled for now
options.nofilter = true;

Expand All @@ -48,7 +44,7 @@ function s3file(list, path, options) {

// Could be more pre- hooks, just upload for now
if (options.pre && options.pre.upload) {
this._pre.upload = this._pre.upload.concat(options.pre.upload);
this.pre("upload", options.pre.upload);
}

}
Expand All @@ -68,22 +64,6 @@ Object.defineProperty(s3file.prototype, 's3config', { get: function() {
}});


/**
* Allows you to add pre middleware after the field has been initialised
*
* @api public
*/

s3file.prototype.pre = function(event, fn) {
if (!this._pre[event]) {
throw new Error('S3File (' + this.list.key + '.' + this.path + ') error: s3field.pre()\n\n' +
'Event ' + event + ' is not supported.\n');
}
this._pre[event].push(fn);
return this;
};


/**
* Registers the field on the List's Mongoose Schema.
*
Expand Down Expand Up @@ -298,7 +278,7 @@ s3file.prototype.uploadFile = function(item, file, update, callback) {
});
};

async.eachSeries(this._pre.upload, function(fn, next) {
this.hooks("pre:upload", function(fn, next) {
fn(item, file, next);
}, function(err) {
if (err) return callback(err);
Expand Down
Loading

0 comments on commit c5c274f

Please sign in to comment.