Skip to content

Commit

Permalink
Merge pull request #19 from ZorrillosDev/go-ipfsd
Browse files Browse the repository at this point in the history
Be sure this lock 'api' file doesnt exists before node boot..
  • Loading branch information
geolffreym authored Jan 28, 2021
2 parents 1f81407 + aabdcea commit 030535a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 25 deletions.
11 changes: 10 additions & 1 deletion public/core/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const path = require('path')
const Ctl = require('ipfsd-ctl')
const settings = require('./settings')
const ipfsConf = require('./settings/ipfs');
const {removeFiles} = require('./utils');

const inDev = Object.is(process.env.ENV, 'dev')
const resolveIpfsPaths = () => {
Expand All @@ -28,14 +29,22 @@ const resolveIpfsPaths = () => {

module.exports = async (ipc) => {
const isInstance = await Ctl.createController({
ipfsOptions: {config: ipfsConf(), repo: path.join(settings.ROOT_HOME, '.ipfs_w')},
ipfsOptions: {config: ipfsConf(), repo: settings.ROOT_IPFS_DIR},
ipfsHttpModule: require('ipfs-http-client'),
ipfsBin: resolveIpfsPaths(),
disposable: false, forceKillTimeout: 2000,
args: ['--enable-pubsub-experiment'],
remote: false, type: 'go'
})

// If api file exists on node setup ipfs-daemon.js line:183 doest spawn process
// Be sure this lock 'api' file doesnt exists before node boot..
const apiLockFile = path.join(settings.ROOT_IPFS_DIR, 'api')
if (fs.existsSync(apiLockFile)) {
log.warn('Removing old `api` file');
await removeFiles(apiLockFile)
}

// Check if running time dir exists
log.warn('Starting node');
ipc.reply('orbit-progress', 'Booting')
Expand Down
2 changes: 1 addition & 1 deletion public/core/orbit.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ module.exports = (ipcMain) => {

open(address, settings = {}) {
return this.orbit.open(address, {
...{overwrite: true, replicate: true},
...{replicate: true, overwrite: true},
...settings
}
)
Expand Down
2 changes: 1 addition & 1 deletion public/core/provs.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const findProv = async (ipfs, key) => {
* @return {Promise<void>}
*/

for await (const cid of ipfs.dht.findProvs(key)) {
for await (const cid of ipfs.dht.findProvs(key, {numProviders: 10})) {
log.info('Connecting to:', cid.id)
const mAddr = cid.addrs.map((m) => `${m.toString()}/p2p/${cid.id}`)

Expand Down
4 changes: 3 additions & 1 deletion public/core/settings/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const fs = require('fs')

const ROOT_DIR = os.tmpdir();
const ROOT_HOME = os.homedir();
const ROOT_URI = 'https://vps1.phillm.net/ipfs/';
const ROOT_URI = 'https://localhost:9090ipfs/';
const ROOT_URI_TORRENT = `${ROOT_URI}`;
const ROOT_STORE = process.env.appPath;
const ROOT_APP = fs.realpathSync(process.cwd());
Expand All @@ -13,6 +13,7 @@ const ROOT_PUBLIC = path.join(ROOT_APP, 'public');
const ROOT_RUNNING_DB = path.join(ROOT_STORE, 'w_alloc')
const ROOT_DB_DIR = path.join(ROOT_RUNNING_DB, 'linvo')
const ROOT_ORBIT_DIR = path.join(ROOT_RUNNING_DB, 'orbit')
const ROOT_IPFS_DIR = path.join(ROOT_HOME, '.ipfs_w')

module.exports = {
ROOT_APP,
Expand All @@ -22,6 +23,7 @@ module.exports = {
ROOT_STORE,
ROOT_PUBLIC,
ROOT_DB_DIR,
ROOT_IPFS_DIR,
ROOT_ORBIT_DIR,
ROOT_RUNNING_DB,
ROOT_TMP_FOLDER,
Expand Down
10 changes: 10 additions & 0 deletions public/core/settings/ipfs.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ module.exports = () => {
"/dns4/node3.delegate.ipfs.io/tcp/443/https"
]
},
"Swarm": {
"ConnMgr": {
"GracePeriod": "20s",
"HighWater": 1500,
"LowWater": 450,
"Type": "basic"
},
"EnableAutoRelay": true,
"EnableRelayHop": false
},
"Discovery": {"MDNS": {"Enabled": true, "Interval": 10}},
"Peering": {
"Peers": [
Expand Down
17 changes: 17 additions & 0 deletions public/core/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const log = require('electron-log');
const rimraf = require('rimraf');

module.exports.removeFiles = (dirOrFIle, options) => {
return new Promise((resolve) => {
rimraf(dirOrFIle, {
...{
disableGlob: true,
maxBusyTries: 20,
emfileWait: 10 * 1000
}, ...options
}, () => {
log.warn('Delete file ' + dirOrFIle);
resolve()
});
})
}
31 changes: 10 additions & 21 deletions public/electron.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const fs = require('fs');
const path = require('path');
const mime = require('mime');
const rimarf = require('rimraf');
const log = require('electron-log');
const {
autoUpdater
Expand All @@ -23,6 +22,7 @@ let win, loadingScreen,
// Dont move appPath from this line
process.env.appPath = appPath;
const {ROOT_TMP_FOLDER} = require(`${__dirname}/core/settings/`);
const {removeFiles} = require(`${__dirname}/core/utils`);
const Orbit = require(`${__dirname}/core/orbit`);
const Auth = require(`${__dirname}/core/auth`);
const gotTheLock = app.requestSingleInstanceLock();
Expand All @@ -34,23 +34,13 @@ dialog.showErrorBox = (title, content) => {
log.info(`${title}\n${content}`);
};

const removeFiles = (dirOrFIle, options) => {
rimarf(dirOrFIle, {
...{
disableGlob: true,
maxBusyTries: 20,
emfileWait: 10 * 1000
}, ...options
}, () => {
log.warn('Delete file ' + dirOrFIle);
});
}, removeCacheDirs = () => {
const removeCacheDirs = () => {
//Auth.removeFromStorage('tmp')
fs.readdirSync(appPath).filter(
fn => fn.startsWith('w_alloc') || fn.startsWith('w_source')
).forEach((file) => {
).forEach(async (file) => {
log.warn('Removing ' + file);
removeFiles(path.join(appPath, file))
await removeFiles(path.join(appPath, file))
});
}, wipeInvalidSync = () => {
Auth.removeFromStorage('peers')
Expand All @@ -60,11 +50,11 @@ const removeFiles = (dirOrFIle, options) => {
}
}, wipeTmpSubs = () => {
//Loop over files in dir
fs.readdir(ROOT_TMP_FOLDER, (err, files) => {
fs.readdir(ROOT_TMP_FOLDER, async (err, files) => {
if (!files || !files.length) return false;
for (const file of files) {
if (/(srt|vtt|zip)$/g.test(file)) {
removeFiles(path.join(ROOT_TMP_FOLDER, file));
await removeFiles(path.join(ROOT_TMP_FOLDER, file));
}
}
})
Expand Down Expand Up @@ -289,13 +279,12 @@ app.whenReady().then(() => {
Orbit(ipcMain);

// Window event
ipcMain.on('rmrf', (dir) => {
removeFiles(dir)
})


ipcMain.on('close', () => app.quit())
ipcMain.on('party', () => {
if (Auth.existKey) removeFiles(Auth.keyFile)
ipcMain.on('party', async () => {
if (Auth.existKey)
await removeFiles(Auth.keyFile)
removeCacheDirs();
})

Expand Down

0 comments on commit 030535a

Please sign in to comment.