sindresorhus / electron-dl

Simplified file downloads for your Electron app
MIT License
1.15k stars 138 forks source link

cannot read 'getOwnerBrowserWindow' #151

Open NovaAppsInc opened 2 years ago

NovaAppsInc commented 2 years ago

So I'm making a converter app with ffmpeg, and it with convert the file and so I will save that file into a folder called converted.

so after it gets converted I try and download it using a the path of the converted file it would be like converted/convert.file format selected by user*

and I get this error

I'm trying to do this through renderer to main from a stackoverflow question here

renderer:

const ipc = require("electron").ipcRenderer;
const fs = require("fs");
const proc = require("child_process")
const $ = require("jquery");
const randomString = require("random-string");

const btn = document.getElementById("upload");
const selected = document.getElementById("format");
const info = document.getElementById("info");
let pp = document.querySelector(".pp");
let img = document.createElement("img");
let down = document.createElement("div");
down.innerHTML = "Your converted file has been downloaded to your <span>Downloads</span folder";
img.id = "previ";

var format = 'png';
var dir = "./converted"

selected.addEventListener("change", () => {
    var text = selected.options[selected.selectedIndex].text;
    format = text;
})

btn.addEventListener("click", (event) => {
    ipc.send("open");
});

var randID = randomString()

ipc.on("selected-file", (event, paths) => {
    if(info.hasChildNodes()) {
       $(down).detach();
    }
    $(info).append(`<div id=${randID} class="alert alert_succ"><span>${paths}</span> is being converted :)</div>`);
    if(!fs.existsSync(dir)) {
        fs.mkdirSync(dir)
    }

    //converter using ffmpeg command
    proc.exec(`ffmpeg -i "${paths}" converted/convert.${format}`, function(error, stdout, stderr) {
        img.src = `converted/convert.${format}`;
        pp.appendChild(img);

        ipc.on("saved", () => {
            fs.rmSync(dir, { recursive: true, force: true });
        });
        ipc.send("save", `converted/convert.${format}`)
        ipc.send("download", {
            url: dir + `convert.${format}`,
            properties: {directory: "Downloads"}
        });

        ipc.on("download complete", (event, file) => {
            console.log(file); // Full file path
        });
        $(info).append(down);

        $(`#${randID}`).detach();

        if(error !== null) {
            console.log(error);
        }
    })
})

main:

const {app, BrowserWindow, ipcMain, dialog} = require('electron');
const {download} = require("electron-dl");
let mainWindow;
let ipc = ipcMain;
const fs = require("fs")

function createWindow () {
    mainWindow = new BrowserWindow({
        width: 855,
        height: 655,
        minWidth: 855,
        minHeight: 655,
        darkTheme: true,
        show: false,
        thickFrame: true,
        autoHideMenuBar: true,
        backgroundColor: '#222222',
        webPreferences: {
            nodeIntegration: true,
            devTools: true,
            contextIsolation: false
        },
        scrollBounce: true
    });

    mainWindow.webContents.on('did-finish-load', function() {
        mainWindow.show();
    });
    mainWindow.loadFile('index.html');
    mainWindow.on('closed', () => {
        mainWindow = null;
    });
    mainWindow.webContents.openDevTools()
    // mainWindow.removeMenu();
}

app.on('ready', createWindow);

app.on('window-all-closed', function () {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', function () {
    if (mainWindow === null) {
        createWindow();
    }
});

ipc.on("download", (event, info) => {
    download(BrowserWindow.getFocusedWindow(), info.url, info.properties).then(dl => window.webContents.send("download complete", dl.getSavePath()));
});

ipc.on("open", () => {
    if(process.platform === "linux" || process.platform === "win32") {
        dialog.showOpenDialog(null, {
            properties:['openFile'],
            defaultPath: app.getPath("pictures"),
            buttonLabel: "select"
        }).then((r) => {
            mainWindow.webContents.send("selected-file", r.filePaths[0])
        }).catch((err) => {
            console.log(err);
        })
    } else if(process.platform === "darwin") {
        dialog.showOpenDialog(null, {
            properties:['openFile']
        }).then((r) => {
            mainWindow.webContents.send("selected-file", r.filePaths[0])
        }).catch((err) => {
            console.log(err);
        })
    }
});
aleciavogel commented 1 year ago

I am experiencing the same error.

ipcMain.on("downloadFile", async (event, data) => {
    const { href, filename } = data;
    DevLogger.log("**** Downloading file: " + path.join(app.getAppPath(), href);
    const win = BrowserWindow.getFocusedWindow();

    // Note: Both return 1, the browser window is NOT null
    console.log(event.sender.id, win.id);

    await download(win, href, {
      openFolderWhenDone: true,
      saveAs: true,
      filename,
    });
  });