Skip to content

Commit

Permalink
Merge branch 'master' into signin
Browse files Browse the repository at this point in the history
  • Loading branch information
dominikwilkowski authored Jul 23, 2018
2 parents b87e78b + eb7c1bb commit 27aa760
Show file tree
Hide file tree
Showing 7 changed files with 1,182 additions and 880 deletions.
5 changes: 3 additions & 2 deletions admin/server/app/createDynamicRouter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var bodyParser = require('body-parser');
var express = require('express');
var multer = require('multer');

var uploads = require('../../../lib/uploads');

module.exports = function createDynamicRouter (keystone) {
// ensure keystone nav has been initialised
Expand All @@ -17,7 +18,7 @@ module.exports = function createDynamicRouter (keystone) {
// Use bodyParser and multer to parse request bodies and file uploads
router.use(bodyParser.json({}));
router.use(bodyParser.urlencoded({ extended: true }));
router.use(multer({ includeEmptyFields: true }));
uploads.configure(router);

// Bind the request to the keystone instance
router.use(function (req, res, next) {
Expand Down
24 changes: 21 additions & 3 deletions fields/types/location/LocationType.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ location.prototype.updateItem = function (item, data, callback) {
if (doGoogleLookup) {
var googleUpdateMode = this.getValueFromData(data, '_improve_overwrite') ? 'overwrite' : true;
this.googleLookup(item, false, googleUpdateMode, function (err, location, result) {
// TODO: we are currently discarding the error; it should probably be
// sent back in the response, needs consideration
// TODO: we are currently log the error but otherwise discard it; should probably be returned.. needs consideration
if (err) console.error(err);
callback();
});
return;
Expand Down Expand Up @@ -431,7 +431,8 @@ location.prototype.googleLookup = function (item, region, update, callback) {

_.forEach(result.address_components, function (val) {
if (_.indexOf(val.types, 'street_number') >= 0) {
location.street1 = [val.long_name];
location.street1 = location.street1 || [];
location.street1.unshift(val.long_name);
}
if (_.indexOf(val.types, 'route') >= 0) {
location.street1 = location.street1 || [];
Expand All @@ -450,6 +451,23 @@ location.prototype.googleLookup = function (item, region, update, callback) {
if (_.indexOf(val.types, 'postal_code') >= 0) {
location.postcode = val.short_name;
}

// These address_components could arguable all map to our 'number' field
// .. https://developers.google.com/maps/documentation/geocoding/intro#GeocodingResponses

// `subpremise` - "Indicates a first-order entity below a named location, usually a singular building within a collection of buildings with a common name"
// In practice this is often the unit/apartment number or level and is not always included
if (_.indexOf(val.types, 'subpremise') >= 0) {
location.number = val.short_name;
}

// These are all optional (rarely used?) and probably shouldn't replace the number if already set (due to subpremise)
// `floor` - Indicates the floor of a building address.
// `post_box` - Indicates a specific postal box.
// `room` - Indicates the room of a building address.
if (_.indexOf(val.types, 'floor') >= 0 || _.indexOf(val.types, 'post_box') >= 0 || _.indexOf(val.types, 'room') >= 0) {
location.number = location.number || val.short_name;
}
});

if (Array.isArray(location.street1)) {
Expand Down
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var Keystone = function () {
'module root': moduleRoot,
'frame guard': 'sameorigin',
'cache admin bundles': true,
'handle uploads': true,
};
this._redirects = {};

Expand Down
41 changes: 41 additions & 0 deletions lib/uploads.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var fs = require('fs');
var multer = require('multer');
var os = require('os');

function handleUploadedFiles (req, res, next) {
if (!req.files || !Array.isArray(req.files)) return next();
var originalFiles = req.files;
var files = {};
originalFiles.forEach(function (i) {
if (i.fieldname in files) {
var tmp = files[i.fieldname];
files[i.fieldname] = [tmp];
}
if (Array.isArray(files[i.fieldname])) {
files[i.fieldname].push(i);
} else {
files[i.fieldname] = i;
}
});
req.files = files;
var cleanup = function () {
originalFiles.forEach(function (i) {
if (i.path) {
fs.unlink(i.path, function () {});
}
});
};
res.on('close', cleanup);
res.on('finish', cleanup);
next();
};

exports.handleUploadedFiles = handleUploadedFiles;

exports.configure = function (app, options) {
var upload = multer(options || {
dest: os.tmpdir(),
});
app.use(upload.any());
app.use(handleUploadedFiles);
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"moment": "^2.19.3",
"mongoose": "^4.13.14",
"morgan": "^1.9.0",
"multer": "^0.1.8",
"multer": "^1.3.1",
"numeral": "^2.0.6",
"object-assign": "^4.1.1",
"qs": "^6.5.2",
Expand Down
11 changes: 6 additions & 5 deletions server/bindBodyParser.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var multer = require('multer');
var bodyParser = require('body-parser');

module.exports = function bindIPRestrictions (keystone, app) {
var uploads = require('../lib/uploads');

module.exports = function bindBodyParser (keystone, app) {
// Set up body options and cookie parser
var bodyParserParams = {};
if (keystone.get('file limit')) {
Expand All @@ -10,7 +11,7 @@ module.exports = function bindIPRestrictions (keystone, app) {
app.use(bodyParser.json(bodyParserParams));
bodyParserParams.extended = true;
app.use(bodyParser.urlencoded(bodyParserParams));
app.use(multer({
includeEmptyFields: true,
}));
if (keystone.get('handle uploads')) {
uploads.configure(app, keystone.get('multer options'));
}
};
Loading

0 comments on commit 27aa760

Please sign in to comment.