Implemented job upload
This commit is contained in:
@@ -1,14 +1,49 @@
|
||||
const { ipcMain } = require("electron");
|
||||
const { ipcMain, dialog } = require("electron");
|
||||
const { StartWatcher, StopWatcher } = require("./file-watcher");
|
||||
const ipcTypes = require("../../src/ipc.types").default;
|
||||
const ipcTypes = require("../../src/ipc.types");
|
||||
const settings = require("electron-settings");
|
||||
const { mainWindow } = require("../main");
|
||||
const _ = require("lodash");
|
||||
|
||||
ipcMain.on(ipcTypes.fileWatcher.toMain.start, async (event, arg) => {
|
||||
ipcMain.on(
|
||||
ipcTypes.default.fileWatcher.toMain.filepathsGet,
|
||||
async (event, object) => {
|
||||
const filePaths = await settings.get("filePaths");
|
||||
event.reply(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
filePaths
|
||||
);
|
||||
}
|
||||
);
|
||||
|
||||
ipcMain.on(ipcTypes.default.fileWatcher.toMain.start, async (event, arg) => {
|
||||
const filePaths = StartWatcher();
|
||||
event.sender.send(ipcTypes.fileWatcher.toRenderer.startSuccess, filePaths);
|
||||
event.sender.send(ipcTypes.fileWatcher.toRenderer.filepathsList, filePaths);
|
||||
// event.sender.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
event.sender.send(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
filePaths
|
||||
);
|
||||
});
|
||||
|
||||
ipcMain.on(ipcTypes.fileWatcher.toMain.stop, async (event, arg) => {
|
||||
ipcMain.on(ipcTypes.default.fileWatcher.toMain.stop, async (event, arg) => {
|
||||
StopWatcher();
|
||||
event.sender.send(ipcTypes.fileWatcher.toRenderer.stopSuccess);
|
||||
event.sender.send(ipcTypes.default.fileWatcher.toRenderer.stopSuccess);
|
||||
});
|
||||
|
||||
ipcMain.on(ipcTypes.default.fileWatcher.toMain.addPath, async (event, arg) => {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
properties: ["openDirectory"],
|
||||
});
|
||||
|
||||
await settings.set(
|
||||
"filePaths",
|
||||
_.union(result.filePaths, await settings.get("filePaths"))
|
||||
);
|
||||
|
||||
const newFilePaths = await settings.get("filePaths");
|
||||
|
||||
event.sender.send(
|
||||
ipcTypes.default.fileWatcher.toRenderer.filepathsList,
|
||||
newFilePaths
|
||||
);
|
||||
});
|
||||
|
||||
@@ -1,15 +1,27 @@
|
||||
const chokidar = require("chokidar");
|
||||
const { ipcMain } = require("electron");
|
||||
const settings = require("electron-settings");
|
||||
const { default: ipcTypes } = require("../../src/ipc.types");
|
||||
const ipcTypes = require("../../src/ipc.types");
|
||||
const path = require("path");
|
||||
const { DecodeEstimate } = require("../decoder/decoder");
|
||||
const { BrowserWindow, Notification } = require("electron");
|
||||
const _ = require("lodash");
|
||||
|
||||
var watcher;
|
||||
|
||||
function StartWatcher() {
|
||||
const filePaths = settings.getSync("filePaths") || [];
|
||||
const filePaths =
|
||||
settings
|
||||
.getSync("filePaths")
|
||||
.map((fp) => path.join(fp, "**.[eE][nN][vV]")) || [];
|
||||
console.log("StartWatcher -> filePaths", filePaths);
|
||||
|
||||
Notification({
|
||||
title: "Watcher started!",
|
||||
body: "Watcher has been started..",
|
||||
}).show();
|
||||
|
||||
watcher = chokidar.watch(filePaths, {
|
||||
ignored: /[\/\\]\./,
|
||||
//ignored: /[\/\\]\./,
|
||||
persistent: true,
|
||||
ignoreInitial: true,
|
||||
awaitWriteFinish: {
|
||||
@@ -18,14 +30,16 @@ function StartWatcher() {
|
||||
},
|
||||
});
|
||||
watcher
|
||||
.on("add", function (path) {
|
||||
.on("add", async function (path) {
|
||||
console.log("File", path, "has been added");
|
||||
HandleNewFile(path);
|
||||
})
|
||||
.on("addDir", function (path) {
|
||||
console.log("Directory", path, "has been added");
|
||||
})
|
||||
.on("change", function (path) {
|
||||
.on("change", async function (path) {
|
||||
console.log("File", path, "has been changed");
|
||||
HandleNewFile(path);
|
||||
})
|
||||
.on("unlink", function (path) {
|
||||
console.log("File", path, "has been removed");
|
||||
@@ -35,27 +49,48 @@ function StartWatcher() {
|
||||
})
|
||||
.on("error", function (error) {
|
||||
console.log("Error happened", error);
|
||||
ipcMain.emit(ipcTypes.fileWatcher.toRenderer.error, error);
|
||||
const b = BrowserWindow.getFocusedWindow();
|
||||
b.webContents.send(ipcTypes.fileWatcher.toRenderer.error, error);
|
||||
})
|
||||
.on("ready", onWatcherReady)
|
||||
.on("raw", function (event, path, details) {
|
||||
// This event should be triggered everytime something happens.
|
||||
console.log("Raw event info:", event, path, details);
|
||||
// console.log("Raw event info:", event, path, details);
|
||||
});
|
||||
return filePaths;
|
||||
}
|
||||
|
||||
function onWatcherReady() {
|
||||
console.log("Ready!");
|
||||
ipcMain.emit(ipcTypes.fileWatcher.toRenderer.startSuccess);
|
||||
const b = BrowserWindow.getFocusedWindow();
|
||||
b.webContents.send(ipcTypes.default.fileWatcher.toRenderer.startSuccess);
|
||||
}
|
||||
|
||||
async function StopWatcher() {
|
||||
await watcher.close();
|
||||
ipcMain.emit(ipcTypes.fileWatcher.toRenderer.stopSuccess);
|
||||
console.log("Watcher Stopped!", watcher);
|
||||
console.log("Watcher Stopped!");
|
||||
}
|
||||
|
||||
exports.StartWatcher = StartWatcher;
|
||||
exports.StopWatcher = StopWatcher;
|
||||
exports.watcher = watcher;
|
||||
|
||||
async function HandleNewFile(path) {
|
||||
const b = BrowserWindow.getAllWindows()[0];
|
||||
b.webContents.send(ipcTypes.default.estimate.toRenderer.estimateDecodeStart);
|
||||
const newJob = await DecodeEstimate(path);
|
||||
const newJobLow = _.transform(newJob, function (result, val, key) {
|
||||
result[key.toLowerCase()] = val;
|
||||
});
|
||||
console.log("HandleNewFile -> newJobLow", newJobLow);
|
||||
|
||||
b.webContents.send(
|
||||
ipcTypes.default.estimate.toRenderer.estimateDecodeSuccess,
|
||||
newJobLow
|
||||
);
|
||||
|
||||
Notification({
|
||||
title: "Job Uploaded",
|
||||
body: "A new job has been uploaded.",
|
||||
}).show();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user