/*
TODO: Needs Review and Spec
*/

module.exports = {

	upload: function (req, res) {
		var knox = require('knox-s3');
		var keystone = req.keystone;
		var Types = keystone.Field.Types;

		if (!keystone.security.csrf.validate(req, req.body.authenticity_token)) {
			return res.status(403).send({ error: { message: 'invalid csrf' } });
		}

		if (req.files && req.files.file) {

			var s3Config = keystone.get('s3 config');

			var file = req.files.file;
			var path = s3Config.s3path ? s3Config.s3path + '/' : '';

			var headers = Types.S3File.prototype.generateHeaders.call({ s3config: s3Config, options: {} }, null, file);

			var s3Client = knox.createClient(s3Config);

			s3Client.putFile(file.path, path + file.name, headers, function (err, s3Response) {
				var sendResult = function () {
					if (err) {
						return res.send({ error: { message: err.message } });
					}

					if (s3Response) {
						if (s3Response.statusCode !== 200) {
							return res.send({ error: { message: 'Amazon returned Http Code: ' + s3Response.statusCode } });
						} else {
							if (s3Config.root) {
								return res.send({ image: { url: s3Config.root + '/' + file.name } });
							} else {
								var region = 's3';
								if (s3Config.region && s3Config.region !== 'us-east-1') {
									region = 's3-' + s3Config.region;
								}
								return res.send({ image: { url: 'https://' + region + '.amazonaws.com/' + s3Config.bucket + '/' + file.name } });
							}
						}
					}
				};

				res.format({
					html: sendResult,
					json: sendResult,
				});
			});

		} else {
			res.json({ error: { message: 'No image selected' } });
		}
	},
};

@@ -508,16 +508,216 @@
  : '';%0A%0A
+%09%09%09if (!file.name) %7B%0A%09%09%09%09var extension;%0A%09%09%09%09if (file.originalname) %7B%0A%09%09%09%09%09extension = file.originalname.match(/.*(%5C..*)/);%0A%09%09%09%09%7D%0A%09%09%09%09file.name = file.filename + (extension ? extension%5B1%5D : '');%0A%09%09%09%7D%0A%0A
 %09%09%09var h

Build S3 upload name when not present (#4871)