Skip to content

Commit

Permalink
installBinaries properly functioning unzipping
Browse files Browse the repository at this point in the history
  • Loading branch information
DMDComposer committed Mar 14, 2022
1 parent c3995d6 commit 949a552
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 50 deletions.
146 changes: 112 additions & 34 deletions loadingWindow/installBinaries.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,157 @@
const { app, BrowserWindow } = require("electron")
const { app } = require("electron")
const { resolve } = require("path")
const { platform } = require("os")
const { execSync } = require("child_process")
const Sudoer = require("electron-sudo").default
const request = require("request")
const { createWriteStream, rm } = require("fs")
const { createWriteStream, rm, rmSync, existsSync } = require("fs")

const sleep = (ms = 0) => new Promise((res) => setTimeout(res, ms))
const appData = resolve(app.getPath("userData"), "tmpDownload.zip")

// binaries to Download
const macFFmpeg = "https://evermeet.cx/ffmpeg/ffmpeg-5.0.zip"
const macFFprobe = "https://evermeet.cx/ffmpeg/ffprobe-5.0.zip"
const macFFPlay = "https://evermeet.cx/ffmpeg/ffplay-5.0.zip"
// const macFFPlay = "https://evermeet.cx/ffmpeg/ffplay-5.0.zip"
const winFFmpeg = "https://www.gyan.dev/ffmpeg/builds/ffmpeg-release-essentials.zip"

async function downloadBinaries(window) {
console.log("\u001b[" + 31 + "m" + "made it here" + "\u001b[0m")
if (platform() === "darwin") {
const resultsFFmpeg = await Download(window, macFFmpeg, appData)
const resultsFFprobe = await Download(window, macFFprobe, appData)
console.log("\u001b[" + 32 + "m" + "WERE DONE!!!!" + "\u001b[0m")
return resultsFFmpeg && resultsFFprobe
}
const results = await Download(window, winFFmpeg, appData)
// const results = await Download2(window)
console.log("\u001b[" + 32 + "m" + "WERE DONE!!!!" + "\u001b[0m")
return results
}

async function Download(window, url, path) {
console.log("downloading")
// updateHeader("Downloading FFmpeg & FFprobe") // update window header

window.webContents.send("update/statusTitle", "Downloading...")
let received_bytes = 0
let total_bytes = 0

const req = request({
method: "GET",
uri: url,
})
return new Promise((resolve, reject) => {
const req = request({
method: "GET",
uri: url,
})

req.pipe(createWriteStream(path))
req.pipe(createWriteStream(path))

req.on("response", (data) => {
console.log("\u001b[" + 32 + "m" + JSON.stringify(data.headers) + "\u001b[0m")
total_bytes = parseInt(data.headers["content-length"])
})
req.on("response", (data) => {
console.log("\u001b[" + 32 + "m" + JSON.stringify(data.headers) + "\u001b[0m")
total_bytes = parseInt(data.headers["content-length"])
})

req.on("data", (chunk) => {
received_bytes += chunk.length
const percentage_downloaded = (received_bytes / total_bytes) * 100
console.log("\u001b[" + 32 + "m" + received_bytes + "\u001b[0m")
window.webContents.send("update/progressBar", percentage_downloaded.toFixed(2))
window.webContents.send("update/statusTitle", "Downloading...")
})
req.on("data", (chunk) => {
received_bytes += chunk.length
const percentage_downloaded = (received_bytes / total_bytes) * 100
console.log("\u001b[" + 32 + "m" + received_bytes + "\u001b[0m")
window.webContents.send("update/progressBar", percentage_downloaded.toFixed(2))
})

req.on("end", async () => {
await Install(window, path)
console.log("done downloading")
return true
req.on("end", async () => {
const results = await Install(window, path)
console.log("done downloading")
resolve(results)
})
console.log("down here")
})
console.log("down here")
}

async function Install(window, path) {
console.log("\u001b[" + 33 + "m" + "in the install" + "\u001b[0m")
window.webContents.send("update/statusTitle", "Unzipping...")

// make sure zip has finished building.
await sleep(2000)

const AdmZip = require("adm-zip")
const zip = new AdmZip(path)
const zipExtractPath =
platform() === "darwin"
? "/usr/local/bin/"
: "D:/Users/Dillon/Downloads/New Folder (3)"

try {
console.log(path)
zip.extractAllTo(zipExtractPath, true)
} catch (e) {
if (e.code == "EACCES") {
console.log("Cannot create directory, permission denied!")

try {
console.log("we're in this catch")
const execCMD = `cp -R "${path.replace(/\s/g, "\\ ")}" "${zipExtractPath.replace(
/\s/g,
"\\ "
)}tmpDownload.zip"`
let options = { name: "electron sudo application" }
const sudoer = new Sudoer(options)

zip.extractAllTo("D:/Users/Dillon/Downloads/New Folder (3)", true)
setTimeout(() => CleanUp(window, path), 2000)
let cp = await sudoer.exec(execCMD)

console.log(cp.stdout)
} catch (error) {
console.log(error)
return false
}

/*
// Granting both read and write permission
const result = chmodSync(zipExtractPath, 0o777)
// Check the file mode
console.log("Current File Mode:", statSync(zipExtractPath).mode)
console.log("Trying to extract files to path")
try {
zip.extractAllTo(zipExtractPath, true)
} catch (error) {
if (e.code === "EACCES")
console.log("Cannot create directory, permission STILL denied!")
}
*/
} else {
console.log(e.code)
try {
console.log("we're trying manual Unzipping")
const execMacCMD = `ditto -xk "${path.replace(
/\s/g,
"\\ "
)}" "${zipExtractPath.replace(/\s/g, "\\ ")}"`
const execWinCMD = `tar -xf "${path.replace(
/\s/g,
"\\ "
)}" "${zipExtractPath.replace(/\s/g, "\\ ")}"`
let options = { name: "electron sudo application" }
const sudoer = new Sudoer(options)

let cp = await sudoer.exec(platform() === "darwin" ? execMacCMD : execWinCMD)

console.log(cp.stdout)
} catch (error) {
console.log(error)
return false
}
}
}
console.log("outside now")
const results = await CleanUp(window, path)
return results
}

function CleanUp(window, path) {
async function CleanUp(window, path) {
window.webContents.send("update/statusTitle", "Cleaning up install...")
await sleep(2000)
console.log(`\u001b[${35}mremoving: ${path}\u001b[0m`)
rm(path, { recursive: true }, (err) => {
if (err) console.log(err)
console.log("tmpDownload.zip has been successfully deleted")
})
setTimeout(() => sleep(100), 2000)
const results = await rmSync(path, { recursive: true })
console.log("\u001b[" + 33 + "m" + "rmSync completed " + "\u001b[0m")
await sleep(2000)
return true
}

Expand Down
1 change: 0 additions & 1 deletion loadingWindow/loading.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,4 @@ <h3>v1.0.1 Application Is Starting...</h3>

</body>
<script src="updater.js" type="module"></script>
<script src="installBinaries.js" type="module"></script>
</html>
43 changes: 42 additions & 1 deletion macUpdater.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const fetch = require("electron-fetch").default
const { extname } = require("path")

const appData = resolve(app.getPath("userData"), "tmpDownload.zip")

async function GetUpdateURL(options) {
let json
let git_api = `https://api.github.com/repos/${options.gitUsername}/${options.gitRepo}/releases/latest`
Expand Down Expand Up @@ -31,8 +33,47 @@ async function GetUpdateURL(options) {
})
}

async function downloadDMG(window, url, path) {
let path = appData
console.log("downloading")
// updateHeader("Downloading FFmpeg & FFprobe") // update window header
let received_bytes = 0
let total_bytes = 0

const req = request({
method: "GET",
uri: url,
})

req.pipe(createWriteStream(path))

req.on("response", (data) => {
console.log("\u001b[" + 32 + "m" + JSON.stringify(data.headers) + "\u001b[0m")
total_bytes = parseInt(data.headers["content-length"])
})

req.on("data", (chunk) => {
received_bytes += chunk.length
const percentage_downloaded = (received_bytes / total_bytes) * 100
console.log("\u001b[" + 32 + "m" + received_bytes + "\u001b[0m")
window.webContents.send("update/progressBar", percentage_downloaded.toFixed(2))
window.webContents.send("update/statusTitle", "Downloading...")
})

req.on("end", async () => {
await installDMG(window, path)
console.log("done downloading")
return true
})
console.log("down here")
}

async function installDMG(window, path) {
console.log("\u001b[" + 35 + "m" + "installDMG: " + "\u001b[0m")
}

async function macUpdater(options) {
return await GetUpdateURL(options)
}

module.exports = { macUpdater }
module.exports = { macUpdater, downloadDMG }
13 changes: 7 additions & 6 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ async function main() {
const binariesInstalled = await checkForBinaries()
if (!binariesInstalled) await getUserDecisionBinaries()
console.log("\u001b[" + 31 + "m" + "log before checking updates" + "\u001b[0m")
autoUpdater.checkForUpdates()
// autoUpdater.checkForUpdates()
})

/* loadingWindow.on("closed", () => {
Expand Down Expand Up @@ -99,11 +99,11 @@ function showLoadingWindow() {
// check for Binaries ffmpeg/ffprobe
async function checkForBinaries() {
if (platform() === "darwin") {
const ffmpegPath = existsSync("usr/local/bin/ffmpeg")
const ffprobePath = existsSync("usr/local/bin/ffprobe")
const ffmpegPath = existsSync("/usr/local/bin/ffmpeg")
const ffprobePath = existsSync("/usr/local/bin/ffprobe")
return ffmpegPath && ffprobePath
}
const ffmpegPath = existsSync("C:/Program Files (x86)/FFmpeg/bin/ffmpeg.exe")
const ffmpegPath = existsSync("C:/Program Files (x86)/FFmpeg/bin/ffmpeg1.exe")
const ffprobePath = existsSync("C:/Program Files (x86)/FFmpeg/bin/ffprobe.exe")
return ffmpegPath && ffprobePath
}
Expand Down Expand Up @@ -176,12 +176,13 @@ async function getUserDecisionWin(updateInfo) {

async function downloadUpdateMac(updateInfo) {
const { releaseNotes, releaseName, releaseDate, path, files, version, tag } = updateInfo
const { macUpdater } = require("./macUpdater")
const { macUpdater, downloadDMG } = require("./macUpdater")
let options = {
gitUsername: "DMDComposer",
gitRepo: "MW_AudioSplitter",
}
// console.log("\u001b[" + 31 + "m" + (await macUpdater(options)) + "\u001b[0m")
const macDmgLink = await macUpdater(options)
const downloadDmg = await downloadDMG(loadingWindow, macDmgLink)
}

autoUpdater.on("download-progress", (progressObj) => {
Expand Down
Loading

0 comments on commit 949a552

Please sign in to comment.