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>
<body>
<h1>Discord Video Sharing</h1>
<p id="info"></p>
<script src="../dist/scripts/renderer.js"></script>
</body>

View file

@ -15,6 +15,8 @@ const createWindow = () => {
win.loadFile(path.join(path.resolve(__dirname, ".."), "pages", "index.html"));
win.webContents.openDevTools(); // debug
return win;
};
const moviesFilter = {
@ -22,16 +24,6 @@ const moviesFilter = {
extensions: ["mp4", "mkv"],
} 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 oldFile = path.parse(ogFile);
return path.join(oldFile.dir, `${part}`.concat(oldFile.base));
@ -43,18 +35,33 @@ const mergeAudio = (file: string) => {
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}"`
);
return outFile;
};
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 */
ipcMain.handle("ffmpeg", () => ffmpegPath);
ipcMain.handle("argv", () => process.argv);
ipcMain.handle("allowedExtensions", () => moviesFilter);
ipcMain.handle("askFile", () => askFile());
ipcMain.handle("mergeAudio", (_, file: string) => mergeAudio(file));
ipcMain.handle("exit", async () => app.quit());
createWindow();
ipcMain.handle("confirmation", async (_, text: string) => confirmation(text));
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {

View file

@ -2,10 +2,10 @@ import { contextBridge, ipcRenderer } from "electron";
/* Context bridge */
contextBridge.exposeInMainWorld("internals", {
ffmpeg: () => ipcRenderer.invoke("ffmpeg"),
argv: () => ipcRenderer.invoke("argv"),
allowedExtensions: () => ipcRenderer.invoke("allowedExtensions"),
askFile: () => ipcRenderer.invoke("askFile"),
mergeAudio: (file: string) => ipcRenderer.invoke("mergeAudio", file),
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: {
ffmpeg: () => Promise<string>;
argv: () => Promise<string[]>;
allowedExtensions: () => Promise<{
extensions: string[];
}>;
askFile: () => Promise<string[]>;
exit: () => any;
mergeAudio: (filename: string) => Promise<void>;
exit: () => Promise<void>;
mergeAudio: (filename: string) => Promise<string>;
confirmation: (text: string) => Promise<void>;
};
/** Search for a file */
const getFile = async () => {
const allowedExtensions = (await internals.allowedExtensions()).extensions;
const argv = await internals.argv();
@ -27,9 +28,11 @@ const getFile = async () => {
return file.join("");
};
/** Main function */
const main = async () => {
const file = await getFile();
document.getElementById("info").innerText = file.concat();
await internals.mergeAudio(file);
const newFile = await internals.mergeAudio(file);
await internals.confirmation(`File ok @ ${newFile}!`);
await internals.exit();
};
main();