Added thumbnail generation + insert image files display + Create page fix
This commit is contained in:
@@ -5,8 +5,6 @@ var Promise = require('bluebird'),
|
||||
fs = Promise.promisifyAll(require("fs-extra")),
|
||||
_ = require('lodash'),
|
||||
farmhash = require('farmhash'),
|
||||
BSONModule = require('bson'),
|
||||
BSON = new BSONModule.BSONPure.BSON(),
|
||||
moment = require('moment');
|
||||
|
||||
/**
|
||||
@@ -82,7 +80,7 @@ module.exports = {
|
||||
// Load from cache
|
||||
|
||||
return fs.readFileAsync(cpath).then((contents) => {
|
||||
return BSON.deserialize(contents);
|
||||
return JSON.parse(contents);
|
||||
}).catch((err) => {
|
||||
winston.error('Corrupted cache file. Deleting it...');
|
||||
fs.unlinkSync(cpath);
|
||||
@@ -156,7 +154,7 @@ module.exports = {
|
||||
// Cache to disk
|
||||
|
||||
if(options.cache) {
|
||||
let cacheData = BSON.serialize(_.pick(pageData, ['html', 'meta', 'tree', 'parent']), false, false, false);
|
||||
let cacheData = JSON.stringify(_.pick(pageData, ['html', 'meta', 'tree', 'parent']), false, false, false);
|
||||
return fs.writeFileAsync(cpath, cacheData).catch((err) => {
|
||||
winston.error('Unable to write to cache! Performance may be affected.');
|
||||
return true;
|
||||
@@ -257,7 +255,7 @@ module.exports = {
|
||||
* @return {String} The full cache path.
|
||||
*/
|
||||
getCachePath(entryPath) {
|
||||
return path.join(this._cachePath, farmhash.fingerprint32(entryPath) + '.bson');
|
||||
return path.join(this._cachePath, farmhash.fingerprint32(entryPath) + '.json');
|
||||
},
|
||||
|
||||
/**
|
||||
|
@@ -2,6 +2,8 @@
|
||||
|
||||
var fs = require('fs'),
|
||||
path = require('path'),
|
||||
loki = require('lokijs'),
|
||||
Promise = require('bluebird'),
|
||||
_ = require('lodash');
|
||||
|
||||
/**
|
||||
@@ -12,7 +14,9 @@ var fs = require('fs'),
|
||||
module.exports = {
|
||||
|
||||
_uploadsPath: './repo/uploads',
|
||||
_uploadsThumbsPath: './data/thumbs',
|
||||
_uploadsFolders: [],
|
||||
_uploadsDb: null,
|
||||
|
||||
/**
|
||||
* Initialize Local Data Storage model
|
||||
@@ -20,22 +24,88 @@ module.exports = {
|
||||
* @param {Object} appconfig The application config
|
||||
* @return {Object} Local Data Storage model instance
|
||||
*/
|
||||
init(appconfig, skipFolderCreation = false) {
|
||||
init(appconfig, mode = 'server') {
|
||||
|
||||
let self = this;
|
||||
|
||||
self._uploadsPath = path.join(ROOTPATH, appconfig.datadir.db, 'uploads');
|
||||
self._uploadsPath = path.resolve(ROOTPATH, appconfig.datadir.repo, 'uploads');
|
||||
self._uploadsThumbsPath = path.resolve(ROOTPATH, appconfig.datadir.db, 'thumbs');
|
||||
|
||||
// Create data directories
|
||||
|
||||
if(!skipFolderCreation) {
|
||||
self.createBaseDirectories(appconfig);
|
||||
// Start in full or bare mode
|
||||
|
||||
switch(mode) {
|
||||
case 'agent':
|
||||
//todo
|
||||
break;
|
||||
case 'server':
|
||||
self.createBaseDirectories(appconfig);
|
||||
break;
|
||||
case 'ws':
|
||||
self.initDb(appconfig);
|
||||
break;
|
||||
}
|
||||
|
||||
return self;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Initialize Uploads DB
|
||||
*
|
||||
* @param {Object} appconfig The application config
|
||||
* @return {boolean} Void
|
||||
*/
|
||||
initDb(appconfig) {
|
||||
|
||||
let self = this;
|
||||
|
||||
let dbReadyResolve;
|
||||
let dbReady = new Promise((resolve, reject) => {
|
||||
dbReadyResolve = resolve;
|
||||
});
|
||||
|
||||
// Initialize Loki.js
|
||||
|
||||
let dbModel = {
|
||||
Store: new loki(path.join(appconfig.datadir.db, 'uploads.db'), {
|
||||
env: 'NODEJS',
|
||||
autosave: true,
|
||||
autosaveInterval: 15000
|
||||
}),
|
||||
onReady: dbReady
|
||||
};
|
||||
|
||||
// Load Models
|
||||
|
||||
dbModel.Store.loadDatabase({}, () => {
|
||||
|
||||
dbModel.Files = dbModel.Store.getCollection('Files');
|
||||
if(!dbModel.Files) {
|
||||
dbModel.Files = dbModel.Store.addCollection('Files', {
|
||||
indices: ['category', 'folder']
|
||||
});
|
||||
}
|
||||
|
||||
dbReadyResolve();
|
||||
|
||||
});
|
||||
|
||||
self._uploadsDb = dbModel;
|
||||
|
||||
return true;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the thumbnails folder path.
|
||||
*
|
||||
* @return {String} The thumbs path.
|
||||
*/
|
||||
getThumbsPath() {
|
||||
return this._uploadsThumbsPath;
|
||||
},
|
||||
|
||||
/**
|
||||
* Creates a base directories (Synchronous).
|
||||
*
|
||||
@@ -99,6 +169,77 @@ module.exports = {
|
||||
*/
|
||||
getUploadsFolders() {
|
||||
return this._uploadsFolders;
|
||||
},
|
||||
|
||||
/**
|
||||
* Sets the uploads files.
|
||||
*
|
||||
* @param {Array<Object>} arrFiles The uploads files
|
||||
* @return {Void} Void
|
||||
*/
|
||||
setUploadsFiles(arrFiles) {
|
||||
|
||||
let self = this;
|
||||
|
||||
if(_.isArray(arrFiles) && arrFiles.length > 0) {
|
||||
self._uploadsDb.Files.clear();
|
||||
self._uploadsDb.Files.insert(arrFiles);
|
||||
self._uploadsDb.Files.ensureIndex('category', true);
|
||||
self._uploadsDb.Files.ensureIndex('folder', true);
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the uploads files.
|
||||
*
|
||||
* @param {String} cat Category type
|
||||
* @param {String} fld Folder
|
||||
* @return {Array<Object>} The files matching the query
|
||||
*/
|
||||
getUploadsFiles(cat, fld) {
|
||||
|
||||
return this._uploadsDb.Files.find({
|
||||
'$and': [{ 'category' : cat },{ 'folder' : fld }]
|
||||
});
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Generate thumbnail of image
|
||||
*
|
||||
* @param {String} sourcePath The source path
|
||||
* @return {Promise<Object>} Promise returning the resized image info
|
||||
*/
|
||||
generateThumbnail(sourcePath, destPath) {
|
||||
|
||||
let sharp = require('sharp');
|
||||
|
||||
return sharp(sourcePath)
|
||||
.withoutEnlargement()
|
||||
.resize(150,150)
|
||||
.background('white')
|
||||
.embed()
|
||||
.flatten()
|
||||
.toFormat('png')
|
||||
.toFile(destPath);
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* Gets the image metadata.
|
||||
*
|
||||
* @param {String} sourcePath The source path
|
||||
* @return {Object} The image metadata.
|
||||
*/
|
||||
getImageMetadata(sourcePath) {
|
||||
|
||||
let sharp = require('sharp');
|
||||
|
||||
return sharp(sourcePath).metadata();
|
||||
|
||||
}
|
||||
|
||||
};
|
@@ -22,7 +22,7 @@ module.exports = {
|
||||
init(appconfig) {
|
||||
|
||||
let self = this;
|
||||
let dbPath = path.resolve(ROOTPATH, appconfig.datadir.db, 'search-index');
|
||||
let dbPath = path.resolve(ROOTPATH, appconfig.datadir.db, 'search');
|
||||
|
||||
searchIndex({
|
||||
deletable: true,
|
||||
|
Reference in New Issue
Block a user