82 lines
2.3 KiB
JavaScript
82 lines
2.3 KiB
JavaScript
const Constants = require('../../constants');
|
|
const fs = require('fs');
|
|
const helpers = require('../../helpers');
|
|
const path = require('path');
|
|
|
|
const getDirectories = source => {
|
|
try {
|
|
return fs.readdirSync(source, {withFileTypes: true})
|
|
.filter(dirent => dirent.isDirectory())
|
|
.map(dirent => dirent.name);
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
const addListeners = ipcMain => {
|
|
ipcMain.on(Constants.IPC_Get_State, event => {
|
|
helpers.mkDirByPathSync(helpers.getDataDirectory());
|
|
|
|
let data = {};
|
|
const configFile = path.join(helpers.getDataDirectory(), 'settings.json');
|
|
if (fs.existsSync(configFile)) {
|
|
data = JSON.parse(fs.readFileSync(configFile, 'utf8'));
|
|
} else {
|
|
data.Release = Constants.DEFAULT_RELEASE;
|
|
data.Version = -1;
|
|
}
|
|
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
if (!data[provider]) {
|
|
data[provider] = {
|
|
AutoMount: false,
|
|
AutoRestart: true,
|
|
MountLocation: '',
|
|
}
|
|
}
|
|
}
|
|
|
|
data.RemoteMounts = data.RemoteMounts || [];
|
|
data.S3Mounts = data.S3Mounts || [];
|
|
|
|
const remoteItems = getDirectories(path.join(helpers.getRepertoryDirectory(), 'remote'));
|
|
for (const dir of remoteItems) {
|
|
const name = 'Remote' + dir.replace('_', ':');
|
|
if (!data.RemoteMounts || data.RemoteMounts.indexOf(name) === -1) {
|
|
data.RemoteMounts.push(name);
|
|
data[name] = {
|
|
AutoMount: false,
|
|
AutoRestart: true,
|
|
MountLocation: '',
|
|
};
|
|
}
|
|
}
|
|
|
|
const s3Items = getDirectories(path.join(helpers.getRepertoryDirectory(), 's3'));
|
|
for (const dir of s3Items) {
|
|
const name = 'S3' + dir;
|
|
if (!data.S3Mounts || data.S3Mounts.indexOf(name) === -1) {
|
|
data.S3Mounts.push(name);
|
|
data[name] = {
|
|
AutoMount: false,
|
|
AutoRestart: true,
|
|
MountLocation: '',
|
|
};
|
|
}
|
|
}
|
|
event.sender.send(Constants.IPC_Get_State_Reply, {
|
|
data,
|
|
});
|
|
});
|
|
|
|
ipcMain.on(Constants.IPC_Save_State, (event, data) => {
|
|
helpers.mkDirByPathSync(helpers.getDataDirectory());
|
|
const configFile = path.join(helpers.getDataDirectory(), 'settings.json');
|
|
fs.writeFileSync(configFile, JSON.stringify(data.State), 'utf8');
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
addListeners
|
|
};
|