mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-08 15:05:26 +02:00
Changed hash function
This commit is contained in:
parent
bdd5da4ec6
commit
4509235409
6 changed files with 35 additions and 27 deletions
|
@ -12,7 +12,6 @@
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/polyfill": "^7.0.0",
|
"@babel/polyfill": "^7.0.0",
|
||||||
"blueimp-md5": "^2.10.0",
|
|
||||||
"body-parser": "latest",
|
"body-parser": "latest",
|
||||||
"codex.editor": "^2.1.3",
|
"codex.editor": "^2.1.3",
|
||||||
"codex.editor.header": "^2.0.5",
|
"codex.editor.header": "^2.0.5",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
const Model = require('../models/page');
|
const Model = require('../models/page');
|
||||||
const Alias = require('../models/alias');
|
const Alias = require('../models/alias');
|
||||||
const aliasTypes = require('../constants/aliasTypes');
|
const aliasTypes = require('../constants/aliasTypes');
|
||||||
const md5 = require('blueimp-md5');
|
const md5 = require('../utils/md5');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @class Pages
|
* @class Pages
|
||||||
|
@ -15,7 +15,7 @@ class Pages {
|
||||||
* @returns {['title', 'body']}
|
* @returns {['title', 'body']}
|
||||||
*/
|
*/
|
||||||
static get REQUIRED_FIELDS() {
|
static get REQUIRED_FIELDS() {
|
||||||
return ['body'];
|
return [ 'body' ];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
const {aliases: aliasesDb} = require('../utils/database/index');
|
const {aliases: aliasesDb} = require('../utils/database/index');
|
||||||
const md5 = require('blueimp-md5');
|
const md5 = require('../utils/md5');
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} AliasData
|
* @typedef {Object} AliasData
|
||||||
* @property {string} _id - alias id
|
* @property {string} _id - alias id
|
||||||
|
|
|
@ -148,29 +148,14 @@ class Page {
|
||||||
* @returns {Promise<Page>}
|
* @returns {Promise<Page>}
|
||||||
*/
|
*/
|
||||||
async save() {
|
async save() {
|
||||||
let newUri = this.uri;
|
this.uri = await this.composeUri(this.uri);
|
||||||
let pageWithSameUri = await Page.getByUri(newUri);
|
|
||||||
let pageWithSameUriCount = 0;
|
|
||||||
|
|
||||||
while (pageWithSameUri._id && pageWithSameUri._id !== this._id) {
|
|
||||||
pageWithSameUriCount++;
|
|
||||||
pageWithSameUri = await Page.getByUri(newUri + `-${pageWithSameUriCount}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.uri = pageWithSameUriCount ? newUri + `-${pageWithSameUriCount}` : newUri;
|
|
||||||
|
|
||||||
if (!this._id) {
|
if (!this._id) {
|
||||||
const insertedRow = await pagesDb.insert({
|
const insertedRow = await pagesDb.insert(this.data);
|
||||||
...this.data,
|
|
||||||
uri: this.uri
|
|
||||||
});
|
|
||||||
|
|
||||||
this._id = insertedRow._id;
|
this._id = insertedRow._id;
|
||||||
} else {
|
} else {
|
||||||
await pagesDb.update({_id: this._id}, {
|
await pagesDb.update({_id: this._id}, this.data);
|
||||||
...this.data,
|
|
||||||
uri: this.uri
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
@ -189,6 +174,23 @@ class Page {
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find and return available uri
|
||||||
|
*
|
||||||
|
* @returns {Promise<string>}
|
||||||
|
*/
|
||||||
|
async composeUri(uri) {
|
||||||
|
let pageWithSameUri = await Page.getByUri(uri);
|
||||||
|
let pageWithSameUriCount = 0;
|
||||||
|
|
||||||
|
while (pageWithSameUri._id && pageWithSameUri._id !== this._id) {
|
||||||
|
pageWithSameUriCount++;
|
||||||
|
pageWithSameUri = await Page.getByUri(uri + `-${pageWithSameUriCount}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pageWithSameUriCount ? uri + `-${pageWithSameUriCount}` : uri;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return readable page data
|
* Return readable page data
|
||||||
*
|
*
|
||||||
|
|
12
src/utils/md5.js
Normal file
12
src/utils/md5.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
const crypto = require('crypto');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create binary md5
|
||||||
|
* @param stringToHash - string to hash
|
||||||
|
* @returns {string} - binary hash of argument
|
||||||
|
*/
|
||||||
|
module.exports = function md5(stringToHash) {
|
||||||
|
return crypto.createHash('md5')
|
||||||
|
.update(stringToHash)
|
||||||
|
.digest('binary');
|
||||||
|
};
|
|
@ -1162,11 +1162,6 @@ bluebird@^3.5.1:
|
||||||
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.3.tgz#7d01c6f9616c9a51ab0f8c549a79dfe6ec33efa7"
|
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.5.3.tgz#7d01c6f9616c9a51ab0f8c549a79dfe6ec33efa7"
|
||||||
integrity sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw==
|
integrity sha512-/qKPUQlaW1OyR51WeCPBvRnAlnZFUJkCSG5HzGnuIqhgyJtF+T94lFnn33eiazjRm2LAHVy2guNnaq48X9SJuw==
|
||||||
|
|
||||||
blueimp-md5@^2.10.0:
|
|
||||||
version "2.10.0"
|
|
||||||
resolved "https://registry.yarnpkg.com/blueimp-md5/-/blueimp-md5-2.10.0.tgz#02f0843921f90dca14f5b8920a38593201d6964d"
|
|
||||||
integrity sha512-EkNUOi7tpV68TqjpiUz9D9NcT8um2+qtgntmMbi5UKssVX2m/2PLqotcric0RE63pB3HPN/fjf3cKHN2ufGSUQ==
|
|
||||||
|
|
||||||
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
|
bn.js@^4.0.0, bn.js@^4.1.0, bn.js@^4.1.1, bn.js@^4.4.0:
|
||||||
version "4.11.8"
|
version "4.11.8"
|
||||||
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
|
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.8.tgz#2cde09eb5ee341f484746bb0309b3253b1b1442f"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue