Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into galactose
Browse files Browse the repository at this point in the history
  • Loading branch information
toto04 committed Sep 21, 2023
2 parents 4d2dcc2 + 4811b7f commit 17c16e9
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 236 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "webeep-sync",
"productName": "WeBeep Sync",
"version": "1.0.2",
"version": "1.0.3",
"description": "keep your webeep files synced",
"repository": {
"type": "git",
Expand Down Expand Up @@ -43,7 +43,7 @@
"copy-webpack-plugin": "^11.0.0",
"css-loader": "^6.7.1",
"dotenv": "^16.0.3",
"electron": "^21.1.1",
"electron": "^22.3.24",
"eslint": "^8.25.0",
"eslint-config-prettier": "^8.6.0",
"eslint-plugin-import": "^2.20.0",
Expand All @@ -56,7 +56,7 @@
"svg-inline-loader": "^0.8.2",
"ts-loader": "^9.4.1",
"typescript": "^4.8.4",
"webpack": "^5.74.0"
"webpack": "^5.76.0"
},
"dependencies": {
"dompurify": "^2.4.0",
Expand All @@ -73,4 +73,4 @@
"sass": "^1.55.0",
"sass-loader": "^13.1.0"
}
}
}
22 changes: 13 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ const { debug, log, error } = createLogger("APP")

const DEV = process.argv.includes("--dev")

// Increase the max heap size
app.commandLine.appendSwitch("js-flags", "--max-old-space-size=8192")

// Handle creating/removing shortcuts on Windows when installing/uninstalling.
if (require("electron-squirrel-startup")) {
// eslint-disable-line global-require
Expand Down Expand Up @@ -75,12 +72,19 @@ async function setLoginItem(openAtLogin: boolean) {
if (process.platform === "linux") return

await app.whenReady()
debug(`Setting openAtLogin to ${openAtLogin}`)
app.setLoginItemSettings({
openAtLogin,
openAsHidden: true,
...windowsLoginSettings,
})

const loginItemSettings = app.getLoginItemSettings(windowsLoginSettings)
// only set the login item if it's different from the current one
if (openAtLogin !== loginItemSettings.openAtLogin) {
debug(`Setting openAtLogin to ${openAtLogin}`)
app.setLoginItemSettings({
openAtLogin,
openAsHidden: true,
...windowsLoginSettings,
})
} else {
debug(`openAtLogin is already ${openAtLogin}`)
}
}

loginManager.on("token", async () => {
Expand Down
30 changes: 22 additions & 8 deletions src/modules/download.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import path from "path"
import fs from "fs/promises"
import { createWriteStream } from "fs"
import stream from "stream/promises"
import { EventEmitter } from "events"
import got, { CancelableRequest, Response } from "got"
import got from "got"

import { createLogger } from "./logger"
import { FileInfo, moodleClient } from "./moodle"
Expand Down Expand Up @@ -57,7 +59,7 @@ export class DownloadManager extends EventEmitter {
private totalUntilNow = 0 // size of all completed downloads

currentDownloads: {
request: CancelableRequest<Response<string>>
cancel: () => void
progress: FileProgress
}[] = []

Expand Down Expand Up @@ -92,7 +94,7 @@ export class DownloadManager extends EventEmitter {
}

private cancelAllRequests() {
for (const download of this.currentDownloads) download.request.cancel()
for (const download of this.currentDownloads) download.cancel()
}

/**
Expand Down Expand Up @@ -167,7 +169,8 @@ export class DownloadManager extends EventEmitter {
const absolutePath = path.join(downloadPath, fullpath)

// make the request to get the file
const request = got.get(file.fileurl, {
const reqAc = new AbortController()
const request = got.stream(file.fileurl, {
searchParams: {
token: loginManager.token, // for some god forsaken reason it's token and not wstoken
},
Expand All @@ -176,7 +179,7 @@ export class DownloadManager extends EventEmitter {
// prepare the download object, this is what will be pushed in the current downloads
// to keep track of the progress
const download: (typeof this.currentDownloads)[number] = {
request,
cancel: () => reqAc.abort(),
progress: {
absolutePath,
filename: file.filename,
Expand All @@ -191,17 +194,27 @@ export class DownloadManager extends EventEmitter {
download.progress.downloaded = transferred
})

const res = await request

try {
await fs.mkdir(path.dirname(absolutePath), { recursive: true })
await fs.writeFile(absolutePath, res.rawBody)
await stream.pipeline(request, createWriteStream(absolutePath), {
signal: reqAc.signal,
})
await fs.utimes(
absolutePath,
new Date(),
new Date(file.timemodified * 1000)
)
} catch (e) {
switch (e.name) {
case "AbortError":
debug(`Cancelled request for file ${fullpath}`)
throw e
case "RequestError":
case "HTTPError":
case "TimeoutError":
throw e
}

if (e.code === "EISDIR") {
try {
await fs.rm(e.path, { recursive: true, force: true })
Expand Down Expand Up @@ -272,6 +285,7 @@ export class DownloadManager extends EventEmitter {
this.cancelAllRequests()
switch (e.name) {
case "CancelError":
case "AbortError":
return SyncResult.stopped

case "RequestError":
Expand Down
Loading

0 comments on commit 17c16e9

Please sign in to comment.