Skip to content

Commit

Permalink
Merge pull request #639 from scottnath/hotfix/put-upload
Browse files Browse the repository at this point in the history
Hotfix/put upload
  • Loading branch information
Snugug committed Jan 26, 2017
2 parents a66f507 + 1436a41 commit 933a781
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 46 deletions.
2 changes: 1 addition & 1 deletion lib/content/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ const filecompare = (files, inserted, val, attributes) => {
const data = inserted;

if (!files || !Array.isArray(files) || !data || typeof data !== 'object' || !values || typeof values !== 'object' || !attributes || typeof attributes !== 'object') {
return values;
return data;
}

// get all file inputs from the content type attributes
Expand Down
34 changes: 34 additions & 0 deletions lib/init/routes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,38 @@
'use strict';

const _ = require('lodash');
const config = require('config');
const multer = require('multer');
const mkdirp = require('mkdirp');

const content = require('../content/middleware');
const users = require('../users/middleware');

module.exports = (app) => {
return new Promise(res => {
// temp file storage location
const dest = config.storage.temp.dest || 'public/tmp/';

// multer form handling setup
const upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, dest);
},
filename: (req, file, cb) => {
cb(null, `/${file.originalname}`);
},
}),
});

// creates temp storage location if it does not exist
mkdirp.sync(dest);

// creates final storage location when using file storage
if (config.storage.type === 'fs' && typeof config.storage.dest === 'string' && config.storage.dest !== '') {
mkdirp.sync(config.storage.dest);
}

// add all content types and flows to request object
app.use((req, response, next) => {
const request = req;
Expand All @@ -16,12 +43,19 @@ module.exports = (app) => {
next();
});

// content type landing
app.use('/content/:type', content);

// save a piece of content
app.use('/content/:type/save', upload.any());

// piece of content actions
app.use('/content/:type/:id', content);
app.use('/content/:type/:id/:revision', content);
app.use('/content/:type/:id/:revision/edit', content);
app.use('/content/:type/:id/:revision/approve', content);

// individual user
app.use('/users/:id', users);

res(app);
Expand Down
45 changes: 6 additions & 39 deletions lib/routes/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
const config = require('config');
const types = require('punchcard-content-types');
const multer = require('multer');
const uuid = require('uuid');
const _ = require('lodash');
const path = require('path');
Expand All @@ -19,18 +18,6 @@ const workflows = require('../workflows');
const schedule = require('../schedule');
const storage = require('../storage');

const multeropts = {
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, config.storage.temp.dest || 'public/tmp/');
},
filename: (req, file, cb) => {
cb(null, `/${file.originalname}`);
},
}),
};
const upload = multer(multeropts);

/*
* Content Route Resolution
*
Expand Down Expand Up @@ -317,34 +304,31 @@ const routes = application => {
* @param {object} res - HTTP Response
* @param {object} next - Express callback
*/
app.post(`/${config.content.base}/:type/${config.content.actions.save}`, upload.any(), (req, res, next) => {
app.post(`/${config.content.base}/:type/${config.content.actions.save}`, (req, res, next) => {
const referrer = _.get(req.session, 'referrer') || req.get('Referrer');
let audits;
let publishable = false;
let check = 'publish';
let files = [];
let source = {
type: '',
type: 'add',
id: '',
revision: '',
revision: Math.floor(1000000 + Math.random() * 9000000),
};

if (req.body.submit === config.content.actions.new) {
check = 'save';
}

// determine data source from referrer
if (_.includes(referrer, '/add')) {
source.type = 'add';
}
else if (_.includes(referrer, '/edit')) {
if (_.includes(referrer, '/edit')) {
source = {
type: 'edit',
id: _.get(req.session, `form.content.edit[${req.params.type.toLowerCase()}].id`, ''),
revision: _.get(req.session, `form.content.edit[${req.params.type.toLowerCase()}].revision`, ''),
};
}
else {
else if (!_.includes(referrer, '/add')) {
// if neither edit or add, something nefarious is afoot
const err = {
message: 'You may only save from an edit or add form. For now...',
Expand Down Expand Up @@ -428,16 +412,14 @@ const routes = application => {
sunset,
publishable,
author: req.user.id,
value: data,
};

return storage.put(files).then(results => {
// if any files were uploaded
if (Object.keys(results).length > 0) {
insert.value = _.merge({}, data, utils.format(results));
}
else {
insert.value = data;
}

return database(`content-type--${req.content.type.id}`).where({
id,
Expand All @@ -449,21 +431,6 @@ const routes = application => {
// get all file inputs from the content type config
insert.value = cutils.filecompare(files, insert.value, content, req.content.type.attributes);


// /* NATH: now
// insert has file data now
// 1. use fileinputs


// 1. check for checkboxes without a new file
// - delete
// 2. check for checkboxes with a new file
// - delete
// 3. new file
// - replace with new
// 4. no file, no checkbox
// - replace with existing
// */
return database(`content-type--${req.params.type.toLowerCase()}`).insert(_.merge({}, insert, audits)).returning('*');
}).then(revision => {
const latest = utils.routes.identifier(revision, req.content.type)[0];
Expand Down
10 changes: 4 additions & 6 deletions lib/storage/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
'use strict';

const config = require('config');
const _ = require('lodash');
const urljoin = require('url-join');

/*
* File Get
Expand All @@ -11,17 +13,13 @@ const config = require('config');
*/
const get = file => {
const f = file;
let path = config.storage.public;
let path = _.get(config.storage, 'public', '');

if (path.indexOf('{{dest}}')) {
path = path.replace('{{dest}}', config.storage.dest);
}

if (path.substr(-1) !== '/') {
path += '/';
}

f.path = path + file.relative;
f.path = urljoin(path, file.relative);

return f;
};
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "punchcard-cms",
"description": "Node-based content management system",
"version": "0.0.0",
"main": "index.js",
"keywords": [
"punchcard-cms",
Expand Down Expand Up @@ -70,6 +71,7 @@
"knex": "^0.12.6",
"lodash": "^4.17.2",
"map-stream": "^0.0.6",
"mkdirp": "^0.5.1",
"modularscale-sass": "^2.1.1",
"moment": "^2.15.0",
"moment-timezone": "^0.5.4",
Expand All @@ -89,6 +91,7 @@
"serve-favicon": "^2.3.0",
"stream-from-array": "^1.0.0",
"underscore.string": "^3.3.4",
"url-join": "^1.1.0",
"uuid": "^2.0.2",
"validator": "^6.0.0",
"vinyl": "^2.0.0",
Expand Down

0 comments on commit 933a781

Please sign in to comment.