Add confirmation

This commit is contained in:
Mylloon 2023-07-30 02:01:16 +02:00
parent 5976fa37a1
commit 12c3dee7ad
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 30 additions and 21 deletions

View file

@ -17,7 +17,6 @@
</head> </head>
<body> <body>
<h1>Discord Video Sharing</h1> <h1>Discord Video Sharing</h1>
<p id="info"></p>
<script src="../dist/scripts/renderer.js"></script> <script src="../dist/scripts/renderer.js"></script>
</body> </body>

View file

@ -15,6 +15,8 @@ const createWindow = () => {
win.loadFile(path.join(path.resolve(__dirname, ".."), "pages", "index.html")); win.loadFile(path.join(path.resolve(__dirname, ".."), "pages", "index.html"));
win.webContents.openDevTools(); // debug win.webContents.openDevTools(); // debug
return win;
}; };
const moviesFilter = { const moviesFilter = {
@ -22,16 +24,6 @@ const moviesFilter = {
extensions: ["mp4", "mkv"], extensions: ["mp4", "mkv"],
} as FileFilter; } as FileFilter;
/** Ask user a file */
const askFile = async () => {
return (
await dialog.showOpenDialog({
filters: [moviesFilter],
properties: ["openFile", "dontAddToRecent"],
})
).filePaths;
};
const getNewFilename = (ogFile: string, part: string) => { const getNewFilename = (ogFile: string, part: string) => {
const oldFile = path.parse(ogFile); const oldFile = path.parse(ogFile);
return path.join(oldFile.dir, `${part}`.concat(oldFile.base)); return path.join(oldFile.dir, `${part}`.concat(oldFile.base));
@ -43,18 +35,33 @@ const mergeAudio = (file: string) => {
child_process.exec( child_process.exec(
`${ffmpegPath} -i "${file}" -filter_complex "[0:a]amerge=inputs=2[a]" -ac 1 -map 0:v -map "[a]" -c:v copy "${outFile}"` `${ffmpegPath} -i "${file}" -filter_complex "[0:a]amerge=inputs=2[a]" -ac 1 -map 0:v -map "[a]" -c:v copy "${outFile}"`
); );
return outFile;
}; };
app.whenReady().then(() => { app.whenReady().then(() => {
const win = createWindow();
/** Ask user a file */
const askFile = async () => {
return dialog.showOpenDialogSync(win, {
filters: [moviesFilter],
properties: ["openFile", "dontAddToRecent"],
});
};
/** Send confirmation to user */
const confirmation = async (message: string) => {
dialog.showMessageBoxSync(win, { message });
};
/* Context bridge */ /* Context bridge */
ipcMain.handle("ffmpeg", () => ffmpegPath);
ipcMain.handle("argv", () => process.argv); ipcMain.handle("argv", () => process.argv);
ipcMain.handle("allowedExtensions", () => moviesFilter); ipcMain.handle("allowedExtensions", () => moviesFilter);
ipcMain.handle("askFile", () => askFile()); ipcMain.handle("askFile", () => askFile());
ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file)); ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file));
ipcMain.handle("exit", async () => app.quit()); ipcMain.handle("exit", async () => app.quit());
ipcMain.handle("confirmation", async (_, text: string) => confirmation(text));
createWindow();
app.on("activate", () => { app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) { if (BrowserWindow.getAllWindows().length === 0) {

View file

@ -2,10 +2,10 @@ import { contextBridge, ipcRenderer } from "electron";
/* Context bridge */ /* Context bridge */
contextBridge.exposeInMainWorld("internals", { contextBridge.exposeInMainWorld("internals", {
ffmpeg: () => ipcRenderer.invoke("ffmpeg"),
argv: () => ipcRenderer.invoke("argv"), argv: () => ipcRenderer.invoke("argv"),
allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"), allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"),
askFile: () => ipcRenderer.invoke("askFile"), askFile: () => ipcRenderer.invoke("askFile"),
mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file), mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file),
exit: () => ipcRenderer.invoke("exit"), exit: () => ipcRenderer.invoke("exit"),
confirmation: (text: string) => ipcRenderer.invoke("confirmation", text),
}); });

View file

@ -1,15 +1,16 @@
/* Context bridge types */ /** Context bridge types */
let internals: { let internals: {
ffmpeg: () => Promise<string>;
argv: () => Promise<string[]>; argv: () => Promise<string[]>;
allowedExtensions: () => Promise<{ allowedExtensions: () => Promise<{
extensions: string[]; extensions: string[];
}>; }>;
askFile: () => Promise<string[]>; askFile: () => Promise<string[]>;
exit: () => any; exit: () => Promise<void>;
mergeAudio: (filename: string) => Promise<void>; mergeAudio: (filename: string) => Promise<string>;
confirmation: (text: string) => Promise<void>;
}; };
/** Search for a file */
const getFile = async () => { const getFile = async () => {
const allowedExtensions = (await internals.allowedExtensions()).extensions; const allowedExtensions = (await internals.allowedExtensions()).extensions;
const argv = await internals.argv(); const argv = await internals.argv();
@ -27,9 +28,11 @@ const getFile = async () => {
return file.join(""); return file.join("");
}; };
/** Main function */
const main = async () => { const main = async () => {
const file = await getFile(); const file = await getFile();
document.getElementById("info").innerText = file.concat(); const newFile = await internals.mergeAudio(file);
await internals.mergeAudio(file); await internals.confirmation(`File ok @ ${newFile}!`);
await internals.exit();
}; };
main(); main();