Skip to content

Commit 27aa760

Browse files
Merge branch 'master' into signin
2 parents b87e78b + eb7c1bb commit 27aa760

File tree

7 files changed

+1182
-880
lines changed

7 files changed

+1182
-880
lines changed

admin/server/app/createDynamicRouter.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
var bodyParser = require('body-parser');
22
var express = require('express');
3-
var multer = require('multer');
3+
4+
var uploads = require('../../../lib/uploads');
45

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

2223
// Bind the request to the keystone instance
2324
router.use(function (req, res, next) {

fields/types/location/LocationType.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,8 @@ location.prototype.updateItem = function (item, data, callback) {
320320
if (doGoogleLookup) {
321321
var googleUpdateMode = this.getValueFromData(data, '_improve_overwrite') ? 'overwrite' : true;
322322
this.googleLookup(item, false, googleUpdateMode, function (err, location, result) {
323-
// TODO: we are currently discarding the error; it should probably be
324-
// sent back in the response, needs consideration
323+
// TODO: we are currently log the error but otherwise discard it; should probably be returned.. needs consideration
324+
if (err) console.error(err);
325325
callback();
326326
});
327327
return;
@@ -431,7 +431,8 @@ location.prototype.googleLookup = function (item, region, update, callback) {
431431

432432
_.forEach(result.address_components, function (val) {
433433
if (_.indexOf(val.types, 'street_number') >= 0) {
434-
location.street1 = [val.long_name];
434+
location.street1 = location.street1 || [];
435+
location.street1.unshift(val.long_name);
435436
}
436437
if (_.indexOf(val.types, 'route') >= 0) {
437438
location.street1 = location.street1 || [];
@@ -450,6 +451,23 @@ location.prototype.googleLookup = function (item, region, update, callback) {
450451
if (_.indexOf(val.types, 'postal_code') >= 0) {
451452
location.postcode = val.short_name;
452453
}
454+
455+
// These address_components could arguable all map to our 'number' field
456+
// .. https://developers.google.com/maps/documentation/geocoding/intro#GeocodingResponses
457+
458+
// `subpremise` - "Indicates a first-order entity below a named location, usually a singular building within a collection of buildings with a common name"
459+
// In practice this is often the unit/apartment number or level and is not always included
460+
if (_.indexOf(val.types, 'subpremise') >= 0) {
461+
location.number = val.short_name;
462+
}
463+
464+
// These are all optional (rarely used?) and probably shouldn't replace the number if already set (due to subpremise)
465+
// `floor` - Indicates the floor of a building address.
466+
// `post_box` - Indicates a specific postal box.
467+
// `room` - Indicates the room of a building address.
468+
if (_.indexOf(val.types, 'floor') >= 0 || _.indexOf(val.types, 'post_box') >= 0 || _.indexOf(val.types, 'room') >= 0) {
469+
location.number = location.number || val.short_name;
470+
}
453471
});
454472

455473
if (Array.isArray(location.street1)) {

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var Keystone = function () {
3838
'module root': moduleRoot,
3939
'frame guard': 'sameorigin',
4040
'cache admin bundles': true,
41+
'handle uploads': true,
4142
};
4243
this._redirects = {};
4344

lib/uploads.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var fs = require('fs');
2+
var multer = require('multer');
3+
var os = require('os');
4+
5+
function handleUploadedFiles (req, res, next) {
6+
if (!req.files || !Array.isArray(req.files)) return next();
7+
var originalFiles = req.files;
8+
var files = {};
9+
originalFiles.forEach(function (i) {
10+
if (i.fieldname in files) {
11+
var tmp = files[i.fieldname];
12+
files[i.fieldname] = [tmp];
13+
}
14+
if (Array.isArray(files[i.fieldname])) {
15+
files[i.fieldname].push(i);
16+
} else {
17+
files[i.fieldname] = i;
18+
}
19+
});
20+
req.files = files;
21+
var cleanup = function () {
22+
originalFiles.forEach(function (i) {
23+
if (i.path) {
24+
fs.unlink(i.path, function () {});
25+
}
26+
});
27+
};
28+
res.on('close', cleanup);
29+
res.on('finish', cleanup);
30+
next();
31+
};
32+
33+
exports.handleUploadedFiles = handleUploadedFiles;
34+
35+
exports.configure = function (app, options) {
36+
var upload = multer(options || {
37+
dest: os.tmpdir(),
38+
});
39+
app.use(upload.any());
40+
app.use(handleUploadedFiles);
41+
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"moment": "^2.19.3",
6363
"mongoose": "^4.13.14",
6464
"morgan": "^1.9.0",
65-
"multer": "^0.1.8",
65+
"multer": "^1.3.1",
6666
"numeral": "^2.0.6",
6767
"object-assign": "^4.1.1",
6868
"qs": "^6.5.2",

server/bindBodyParser.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
var multer = require('multer');
21
var bodyParser = require('body-parser');
32

4-
module.exports = function bindIPRestrictions (keystone, app) {
3+
var uploads = require('../lib/uploads');
4+
5+
module.exports = function bindBodyParser (keystone, app) {
56
// Set up body options and cookie parser
67
var bodyParserParams = {};
78
if (keystone.get('file limit')) {
@@ -10,7 +11,7 @@ module.exports = function bindIPRestrictions (keystone, app) {
1011
app.use(bodyParser.json(bodyParserParams));
1112
bodyParserParams.extended = true;
1213
app.use(bodyParser.urlencoded(bodyParserParams));
13-
app.use(multer({
14-
includeEmptyFields: true,
15-
}));
14+
if (keystone.get('handle uploads')) {
15+
uploads.configure(app, keystone.get('multer options'));
16+
}
1617
};

0 commit comments

Comments
 (0)