249 lines
7.1 KiB
JavaScript
249 lines
7.1 KiB
JavaScript
const Constants = require('../../constants');
|
|
const fs = require('fs');
|
|
const helpers = require('../../helpers');
|
|
const os = require('os');
|
|
|
|
let expectedUnmount = {};
|
|
let firstMountCheck = true;
|
|
let manualMountDetection = {};
|
|
let mountedData = {};
|
|
let mountedLocations = [];
|
|
|
|
const clearManualMountDetection = provider => {
|
|
if (manualMountDetection[provider]) {
|
|
clearInterval(manualMountDetection[provider]);
|
|
delete manualMountDetection[provider];
|
|
}
|
|
};
|
|
|
|
const monitorMount = (sender, provider, version, pid, location) => {
|
|
manualMountDetection[provider] = setInterval(() => {
|
|
helpers
|
|
.detectRepertoryMounts(version)
|
|
.then(result => {
|
|
if (result[provider].PID !== pid) {
|
|
if (result[provider].PID === -1) {
|
|
clearManualMountDetection(provider);
|
|
sender.send(Constants.IPC_Unmount_Drive_Reply, {
|
|
data: {
|
|
Expected: expectedUnmount[provider],
|
|
Location: location,
|
|
Provider: provider,
|
|
Error: Error(provider + ' Unmounted').toString(),
|
|
Success: false,
|
|
}
|
|
});
|
|
} else {
|
|
pid = result[provider].PID;
|
|
}
|
|
}
|
|
})
|
|
.catch(e => {
|
|
console.log(e);
|
|
});
|
|
},6000);
|
|
};
|
|
|
|
const unmountAllDrives = () => {
|
|
// Reset mount states
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
clearManualMountDetection(provider);
|
|
expectedUnmount[provider] = true;
|
|
}
|
|
|
|
// Unmount all items
|
|
for (const i in mountedLocations) {
|
|
const data = mountedData[mountedLocations[i]];
|
|
helpers.stopMountProcessSync(data.Version, data.Provider);
|
|
}
|
|
|
|
mountedLocations = [];
|
|
mountedData = {};
|
|
};
|
|
|
|
const addListeners = (ipcMain, setTrayImage, standardIPCReply) => {
|
|
ipcMain.on(Constants.IPC_Check_Mount_Location + '_sync', (event, data) => {
|
|
let response = {
|
|
Success: true,
|
|
Error: ''
|
|
};
|
|
|
|
try {
|
|
if (fs.existsSync(data.Location) && fs.statSync(data.Location).isDirectory()) {
|
|
if (fs.readdirSync(data.Location).length !== 0) {
|
|
response.Success = false;
|
|
response.Error = 'Directory not empty: ' + data.Location;
|
|
}
|
|
} else {
|
|
response.Success = false;
|
|
response.Error = 'Directory not found: ' + data.Location;
|
|
}
|
|
} catch (e) {
|
|
response.Success = false;
|
|
response.Error = e.toString();
|
|
}
|
|
event.returnValue = response;
|
|
});
|
|
|
|
ipcMain.on(Constants.IPC_Detect_Mounts, (event, data) => {
|
|
let driveLetters = {};
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
driveLetters[provider] = [];
|
|
}
|
|
|
|
const grabDriveLetters = (locations) => {
|
|
for (let i = 'c'.charCodeAt(0); i <= 'z'.charCodeAt(0); i++) {
|
|
const drive = (String.fromCharCode(i) + ':').toUpperCase();
|
|
let driveInUse;
|
|
if (Object.keys(locations).length > 0) {
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
driveInUse = locations[provider].startsWith(drive);
|
|
if (driveInUse)
|
|
break;
|
|
}
|
|
}
|
|
if (!driveInUse) {
|
|
try {
|
|
if (!fs.existsSync(drive)) {
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
driveLetters[provider].push(drive);
|
|
}
|
|
}
|
|
} catch (e) {
|
|
}
|
|
}
|
|
}
|
|
|
|
if (Object.keys(locations).length > 0) {
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
if (locations[provider].length > 0) {
|
|
if (!driveLetters[provider].find((driveLetter) => {
|
|
return driveLetter === locations[provider];
|
|
})) {
|
|
driveLetters[provider].push(locations[provider]);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
const setImage = (locations) => {
|
|
let driveInUse;
|
|
if (Object.keys(locations).length > 0) {
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
driveInUse = locations[provider].length > 0;
|
|
if (driveInUse)
|
|
break;
|
|
}
|
|
}
|
|
|
|
setTrayImage(driveInUse)
|
|
};
|
|
|
|
helpers
|
|
.detectRepertoryMounts(data.Version)
|
|
.then((results) => {
|
|
let storageData = {};
|
|
let locations = {};
|
|
for (const provider of Constants.PROVIDER_LIST) {
|
|
storageData[provider] = results[provider] ? results[provider] : {
|
|
Active: false,
|
|
Location: '',
|
|
PID: -1,
|
|
};
|
|
locations[provider] = storageData[provider].Location;
|
|
|
|
if (storageData[provider].PID !== -1) {
|
|
expectedUnmount[provider] = false;
|
|
if (firstMountCheck) {
|
|
monitorMount(event.sender, provider, data.Version, storageData[provider].PID, storageData[provider].Location);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (os.platform() === 'win32') {
|
|
grabDriveLetters(locations);
|
|
}
|
|
|
|
setImage(locations);
|
|
if (firstMountCheck) {
|
|
firstMountCheck = false;
|
|
}
|
|
standardIPCReply(event, Constants.IPC_Detect_Mounts_Reply, {
|
|
DriveLetters: driveLetters,
|
|
Locations: locations,
|
|
});
|
|
})
|
|
.catch(error => {
|
|
if (os.platform() === 'win32') {
|
|
grabDriveLetters({});
|
|
}
|
|
setImage({});
|
|
standardIPCReply(event, Constants.IPC_Detect_Mounts_Reply, {
|
|
DriveLetters: driveLetters,
|
|
}, error);
|
|
});
|
|
});
|
|
|
|
ipcMain.on(Constants.IPC_Mount_Drive, (event, data) => {
|
|
expectedUnmount[data.Provider] = false;
|
|
|
|
if (mountedLocations.indexOf(data.Location) !== -1) {
|
|
console.log(data.Provider + ' already mounted: ' + data.Location);
|
|
} else {
|
|
mountedLocations.push(data.Location);
|
|
mountedData[data.Location] = {
|
|
Version: data.Version,
|
|
Provider: data.Provider,
|
|
};
|
|
const errorHandler = (pid, error) => {
|
|
if (mountedLocations.indexOf(data.Location) !== -1) {
|
|
mountedLocations.splice(mountedLocations.indexOf(data.Location), 1);
|
|
delete mountedData[data.Location];
|
|
}
|
|
|
|
standardIPCReply(event, Constants.IPC_Unmount_Drive_Reply, {
|
|
Expected: expectedUnmount[data.Provider],
|
|
Location: data.Location,
|
|
Provider: data.Provider,
|
|
}, error || Error(data.Provider + ' Unmounted'));
|
|
};
|
|
helpers
|
|
.executeMount(data.Version, data.Provider, data.Location, data.NoConsoleSupported, (error, pid) => {
|
|
errorHandler(pid, error);
|
|
})
|
|
.then(() => {
|
|
standardIPCReply(event, Constants.IPC_Mount_Drive_Reply, {
|
|
Provider: data.Provider,
|
|
});
|
|
})
|
|
.catch(error => {
|
|
errorHandler(-1, error);
|
|
});
|
|
}
|
|
});
|
|
|
|
ipcMain.on(Constants.IPC_Unmount_All_Drives, (event, data) => {
|
|
unmountAllDrives();
|
|
standardIPCReply(event, Constants.IPC_Unmount_All_Drives_Reply);
|
|
});
|
|
|
|
ipcMain.on(Constants.IPC_Unmount_Drive, (event, data) => {
|
|
clearManualMountDetection(data.Provider);
|
|
|
|
expectedUnmount[data.Provider] = true;
|
|
helpers
|
|
.stopMountProcess(data.Version, data.Provider)
|
|
.then(result => {
|
|
console.log(result);
|
|
})
|
|
.catch(e => {
|
|
console.log(e);
|
|
});
|
|
});
|
|
};
|
|
|
|
module.exports = {
|
|
addListeners,
|
|
unmountAllDrives
|
|
}; |