Update to remove null byte before saving to DB and remove null byte on changes

This commit is contained in:
Wu Cheng-Han 2017-01-02 11:05:05 +08:00
parent c3a96ff112
commit d9e19b6029
4 changed files with 42 additions and 7 deletions

View file

@ -20,6 +20,13 @@ if (config.dburl)
else else
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig); sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig);
// [Postgres] Handling NULL bytes
// https://github.com/sequelize/sequelize/issues/6485
function stripNullByte(value) {
return value ? value.replace(/\u0000/g, "") : value;
}
sequelize.stripNullByte = stripNullByte;
var db = {}; var db = {};
fs fs

View file

@ -52,13 +52,22 @@ module.exports = function (sequelize, DataTypes) {
defaultValue: 0 defaultValue: 0
}, },
title: { title: {
type: DataTypes.TEXT type: DataTypes.TEXT,
set: function (value) {
this.setDataValue('title', sequelize.stripNullByte(value));
}
}, },
content: { content: {
type: DataTypes.TEXT type: DataTypes.TEXT,
set: function (value) {
this.setDataValue('content', sequelize.stripNullByte(value));
}
}, },
authorship: { authorship: {
type: DataTypes.TEXT type: DataTypes.TEXT,
set: function (value) {
this.setDataValue('authorship', JSON.stringify(value));
}
}, },
lastchangeAt: { lastchangeAt: {
type: DataTypes.DATE type: DataTypes.DATE

View file

@ -59,19 +59,31 @@ module.exports = function (sequelize, DataTypes) {
defaultValue: Sequelize.UUIDV4 defaultValue: Sequelize.UUIDV4
}, },
patch: { patch: {
type: DataTypes.TEXT type: DataTypes.TEXT,
set: function (value) {
this.setDataValue('patch', sequelize.stripNullByte(value));
}
}, },
lastContent: { lastContent: {
type: DataTypes.TEXT type: DataTypes.TEXT,
set: function (value) {
this.setDataValue('lastContent', sequelize.stripNullByte(value));
}
}, },
content: { content: {
type: DataTypes.TEXT type: DataTypes.TEXT,
set: function (value) {
this.setDataValue('content', sequelize.stripNullByte(value));
}
}, },
length: { length: {
type: DataTypes.INTEGER type: DataTypes.INTEGER
}, },
authorship: { authorship: {
type: DataTypes.TEXT type: DataTypes.TEXT,
set: function (value) {
this.setDataValue('authorship', value ? JSON.stringify(value) : value);
}
} }
}, { }, {
classMethods: { classMethods: {

View file

@ -3207,6 +3207,12 @@ function buildCursor(user) {
} }
//editor actions //editor actions
function removeNullByte(cm, change) {
var str = change.text.join("\n");
if (/\u0000/g.test(str) && change.update) {
change.update(change.from, change.to, str.replace(/\u0000/g, "").split("\n"));
}
}
function enforceMaxLength(cm, change) { function enforceMaxLength(cm, change) {
var maxLength = cm.getOption("maxLength"); var maxLength = cm.getOption("maxLength");
if (maxLength && change.update) { if (maxLength && change.update) {
@ -3228,6 +3234,7 @@ var ignoreEmitEvents = ['setValue', 'ignoreHistory'];
editor.on('beforeChange', function (cm, change) { editor.on('beforeChange', function (cm, change) {
if (debug) if (debug)
console.debug(change); console.debug(change);
removeNullByte(cm, change);
if (enforceMaxLength(cm, change)) { if (enforceMaxLength(cm, change)) {
$('.limit-modal').modal('show'); $('.limit-modal').modal('show');
} }