Added attachments to campaigns
This commit is contained in:
parent
89715c56fc
commit
bfc6983c93
6 changed files with 444 additions and 115 deletions
|
@ -12,6 +12,7 @@ let feed = require('../feed');
|
||||||
let log = require('npmlog');
|
let log = require('npmlog');
|
||||||
let mailer = require('../mailer');
|
let mailer = require('../mailer');
|
||||||
let caches = require('../caches');
|
let caches = require('../caches');
|
||||||
|
let humanize = require('humanize');
|
||||||
|
|
||||||
let allowedKeys = ['description', 'from', 'address', 'subject', 'template', 'source_url', 'list', 'segment', 'html', 'text', 'tracking_disabled'];
|
let allowedKeys = ['description', 'from', 'address', 'subject', 'template', 'source_url', 'list', 'segment', 'html', 'text', 'tracking_disabled'];
|
||||||
|
|
||||||
|
@ -362,6 +363,120 @@ module.exports.get = (id, withSegment, callback) => {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
module.exports.getAttachments = (campaign, callback) => {
|
||||||
|
campaign = Number(campaign) || 0;
|
||||||
|
|
||||||
|
if (campaign < 1) {
|
||||||
|
return callback(new Error('Missing Campaign ID'));
|
||||||
|
}
|
||||||
|
|
||||||
|
db.getConnection((err, connection) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
connection.query('SELECT `id`, `filename`, `size`, `created` FROM `attachments` WHERE `campaign`=?', [campaign], (err, rows) => {
|
||||||
|
connection.release();
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!rows || !rows.length) {
|
||||||
|
return callback(null, []);
|
||||||
|
}
|
||||||
|
|
||||||
|
let attachments = rows.map((row, i) => {
|
||||||
|
row = tools.convertKeys(row);
|
||||||
|
row.index = i + 1;
|
||||||
|
row.size = humanize.filesize(Number(row.size) || 0);
|
||||||
|
return row;
|
||||||
|
});
|
||||||
|
return callback(null, attachments);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.addAttachment = (id, attachment, callback) => {
|
||||||
|
|
||||||
|
let size = attachment.content ? attachment.content.length : 0;
|
||||||
|
|
||||||
|
if (!size) {
|
||||||
|
return callback(new Error('Emtpy or too large attahcment'));
|
||||||
|
}
|
||||||
|
db.getConnection((err, connection) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let keys = ['campaign', 'size'];
|
||||||
|
let values = [id, size];
|
||||||
|
|
||||||
|
Object.keys(attachment).forEach(key => {
|
||||||
|
let value;
|
||||||
|
if (Buffer.isBuffer(attachment[key])) {
|
||||||
|
value = attachment[key];
|
||||||
|
} else {
|
||||||
|
value = typeof attachment[key] === 'number' ? attachment[key] : (attachment[key] || '').toString().trim();
|
||||||
|
}
|
||||||
|
|
||||||
|
key = tools.toDbKey(key);
|
||||||
|
keys.push(key);
|
||||||
|
values.push(value);
|
||||||
|
});
|
||||||
|
|
||||||
|
let query = 'INSERT INTO `attachments` (`' + keys.join('`, `') + '`) VALUES (' + values.map(() => '?').join(',') + ')';
|
||||||
|
connection.query(query, values, (err, result) => {
|
||||||
|
connection.release();
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let attachmentId = result && result.insertId || false;
|
||||||
|
return callback(null, attachmentId);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.deleteAttachment = (id, attachment, callback) => {
|
||||||
|
db.getConnection((err, connection) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let query = 'DELETE FROM `attachments` WHERE `id`=? AND `campaign`=? LIMIT 1';
|
||||||
|
connection.query(query, [attachment, id], (err, result) => {
|
||||||
|
connection.release();
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let deleted = result && result.affectedRows || false;
|
||||||
|
return callback(null, deleted);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.getAttachment = (id, attachment, callback) => {
|
||||||
|
db.getConnection((err, connection) => {
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
|
||||||
|
let query = 'SELECT `filename`, `content_type`, `content` FROM `attachments` WHERE `id`=? AND `campaign`=? LIMIT 1';
|
||||||
|
connection.query(query, [attachment, id], (err, rows) => {
|
||||||
|
connection.release();
|
||||||
|
if (err) {
|
||||||
|
return callback(err);
|
||||||
|
}
|
||||||
|
if (!rows || !rows.length) {
|
||||||
|
return callback(null, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
let attachment = tools.convertKeys(rows[0]);
|
||||||
|
return callback(null, attachment);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
module.exports.getLinks = (id, linkId, callback) => {
|
module.exports.getLinks = (id, linkId, callback) => {
|
||||||
if (!callback && typeof linkId === 'function') {
|
if (!callback && typeof linkId === 'function') {
|
||||||
callback = linkId;
|
callback = linkId;
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
{
|
{
|
||||||
"schemaVersion": 18
|
"schemaVersion": 19
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,11 @@ let tools = require('../lib/tools');
|
||||||
let striptags = require('striptags');
|
let striptags = require('striptags');
|
||||||
let passport = require('../lib/passport');
|
let passport = require('../lib/passport');
|
||||||
let htmlescape = require('escape-html');
|
let htmlescape = require('escape-html');
|
||||||
|
let multer = require('multer');
|
||||||
|
let uploadStorage = multer.memoryStorage();
|
||||||
|
let uploads = multer({
|
||||||
|
storage: uploadStorage
|
||||||
|
});
|
||||||
|
|
||||||
router.all('/*', (req, res, next) => {
|
router.all('/*', (req, res, next) => {
|
||||||
if (!req.user) {
|
if (!req.user) {
|
||||||
|
@ -120,6 +125,12 @@ router.get('/edit/:id', passport.csrfProtection, (req, res, next) => {
|
||||||
return res.redirect('/campaigns');
|
return res.redirect('/campaigns');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
campaigns.getAttachments(campaign.id, (err, attachments) => {
|
||||||
|
if (err) {
|
||||||
|
return next(err);
|
||||||
|
}
|
||||||
|
campaign.attachments = attachments;
|
||||||
|
|
||||||
settings.list(['disableWysiwyg'], (err, configItems) => {
|
settings.list(['disableWysiwyg'], (err, configItems) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
return next(err);
|
return next(err);
|
||||||
|
@ -151,6 +162,7 @@ router.get('/edit/:id', passport.csrfProtection, (req, res, next) => {
|
||||||
campaign.disableWysiwyg = configItems.disableWysiwyg;
|
campaign.disableWysiwyg = configItems.disableWysiwyg;
|
||||||
campaign.showGeneral = req.query.tab === 'general' || !req.query.tab;
|
campaign.showGeneral = req.query.tab === 'general' || !req.query.tab;
|
||||||
campaign.showTemplate = req.query.tab === 'template';
|
campaign.showTemplate = req.query.tab === 'template';
|
||||||
|
campaign.showAttachments = req.query.tab === 'attachments';
|
||||||
|
|
||||||
let view;
|
let view;
|
||||||
switch (campaign.type) {
|
switch (campaign.type) {
|
||||||
|
@ -240,6 +252,7 @@ router.get('/edit/:id', passport.csrfProtection, (req, res, next) => {
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
router.post('/edit', passport.parseForm, passport.csrfProtection, (req, res) => {
|
router.post('/edit', passport.parseForm, passport.csrfProtection, (req, res) => {
|
||||||
campaigns.update(req.body.id, req.body, (err, updated) => {
|
campaigns.update(req.body.id, req.body, (err, updated) => {
|
||||||
|
@ -762,4 +775,80 @@ router.post('/inactivate', passport.parseForm, passport.csrfProtection, (req, re
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post('/attachment', uploads.single('attachment'), passport.parseForm, passport.csrfProtection, (req, res) => {
|
||||||
|
campaigns.get(req.body.id, false, (err, campaign) => {
|
||||||
|
if (err || !campaign) {
|
||||||
|
req.flash('danger', err && err.message || err || 'Could not find campaign with specified ID');
|
||||||
|
return res.redirect('/campaigns');
|
||||||
|
}
|
||||||
|
campaigns.addAttachment(campaign.id, {
|
||||||
|
filename: req.file.originalname,
|
||||||
|
contentType: req.file.mimetype,
|
||||||
|
content: req.file.buffer
|
||||||
|
}, (err, attachmentId) => {
|
||||||
|
if (err) {
|
||||||
|
req.flash('danger', err && err.message || err);
|
||||||
|
} else if (attachmentId) {
|
||||||
|
req.flash('success', 'Attachment uploaded');
|
||||||
|
} else {
|
||||||
|
req.flash('info', 'Could not store attachment');
|
||||||
|
}
|
||||||
|
return res.redirect('/campaigns/edit/' + campaign.id + '?tab=attachments');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/attachment/delete', passport.parseForm, passport.csrfProtection, (req, res) => {
|
||||||
|
campaigns.get(req.body.id, false, (err, campaign) => {
|
||||||
|
if (err || !campaign) {
|
||||||
|
req.flash('danger', err && err.message || err || 'Could not find campaign with specified ID');
|
||||||
|
return res.redirect('/campaigns');
|
||||||
|
}
|
||||||
|
campaigns.deleteAttachment(campaign.id, Number(req.body.attachment), (err, deleted) => {
|
||||||
|
if (err) {
|
||||||
|
req.flash('danger', err && err.message || err);
|
||||||
|
} else if (deleted) {
|
||||||
|
req.flash('success', 'Attachment deleted');
|
||||||
|
} else {
|
||||||
|
req.flash('info', 'Could not delete attachment');
|
||||||
|
}
|
||||||
|
return res.redirect('/campaigns/edit/' + campaign.id + '?tab=attachments');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.post('/attachment/download', passport.parseForm, passport.csrfProtection, (req, res) => {
|
||||||
|
campaigns.get(req.body.id, false, (err, campaign) => {
|
||||||
|
if (err || !campaign) {
|
||||||
|
req.flash('danger', err && err.message || err || 'Could not find campaign with specified ID');
|
||||||
|
return res.redirect('/campaigns');
|
||||||
|
}
|
||||||
|
campaigns.getAttachment(campaign.id, Number(req.body.attachment), (err, attachment) => {
|
||||||
|
if (err) {
|
||||||
|
req.flash('danger', err && err.message || err);
|
||||||
|
return res.redirect('/campaigns/edit/' + campaign.id + '?tab=attachments');
|
||||||
|
} else if (!attachment) {
|
||||||
|
req.flash('success', 'Attachment uploaded');
|
||||||
|
return res.redirect('/campaigns/edit/' + campaign.id + '?tab=attachments');
|
||||||
|
}
|
||||||
|
|
||||||
|
res.set('Content-Disposition', 'attachment; filename="' + encodeURIComponent(attachment.filename).replace(/['()]/g, escape) + '"');
|
||||||
|
res.set('Content-Type', attachment.contentType);
|
||||||
|
res.send(attachment.content);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
router.get('/attachment/:campaign', passport.csrfProtection, (req, res) => {
|
||||||
|
campaigns.get(req.params.campaign, false, (err, campaign) => {
|
||||||
|
if (err || !campaign) {
|
||||||
|
req.flash('danger', err && err.message || err || 'Could not find campaign with specified ID');
|
||||||
|
return res.redirect('/campaigns');
|
||||||
|
}
|
||||||
|
campaign.csrfToken = req.csrfToken();
|
||||||
|
res.render('campaigns/upload-attachment', campaign);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|
21
setup/sql/upgrade-00019.sql
Normal file
21
setup/sql/upgrade-00019.sql
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
# Header section
|
||||||
|
# Define incrementing schema version number
|
||||||
|
SET @schema_version = '19';
|
||||||
|
|
||||||
|
CREATE TABLE `attachments` (
|
||||||
|
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
|
||||||
|
`campaign` int(11) unsigned NOT NULL,
|
||||||
|
`filename` varchar(255) CHARACTER SET utf8mb4 NOT NULL DEFAULT '',
|
||||||
|
`content_type` varchar(100) CHARACTER SET ascii NOT NULL DEFAULT '',
|
||||||
|
`content` longblob,
|
||||||
|
`size` int(11) NOT NULL,
|
||||||
|
`created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
PRIMARY KEY (`id`),
|
||||||
|
KEY `campaign` (`campaign`),
|
||||||
|
CONSTRAINT `attachments_ibfk_1` FOREIGN KEY (`campaign`) REFERENCES `campaigns` (`id`) ON DELETE CASCADE
|
||||||
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||||
|
|
||||||
|
# Footer section
|
||||||
|
LOCK TABLES `settings` WRITE;
|
||||||
|
INSERT INTO `settings` (`key`, `value`) VALUES('db_schema_version', @schema_version) ON DUPLICATE KEY UPDATE `value`=@schema_version;
|
||||||
|
UNLOCK TABLES;
|
|
@ -17,16 +17,29 @@
|
||||||
<input type="hidden" name="id" value="{{id}}" />
|
<input type="hidden" name="id" value="{{id}}" />
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
{{#each attachments}}
|
||||||
|
<form method="post" id="attachment-download-{{id}}" action="/campaigns/attachment/download">
|
||||||
|
<input type="hidden" name="_csrf" value="{{../csrfToken}}">
|
||||||
|
<input type="hidden" name="id" value="{{../id}}" />
|
||||||
|
<input type="hidden" name="attachment" value="{{id}}" />
|
||||||
|
</form>
|
||||||
|
<form method="post" class="delete-form" id="attachment-delete-{{id}}" action="/campaigns/attachment/delete">
|
||||||
|
<input type="hidden" name="_csrf" value="{{../csrfToken}}">
|
||||||
|
<input type="hidden" name="id" value="{{../id}}" />
|
||||||
|
<input type="hidden" name="attachment" value="{{id}}" />
|
||||||
|
</form>
|
||||||
|
{{/each}}
|
||||||
|
|
||||||
<form class="form-horizontal" method="post" action="/campaigns/edit">
|
<form class="form-horizontal" method="post" action="/campaigns/edit">
|
||||||
<input type="hidden" name="_csrf" value="{{csrfToken}}">
|
<input type="hidden" name="_csrf" value="{{csrfToken}}">
|
||||||
<input type="hidden" name="id" value="{{id}}" />
|
<input type="hidden" name="id" value="{{id}}" />
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
|
|
||||||
<!-- Nav tabs -->
|
<!-- Nav tabs -->
|
||||||
<ul class="nav nav-tabs" role="tablist">
|
<ul class="nav nav-tabs" role="tablist">
|
||||||
<li role="presentation" class="{{#if showGeneral}}active{{/if}}"><a href="#general" aria-controls="general" role="tab" data-toggle="tab">General</a></li>
|
<li role="presentation" class="{{#if showGeneral}}active{{/if}}"><a href="#general" aria-controls="general" role="tab" data-toggle="tab">General</a></li>
|
||||||
<li role="presentation" class="{{#if showTemplate}}active{{/if}}"><a href="#template" aria-controls="template" role="tab" data-toggle="tab">Template</a></li>
|
<li role="presentation" class="{{#if showTemplate}}active{{/if}}"><a href="#template" aria-controls="template" role="tab" data-toggle="tab">Template</a></li>
|
||||||
|
<li role="presentation" class="{{#if showAttachments}}active{{/if}}"><a href="#attachments" aria-controls="attachments" role="tab" data-toggle="tab">Attachments{{#if attachments}} <span class="badge">{{attachments.length}}</span>{{/if}}</a></li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<div class="tab-content">
|
<div class="tab-content">
|
||||||
|
@ -109,7 +122,6 @@
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</div>
|
</div>
|
||||||
<div role="tabpanel" class="tab-pane {{#if showTemplate}}active{{/if}}" id="template">
|
<div role="tabpanel" class="tab-pane {{#if showTemplate}}active{{/if}}" id="template">
|
||||||
|
|
||||||
<p></p>
|
<p></p>
|
||||||
|
|
||||||
<fieldset>
|
<fieldset>
|
||||||
|
@ -187,7 +199,67 @@
|
||||||
{{/if}}
|
{{/if}}
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div role="tabpanel" class="tab-pane {{#if showAttachments}}active{{/if}}" id="attachments">
|
||||||
|
|
||||||
|
<p></p>
|
||||||
|
|
||||||
|
|
||||||
|
<fieldset>
|
||||||
|
<legend>
|
||||||
|
Attachments
|
||||||
|
</legend>
|
||||||
|
|
||||||
|
<div class="table-responsive">
|
||||||
|
<table class="table table-bordered table-hover">
|
||||||
|
<thead>
|
||||||
|
<th class="col-md-1">
|
||||||
|
#
|
||||||
|
</th>
|
||||||
|
<th>
|
||||||
|
File
|
||||||
|
</th>
|
||||||
|
<th class="col-md-1">
|
||||||
|
Size
|
||||||
|
</th>
|
||||||
|
<th class="col-md-1">
|
||||||
|
|
||||||
|
</th>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{{#if attachments}}
|
||||||
|
{{#each attachments}}
|
||||||
|
<tr>
|
||||||
|
<td>
|
||||||
|
{{index}}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<button type="submit" form="attachment-download-{{id}}" class="btn btn-link btn-xs"><i class="glyphicon glyphicon-cloud-download"></i> {{filename}}</button>
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
{{size}}
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<button type="submit" form="attachment-delete-{{id}}" class="btn btn-danger btn-xs"><i class="glyphicon glyphicon-remove"></i> Delete</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{{/each}}
|
||||||
|
{{else}}
|
||||||
|
<tr>
|
||||||
|
<td colspan="4">
|
||||||
|
No data available in table
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{{/if}}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="pull-right">
|
||||||
|
<a class="btn btn-info btn-sm" href="/campaigns/attachment/{{id}}" role="button"><span class="glyphicon glyphicon-plus" aria-hidden="true"></span> Add Attachment</a>
|
||||||
|
</div>
|
||||||
|
</fieldset>
|
||||||
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<hr/>
|
<hr/>
|
||||||
|
@ -200,4 +272,5 @@
|
||||||
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-ok"></i> Update</button>
|
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-ok"></i> Update</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
31
views/campaigns/upload-attachment.hbs
Normal file
31
views/campaigns/upload-attachment.hbs
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
<ol class="breadcrumb">
|
||||||
|
<li><a href="/">Home</a></li>
|
||||||
|
<li><a href="/campaigns">Campaigns</a></li>
|
||||||
|
{{#if parent}}
|
||||||
|
<li><a href="/campaigns/view/{{parent.id}}">{{parent.name}}</a></li>
|
||||||
|
{{/if}}
|
||||||
|
<li><a href="/campaigns/view/{{id}}">{{name}}</a></li>
|
||||||
|
<li><a href="/campaigns/edit/{{id}}?tab=attachments">Edit Campaign</a></li>
|
||||||
|
<li class="active">Add Attachment</li>
|
||||||
|
</ol>
|
||||||
|
|
||||||
|
<h2>Edit Campaign <a class="btn btn-default btn-xs" href="/campaigns/view/{{id}}" role="button"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span> View campaign</a></h2>
|
||||||
|
|
||||||
|
<hr>
|
||||||
|
|
||||||
|
<form class="form-horizontal" method="post" action="/campaigns/attachment" enctype="multipart/form-data">
|
||||||
|
<input type="hidden" name="_csrf" value="{{csrfToken}}">
|
||||||
|
<input type="hidden" name="id" value="{{id}}" />
|
||||||
|
<div class="form-group">
|
||||||
|
<label for="attachment" class="col-sm-2 control-label">Upload new</label>
|
||||||
|
<div class="col-sm-6">
|
||||||
|
<input type="file" class="form-control" name="attachment" id="attachment" required>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="form-group">
|
||||||
|
<div class="col-sm-offset-2 col-sm-10">
|
||||||
|
<button type="submit" class="btn btn-primary"><i class="glyphicon glyphicon-cloud-upload"></i> Upload</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
Loading…
Add table
Add a link
Reference in a new issue