Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions app/back-end/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const Plugins = require('./plugins.js');
const DBUtils = require('./helpers/db.utils.js');
const Site = require('./site.js');
const Utils = require('./helpers/utils.js');
const FileHelper = require('./helpers/file.js');
// List of the Event classes
const EventClasses = require('./events/_modules.js');
// Migration classes
Expand Down Expand Up @@ -52,7 +53,7 @@ class App {
this.initPath = path.join(this.appDir, 'config', 'window-config.json');
this.appConfigPath = path.join(this.appDir, 'config', 'app-config.json');
this.tinymceOverridedConfigPath = path.join(this.appDir, 'config', 'tinymce.override.json');
this.versionData = JSON.parse(fs.readFileSync(__dirname + '/builddata.json', 'utf8'));
this.versionData = JSON.parse(FileHelper.readFileSync(__dirname + '/builddata.json', 'utf8'));
this.windowBounds = null;
this.appConfig = null;
this.tinymceOverridedConfig = {};
Expand Down Expand Up @@ -165,8 +166,8 @@ class App {

// Check if both config.json files exists
if (fs.existsSync(appThemeConfig) && fs.existsSync(userThemeConfig)) {
let appThemeData = JSON.parse(fs.readFileSync(appThemeConfig, 'utf8'));
let userThemeData = JSON.parse(fs.readFileSync(userThemeConfig, 'utf8'));
let appThemeData = JSON.parse(FileHelper.readFileSync(appThemeConfig, 'utf8'));
let userThemeData = JSON.parse(FileHelper.readFileSync(userThemeConfig, 'utf8'));

// If app theme is newer version than the existing one
if(compare(appThemeData.version, userThemeData.version) === 1) {
Expand Down Expand Up @@ -231,7 +232,7 @@ class App {
let themeDir = path.join(siteDir, 'input', 'themes', themes.currentTheme(true));
let themeOverridesDir = path.join(siteDir, 'input', 'themes', themes.currentTheme(true) + '-override');
let themeConfig = Themes.loadThemeConfig(themeConfigPath, themeDir);
let menuStructure = fs.readFileSync(menuConfigPath, 'utf8');
let menuStructure = FileHelper.readFileSync(menuConfigPath, 'utf8');
let parsedMenuStructure = {};

try {
Expand Down Expand Up @@ -283,7 +284,7 @@ class App {

// Load the config
let defaultSiteConfig = JSON.parse(JSON.stringify(defaultAstCurrentSiteConfig));
let siteConfig = fs.readFileSync(configFilePath);
let siteConfig = FileHelper.readFileSync(configFilePath);
siteConfig = JSON.parse(siteConfig);

if (siteConfig.name !== siteName) {
Expand Down Expand Up @@ -437,7 +438,7 @@ class App {
loadConfig () {
// Try to get window bounds
try {
this.windowBounds = JSON.parse(fs.readFileSync(this.initPath, 'utf8'));
this.windowBounds = JSON.parse(FileHelper.readFileSync(this.initPath, 'utf8'));
} catch (e) {
console.log('The window-config.json file will be created');
}
Expand Down Expand Up @@ -490,7 +491,7 @@ class App {

// Try to get application config
try {
this.appConfig = JSON.parse(fs.readFileSync(this.appConfigPath, 'utf8'));
this.appConfig = JSON.parse(FileHelper.readFileSync(this.appConfigPath, 'utf8'));
this.appConfig = Utils.mergeObjects(JSON.parse(JSON.stringify(defaultAstAppConfig)), this.appConfig);
} catch (e) {
if (this.hasPermissionsErrors(e)) {
Expand Down Expand Up @@ -518,7 +519,7 @@ class App {
loadAdditionalConfig () {
// Try to get TinyMCE overrided config
try {
this.tinymceOverridedConfig = JSON.parse(fs.readFileSync(this.tinymceOverridedConfigPath, 'utf8'));
this.tinymceOverridedConfig = JSON.parse(FileHelper.readFileSync(this.tinymceOverridedConfigPath, 'utf8'));
} catch (e) {}

if (this.appConfig.sitesLocation) {
Expand Down
3 changes: 2 additions & 1 deletion app/back-end/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const slug = require('./helpers/slug');
const ImageHelper = require('./helpers/image.helper.js');
const Themes = require('./themes.js');
const Utils = require('./helpers/utils.js');
const FileHelper = require('./helpers/file.js');

/**
* Author Model - used for operations connected with author management
Expand Down Expand Up @@ -366,7 +367,7 @@ class Author extends Model {
let themeConfigPath = path.join(this.application.sitesDir, this.site, 'input', 'config', 'theme.config.json');

if (fs.existsSync(themeConfigPath)) {
let themeConfigString = fs.readFileSync(themeConfigPath, 'utf8');
let themeConfigString = FileHelper.readFileSync(themeConfigPath, 'utf8');
themesHelper.checkAndCleanImages(themeConfigString);
}
}
Expand Down
7 changes: 4 additions & 3 deletions app/back-end/events/app.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs-extra');
const path = require('path');
const FileHelper = require('../helpers/file.js');
const ipcMain = require('electron').ipcMain;
const Themes = require('../themes.js');
const Languages = require('../languages.js');
Expand Down Expand Up @@ -97,7 +98,7 @@ class AppEvents {
* Save app color theme config
*/
ipcMain.on('app-save-color-theme', function (event, theme) {
let appConfig = fs.readFileSync(appInstance.appConfigPath, 'utf8');
let appConfig = FileHelper.readFileSync(appInstance.appConfigPath, 'utf8');

try {
appConfig = JSON.parse(appConfig);
Expand Down Expand Up @@ -440,7 +441,7 @@ class AppEvents {
}

let filePath = path.join(appInstance.app.getPath('logs'), filename);
let fileContent = fs.readFileSync(filePath, 'utf8');
let fileContent = FileHelper.readFileSync(filePath, 'utf8');

event.sender.send('app-log-file-loaded', {
fileContent: fileContent
Expand All @@ -458,7 +459,7 @@ class AppEvents {
return;
}

let appConfig = fs.readFileSync(appInstance.appConfigPath, 'utf8');
let appConfig = FileHelper.readFileSync(appInstance.appConfigPath, 'utf8');

try {
appConfig = JSON.parse(appConfig);
Expand Down
3 changes: 2 additions & 1 deletion app/back-end/events/credits.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs-extra');
const path = require('path');
const ipcMain = require('electron').ipcMain;
const FileHelper = require('../helpers/file.js');

/*
* Events for the IPC communication regarding credits
Expand All @@ -18,7 +19,7 @@ class CreditsEvents {
}

if(fs.existsSync(filePath)) {
licenseText = fs.readFileSync(filePath, 'utf-8');
licenseText = FileHelper.readFileSync(filePath, 'utf-8');
}

event.sender.send('app-license-loaded', licenseText);
Expand Down
7 changes: 4 additions & 3 deletions app/back-end/events/page.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs');
const path = require('path');
const FileHelper = require('../helpers/file.js');
const ipcMain = require('electron').ipcMain;
const Page = require('../page.js');

Expand Down Expand Up @@ -85,7 +86,7 @@ class PageEvents {
let pagesFile = path.join(this.app.sitesDir, siteName, 'input', 'config', 'pages.config.json');

if (fs.existsSync(pagesFile)) {
let pagesHierarchy = JSON.parse(fs.readFileSync(pagesFile, { encoding: 'utf8' }));
let pagesHierarchy = JSON.parse(FileHelper.readFileSync(pagesFile, { encoding: 'utf8' }));
pagesHierarchy = this.removeDuplicatedDataFromHierarchy(pagesHierarchy);
event.sender.send('app-pages-hierarchy-loaded', pagesHierarchy);
} else {
Expand All @@ -104,7 +105,7 @@ class PageEvents {
// Update pages hierarchy during post conversion
ipcMain.on('app-pages-hierarchy-update', (event, postIDs) => {
let pagesFile = path.join(this.app.sitesDir, pagesData.siteName, 'input', 'config', 'pages.config.json');
let pagesHierarchy = JSON.parse(fs.readFileSync(pagesFile, { encoding: 'utf8' }));
let pagesHierarchy = JSON.parse(FileHelper.readFileSync(pagesFile, { encoding: 'utf8' }));

for (let i = 0; i < postIDs.length; i++) {
pagesHierarchy.push({
Expand All @@ -115,7 +116,7 @@ class PageEvents {

pagesHierarchy = this.removeNullDataFromHierarchy(pagesHierarchy);
pagesHierarchy = this.removeDuplicatedDataFromHierarchy(pagesHierarchy);
fs.writeFileSync(pagesFile, JSON.stringify(updatedHierarchy, null, 4), { encoding: 'utf8' });
fs.writeFileSync(pagesFile, JSON.stringify(pagesHierarchy, null, 4), { encoding: 'utf8' });
});
}

Expand Down
7 changes: 4 additions & 3 deletions app/back-end/events/plugins-api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const ipcMain = require('electron').ipcMain;
const fs = require('fs');
const path = require('path');
const FileHelper = require('../helpers/file.js');

/*
* Events for the IPC communication regarding plugins
Expand All @@ -19,7 +20,7 @@ class PluginsApiEvents {
return false;
}

let fileContent = fs.readFileSync(filePath);
let fileContent = FileHelper.readFileSync(filePath);
fileContent = fileContent.toString();
return fileContent;
});
Expand All @@ -34,7 +35,7 @@ class PluginsApiEvents {
return false;
}

let fileContent = fs.readFileSync(filePath);
let fileContent = FileHelper.readFileSync(filePath);
fileContent = fileContent.toString();
return fileContent;
});
Expand All @@ -50,7 +51,7 @@ class PluginsApiEvents {
return false;
}

let fileContent = fs.readFileSync(filePath);
let fileContent = FileHelper.readFileSync(filePath);
fileContent = fileContent.toString();
return fileContent;
});
Expand Down
7 changes: 4 additions & 3 deletions app/back-end/events/site.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const fs = require('fs-extra');
const os = require('os');
const path = require('path');
const FileHelper = require('../helpers/file.js');
const slug = require('./../helpers/slug');
const passwordSafeStorage = require('keytar');
const ipcMain = require('electron').ipcMain;
Expand Down Expand Up @@ -130,7 +131,7 @@ class SiteEvents {
}

let configFile = path.join(appInstance.sitesDir, siteName, 'input', 'config', 'site.config.json');
let oldConfig = fs.readFileSync(configFile, 'utf8');
let oldConfig = FileHelper.readFileSync(configFile, 'utf8');
let themesPath = path.join(appInstance.sitesDir, siteName, 'input', 'themes');
let newThemeConfig = {};
oldConfig = JSON.parse(oldConfig);
Expand Down Expand Up @@ -340,7 +341,7 @@ class SiteEvents {
let themeConfigPath = path.join(appInstance.sitesDir, siteName, 'input', 'config', 'theme.config.json');

if (fs.existsSync(themeConfigPath)) {
let themeConfigString = fs.readFileSync(themeConfigPath, 'utf8');
let themeConfigString = FileHelper.readFileSync(themeConfigPath, 'utf8');
themesHelper.checkAndCleanImages(themeConfigString);
}

Expand Down Expand Up @@ -604,7 +605,7 @@ class SiteEvents {
}

let configPath = path.join(appInstance.sitesDir, siteName, 'input', 'config', 'site.config.json');
let config = fs.readFileSync(configPath, 'utf8');
let config = FileHelper.readFileSync(configPath, 'utf8');

try {
config = JSON.parse(config);
Expand Down
3 changes: 2 additions & 1 deletion app/back-end/events/sync.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs-extra');
const path = require('path');
const FileHelper = require('../helpers/file.js');
const ipcMain = require('electron').ipcMain;

/*
Expand All @@ -23,7 +24,7 @@ class SyncEvents {

saveSyncStatus(status, siteName) {
let configFile = path.join(this.app.sitesDir, siteName, 'input', 'config', 'site.config.json');
let configContent = fs.readFileSync(configFile, 'utf8');
let configContent = FileHelper.readFileSync(configFile, 'utf8');
configContent = JSON.parse(configContent);
configContent.synced = status;

Expand Down
26 changes: 26 additions & 0 deletions app/back-end/helpers/file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const fs = require('fs');

/**
* FileHelper wraps fs.readFileSync to avoid leaking file descriptors.
*/
class FileHelper {
/**
* Reads a file synchronously, ensuring the file descriptor is closed.
* @param {string|Buffer|URL|integer} path - filename or file descriptor
* @param {Object|string} [options] - options or encoding
* @returns {string|Buffer}
*/
static readFileSync(path, options) {
let fd;
try {
fd = fs.openSync(path, 'r');
return fs.readFileSync(fd, options);
} finally {
if (fd !== undefined) {
try { fs.closeSync(fd); } catch (e) { /* ignore */ }
}
}
}
}

module.exports = FileHelper;
3 changes: 2 additions & 1 deletion app/back-end/helpers/updates.helper.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const fs = require('fs');
const FileHelper = require('./file.js');
const https = require('https');

class UpdatesHelper {
Expand Down Expand Up @@ -44,7 +45,7 @@ class UpdatesHelper {

readExistingData () {
if (fs.existsSync(this.filePath)) {
let body = fs.readFileSync(this.filePath, 'utf8');
let body = FileHelper.readFileSync(this.filePath, 'utf8');
this.handleResponse(body);
} else {
this.sendError();
Expand Down
3 changes: 2 additions & 1 deletion app/back-end/helpers/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const fs = require('fs-extra');
const path = require('path');
const FileHelper = require('./file.js');
const normalizePath = require('normalize-path');

/*
Expand Down Expand Up @@ -299,7 +300,7 @@ class UtilsHelper {
}

try {
themeConfig = JSON.parse(fs.readFileSync(themeConfigPath));
themeConfig = JSON.parse(FileHelper.readFileSync(themeConfigPath));
} catch(e) {
console.log('The theme config.json file is corrupted');
return {};
Expand Down
4 changes: 2 additions & 2 deletions app/back-end/helpers/validators/language-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs-extra');
const FileHelper = require('../file.js');

/**
* Checks if language config file meets the requirements
Expand All @@ -7,7 +7,7 @@ const fs = require('fs-extra');
* @returns {boolean|string}
*/
function languageConfigValidator(configPath) {
let configContent = fs.readFileSync(configPath);
let configContent = FileHelper.readFileSync(configPath);
let configParsed = false;

try {
Expand Down
4 changes: 2 additions & 2 deletions app/back-end/helpers/validators/plugin-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const fs = require('fs-extra');
const FileHelper = require('../file.js');

/**
* Checks if plugin config file meets the requirements
Expand All @@ -7,7 +7,7 @@ const fs = require('fs-extra');
* @returns {boolean|string}
*/
function pluginConfigValidator(configPath) {
let configContent = fs.readFileSync(configPath);
let configContent = FileHelper.readFileSync(configPath);
let configParsed = false;

try {
Expand Down
5 changes: 3 additions & 2 deletions app/back-end/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Image instance
*/

const FileHelper = require('./helpers/file.js');
const fs = require('fs-extra');
const path = require('path');
const Model = require('./model.js');
Expand Down Expand Up @@ -207,7 +208,7 @@ class Image extends Model {
let themesHelper = new Themes(this.application, { site: this.site });
let currentTheme = themesHelper.currentTheme();
let siteConfigPath = path.join(this.siteDir, 'input', 'config', 'site.config.json');
let siteConfig = JSON.parse(fs.readFileSync(siteConfigPath));
let siteConfig = JSON.parse(FileHelper.readFileSync(siteConfigPath));
siteConfig = Utils.mergeObjects(defaultSiteConfig, siteConfig);
let imagesQuality = 60;
let alphaQuality = 100;
Expand Down Expand Up @@ -547,7 +548,7 @@ class Image extends Model {
};

// Get content of the SVG image
let svgFileContent = fs.readFileSync(imagePath, 'utf8');
let svgFileContent = FileHelper.readFileSync(imagePath, 'utf8');
// Look for the non-percentage values in the <svg> tag
let svgWidth = svgFileContent.match(/\<svg.*width=['"]{1}(.*?)['"]{1}.*\>/mi);
let svgHeight = svgFileContent.match(/\<svg.*height=['"]{1}(.*?)['"]{1}.*\>/mi);
Expand Down
Loading