2016-08-27 05:04:08 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
var Promise = require('bluebird'),
|
|
|
|
path = require('path'),
|
2016-08-29 05:21:35 +00:00
|
|
|
fs = Promise.promisifyAll(require("fs-extra")),
|
2016-08-27 05:04:08 +00:00
|
|
|
_ = require('lodash'),
|
|
|
|
farmhash = require('farmhash'),
|
2016-08-28 01:46:10 +00:00
|
|
|
BSONModule = require('bson'),
|
2016-09-01 02:45:28 +00:00
|
|
|
BSON = new BSONModule.BSONPure.BSON(),
|
|
|
|
moment = require('moment');
|
2016-08-27 05:04:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Entries Model
|
|
|
|
*/
|
|
|
|
module.exports = {
|
|
|
|
|
|
|
|
_repoPath: 'repo',
|
|
|
|
_cachePath: 'data/cache',
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize Entries model
|
|
|
|
*
|
|
|
|
* @param {Object} appconfig The application config
|
|
|
|
* @return {Object} Entries model instance
|
|
|
|
*/
|
|
|
|
init(appconfig) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
2016-09-04 05:12:42 +00:00
|
|
|
self._repoPath = path.resolve(ROOTPATH, appconfig.datadir.repo);
|
|
|
|
self._cachePath = path.resolve(ROOTPATH, appconfig.datadir.db, 'cache');
|
2016-08-27 05:04:08 +00:00
|
|
|
|
|
|
|
return self;
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-08-30 02:19:47 +00:00
|
|
|
/**
|
|
|
|
* Check if a document already exists
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {Promise<Boolean>} True if exists, false otherwise
|
|
|
|
*/
|
|
|
|
exists(entryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
return self.fetchOriginal(entryPath, {
|
|
|
|
parseMarkdown: false,
|
|
|
|
parseMeta: false,
|
|
|
|
parseTree: false,
|
|
|
|
includeMarkdown: false,
|
|
|
|
includeParentInfo: false,
|
|
|
|
cache: false
|
|
|
|
}).then(() => {
|
|
|
|
return true;
|
|
|
|
}).catch((err) => {
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-08-28 01:46:10 +00:00
|
|
|
/**
|
2016-08-29 05:21:35 +00:00
|
|
|
* Fetch a document from cache, otherwise the original
|
2016-08-28 01:46:10 +00:00
|
|
|
*
|
2016-08-29 05:21:35 +00:00
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {Promise<Object>} Page Data
|
2016-08-28 01:46:10 +00:00
|
|
|
*/
|
2016-08-27 05:04:08 +00:00
|
|
|
fetch(entryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
let cpath = self.getCachePath(entryPath);
|
2016-08-27 05:04:08 +00:00
|
|
|
|
|
|
|
return fs.statAsync(cpath).then((st) => {
|
|
|
|
return st.isFile();
|
|
|
|
}).catch((err) => {
|
|
|
|
return false;
|
|
|
|
}).then((isCache) => {
|
|
|
|
|
|
|
|
if(isCache) {
|
|
|
|
|
2016-08-28 01:46:10 +00:00
|
|
|
// Load from cache
|
2016-08-27 05:04:08 +00:00
|
|
|
|
2016-08-28 01:46:10 +00:00
|
|
|
return fs.readFileAsync(cpath).then((contents) => {
|
|
|
|
return BSON.deserialize(contents);
|
2016-08-27 05:04:08 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
winston.error('Corrupted cache file. Deleting it...');
|
|
|
|
fs.unlinkSync(cpath);
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2016-08-28 01:46:10 +00:00
|
|
|
// Load original
|
|
|
|
|
|
|
|
return self.fetchOriginal(entryPath);
|
2016-08-27 05:04:08 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-08-28 01:46:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the original document entry
|
|
|
|
*
|
2016-08-29 05:21:35 +00:00
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @param {Object} options The options
|
|
|
|
* @return {Promise<Object>} Page data
|
2016-08-28 01:46:10 +00:00
|
|
|
*/
|
|
|
|
fetchOriginal(entryPath, options) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
let fpath = self.getFullPath(entryPath);
|
|
|
|
let cpath = self.getCachePath(entryPath);
|
2016-08-28 01:46:10 +00:00
|
|
|
|
|
|
|
options = _.defaults(options, {
|
|
|
|
parseMarkdown: true,
|
|
|
|
parseMeta: true,
|
|
|
|
parseTree: true,
|
|
|
|
includeMarkdown: false,
|
|
|
|
includeParentInfo: true,
|
|
|
|
cache: true
|
|
|
|
});
|
|
|
|
|
|
|
|
return fs.statAsync(fpath).then((st) => {
|
|
|
|
if(st.isFile()) {
|
|
|
|
return fs.readFileAsync(fpath, 'utf8').then((contents) => {
|
|
|
|
|
|
|
|
// Parse contents
|
|
|
|
|
|
|
|
let pageData = {
|
|
|
|
markdown: (options.includeMarkdown) ? contents : '',
|
|
|
|
html: (options.parseMarkdown) ? mark.parseContent(contents) : '',
|
|
|
|
meta: (options.parseMeta) ? mark.parseMeta(contents) : {},
|
|
|
|
tree: (options.parseTree) ? mark.parseTree(contents) : []
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!pageData.meta.title) {
|
|
|
|
pageData.meta.title = _.startCase(entryPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
pageData.meta.path = entryPath;
|
|
|
|
|
|
|
|
// Get parent
|
|
|
|
|
|
|
|
let parentPromise = (options.includeParentInfo) ? self.getParentInfo(entryPath).then((parentData) => {
|
|
|
|
return (pageData.parent = parentData);
|
|
|
|
}).catch((err) => {
|
|
|
|
return (pageData.parent = false);
|
|
|
|
}) : Promise.resolve(true);
|
|
|
|
|
|
|
|
return parentPromise.then(() => {
|
|
|
|
|
|
|
|
// Cache to disk
|
|
|
|
|
|
|
|
if(options.cache) {
|
2016-09-05 23:23:49 +00:00
|
|
|
let cacheData = BSON.serialize(_.pick(pageData, ['html', 'meta', 'tree', 'parent']), false, false, false);
|
2016-08-28 01:46:10 +00:00
|
|
|
return fs.writeFileAsync(cpath, cacheData).catch((err) => {
|
|
|
|
winston.error('Unable to write to cache! Performance may be affected.');
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}).return(pageData);
|
|
|
|
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-30 02:19:47 +00:00
|
|
|
}).catch((err) => {
|
|
|
|
return Promise.reject(new Error('Entry ' + entryPath + ' does not exist!'));
|
2016-08-28 01:46:10 +00:00
|
|
|
});
|
2016-08-27 05:04:08 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2016-08-28 01:46:10 +00:00
|
|
|
/**
|
|
|
|
* Parse raw url path and make it safe
|
|
|
|
*
|
|
|
|
* @param {String} urlPath The url path
|
|
|
|
* @return {String} Safe entry path
|
|
|
|
*/
|
2016-08-27 05:04:08 +00:00
|
|
|
parsePath(urlPath) {
|
|
|
|
|
|
|
|
let wlist = new RegExp('[^a-z0-9/\-]','g');
|
|
|
|
|
|
|
|
urlPath = _.toLower(urlPath).replace(wlist, '');
|
|
|
|
|
|
|
|
if(urlPath === '/') {
|
|
|
|
urlPath = 'home';
|
|
|
|
}
|
|
|
|
|
|
|
|
let urlParts = _.filter(_.split(urlPath, '/'), (p) => { return !_.isEmpty(p); });
|
|
|
|
|
|
|
|
return _.join(urlParts, '/');
|
|
|
|
|
2016-08-28 01:46:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the parent information.
|
|
|
|
*
|
2016-08-29 05:21:35 +00:00
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {Promise<Object|False>} The parent information.
|
2016-08-28 01:46:10 +00:00
|
|
|
*/
|
|
|
|
getParentInfo(entryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
if(_.includes(entryPath, '/')) {
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
let parentParts = _.initial(_.split(entryPath, '/'));
|
|
|
|
let parentPath = _.join(parentParts,'/');
|
2016-08-28 01:46:10 +00:00
|
|
|
let parentFile = _.last(parentParts);
|
2016-08-29 05:21:35 +00:00
|
|
|
let fpath = self.getFullPath(parentPath);
|
2016-08-28 01:46:10 +00:00
|
|
|
|
|
|
|
return fs.statAsync(fpath).then((st) => {
|
|
|
|
if(st.isFile()) {
|
|
|
|
return fs.readFileAsync(fpath, 'utf8').then((contents) => {
|
|
|
|
|
|
|
|
let pageMeta = mark.parseMeta(contents);
|
|
|
|
|
|
|
|
return {
|
|
|
|
path: parentPath,
|
|
|
|
title: (pageMeta.title) ? pageMeta.title : _.startCase(parentFile),
|
|
|
|
subtitle: (pageMeta.subtitle) ? pageMeta.subtitle : false
|
|
|
|
};
|
|
|
|
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.reject(new Error('Parent entry is not a valid file.'));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
} else {
|
|
|
|
return Promise.reject(new Error('Parent entry is root.'));
|
|
|
|
}
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the full original path of a document.
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {String} The full path.
|
|
|
|
*/
|
|
|
|
getFullPath(entryPath) {
|
|
|
|
return path.join(this._repoPath, entryPath + '.md');
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the full cache path of a document.
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {String} The full cache path.
|
|
|
|
*/
|
|
|
|
getCachePath(entryPath) {
|
|
|
|
return path.join(this._cachePath, farmhash.fingerprint32(entryPath) + '.bson');
|
|
|
|
},
|
|
|
|
|
2016-09-01 02:45:28 +00:00
|
|
|
/**
|
|
|
|
* Gets the entry path from full path.
|
|
|
|
*
|
|
|
|
* @param {String} fullPath The full path
|
|
|
|
* @return {String} The entry path
|
|
|
|
*/
|
|
|
|
getEntryPathFromFullPath(fullPath) {
|
|
|
|
let absRepoPath = path.resolve(ROOTPATH, this._repoPath);
|
|
|
|
return _.chain(fullPath).replace(absRepoPath, '').replace('.md', '').replace(new RegExp('\\\\', 'g'),'/').value();
|
|
|
|
},
|
|
|
|
|
2016-08-29 05:21:35 +00:00
|
|
|
/**
|
|
|
|
* Update an existing document
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @param {String} contents The markdown-formatted contents
|
|
|
|
* @return {Promise<Boolean>} True on success, false on failure
|
|
|
|
*/
|
|
|
|
update(entryPath, contents) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let fpath = self.getFullPath(entryPath);
|
|
|
|
|
|
|
|
return fs.statAsync(fpath).then((st) => {
|
|
|
|
if(st.isFile()) {
|
|
|
|
return self.makePersistent(entryPath, contents).then(() => {
|
2016-09-05 23:23:49 +00:00
|
|
|
return self.updateCache(entryPath);
|
2016-08-29 05:21:35 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.reject(new Error('Entry does not exist!'));
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
2016-09-05 23:23:49 +00:00
|
|
|
winston.error(err);
|
|
|
|
return Promise.reject(new Error('Failed to save document.'));
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update local cache and search index
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {Promise} Promise of the operation
|
|
|
|
*/
|
|
|
|
updateCache(entryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
return self.fetchOriginal(entryPath, {
|
|
|
|
parseMarkdown: true,
|
|
|
|
parseMeta: true,
|
|
|
|
parseTree: true,
|
|
|
|
includeMarkdown: true,
|
|
|
|
includeParentInfo: true,
|
|
|
|
cache: true
|
|
|
|
}).then((pageData) => {
|
|
|
|
return {
|
|
|
|
entryPath,
|
|
|
|
meta: pageData.meta,
|
|
|
|
parent: pageData.parent || {},
|
|
|
|
text: mark.removeMarkdown(pageData.markdown)
|
|
|
|
};
|
|
|
|
}).then((content) => {
|
|
|
|
ws.emit('searchAdd', {
|
|
|
|
auth: WSInternalKey,
|
|
|
|
content
|
|
|
|
});
|
|
|
|
return true;
|
2016-08-30 02:19:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new document
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @param {String} contents The markdown-formatted contents
|
|
|
|
* @return {Promise<Boolean>} True on success, false on failure
|
|
|
|
*/
|
|
|
|
create(entryPath, contents) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
|
|
|
|
return self.exists(entryPath).then((docExists) => {
|
|
|
|
if(!docExists) {
|
|
|
|
return self.makePersistent(entryPath, contents).then(() => {
|
2016-09-05 23:23:49 +00:00
|
|
|
return self.updateCache(entryPath);
|
2016-08-30 02:19:47 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return Promise.reject(new Error('Entry already exists!'));
|
|
|
|
}
|
|
|
|
}).catch((err) => {
|
|
|
|
winston.error(err);
|
|
|
|
return Promise.reject(new Error('Something went wrong.'));
|
2016-08-29 05:21:35 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Makes a document persistent to disk and git repository
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @param {String} contents The markdown-formatted contents
|
|
|
|
* @return {Promise<Boolean>} True on success, false on failure
|
|
|
|
*/
|
|
|
|
makePersistent(entryPath, contents) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let fpath = self.getFullPath(entryPath);
|
|
|
|
|
|
|
|
return fs.outputFileAsync(fpath, contents).then(() => {
|
|
|
|
return git.commitDocument(entryPath);
|
|
|
|
});
|
|
|
|
|
2016-08-30 02:19:47 +00:00
|
|
|
},
|
|
|
|
|
2016-09-09 23:22:18 +00:00
|
|
|
/**
|
|
|
|
* Move a document
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The current entry path
|
|
|
|
* @param {String} newEntryPath The new entry path
|
|
|
|
* @return {Promise} Promise of the operation
|
|
|
|
*/
|
|
|
|
move(entryPath, newEntryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
2016-09-04 05:12:42 +00:00
|
|
|
|
2016-09-11 02:53:21 +00:00
|
|
|
if(_.isEmpty(entryPath) || entryPath === 'home') {
|
|
|
|
return Promise.reject(new Error('Invalid path!'));
|
|
|
|
}
|
|
|
|
|
2016-09-09 23:22:18 +00:00
|
|
|
return git.moveDocument(entryPath, newEntryPath).then(() => {
|
|
|
|
return git.commitDocument(newEntryPath).then(() => {
|
2016-09-11 02:53:21 +00:00
|
|
|
|
|
|
|
// Delete old cache version
|
|
|
|
|
|
|
|
let oldEntryCachePath = self.getCachePath(entryPath);
|
|
|
|
fs.unlinkAsync(oldEntryCachePath).catch((err) => { return true; });
|
|
|
|
|
|
|
|
// Delete old index entry
|
|
|
|
|
|
|
|
ws.emit('searchDel', {
|
|
|
|
auth: WSInternalKey,
|
|
|
|
entryPath
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create cache for new entry
|
|
|
|
|
2016-09-09 23:22:18 +00:00
|
|
|
return self.updateCache(newEntryPath);
|
2016-09-11 02:53:21 +00:00
|
|
|
|
2016-09-09 23:22:18 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
2016-09-04 05:12:42 +00:00
|
|
|
|
2016-08-30 02:19:47 +00:00
|
|
|
/**
|
|
|
|
* Generate a starter page content based on the entry path
|
|
|
|
*
|
|
|
|
* @param {String} entryPath The entry path
|
|
|
|
* @return {Promise<String>} Starter content
|
|
|
|
*/
|
|
|
|
getStarter(entryPath) {
|
|
|
|
|
|
|
|
let self = this;
|
|
|
|
let formattedTitle = _.startCase(_.last(_.split(entryPath, '/')));
|
|
|
|
|
|
|
|
return fs.readFileAsync(path.join(ROOTPATH, 'client/content/create.md'), 'utf8').then((contents) => {
|
|
|
|
return _.replace(contents, new RegExp('{TITLE}', 'g'), formattedTitle);
|
|
|
|
});
|
|
|
|
|
2016-08-27 05:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|