Skip to content

Commit

Permalink
feat: faster local dir listing, macOS backgrounding, bump deps
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Sep 21, 2024
1 parent 44e5a05 commit 2b76990
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 48 deletions.
28 changes: 16 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,18 @@
"wait-on": "^7.2.0"
},
"dependencies": {
"@filen/network-drive": "^0.9.16",
"@filen/network-drive": "^0.9.17",
"@filen/s3": "^0.2.39",
"@filen/sdk": "^0.1.167",
"@filen/sync": "^0.1.66",
"@filen/web": "^0.1.43",
"@filen/sync": "^0.1.71",
"@filen/web": "^0.1.44",
"@filen/webdav": "^0.2.52",
"axios": "^1.7.2",
"cors": "^2.8.5",
"diskusage-ng": "^1.0.4",
"electron-updater": "^6.2.1",
"express": "^4.19.2",
"fast-glob": "^3.3.2",
"find-free-ports": "^3.1.1",
"fs-extra": "^11.2.0",
"lodash": "^4.17.21",
Expand Down
4 changes: 3 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ export class FilenDesktop {
this.initializeSDK()

app.on("window-all-closed", () => {
app.quit()
if (process.platform !== "darwin") {
app.quit()
}
})

app.on("second-instance", () => {
Expand Down
9 changes: 3 additions & 6 deletions src/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { type DirDownloadType } from "@filen/sdk/dist/types/api/v3/dir/download"
import pathModule from "path"
import fs from "fs-extra"
import { v4 as uuidv4 } from "uuid"
import { isPortInUse, canStartServerOnIPAndPort, type SerializedError, getDiskType } from "../utils"
import { isPortInUse, canStartServerOnIPAndPort, type SerializedError, getDiskType, getLocalDirectorySize } from "../utils"
import { type SyncMessage } from "@filen/sync/dist/types"
import { getTrayIcon, getAppIcon, getOverlayIcon } from "../assets"
import { type ProgressInfo, type UpdateDownloadedEvent } from "electron-updater"
Expand Down Expand Up @@ -397,12 +397,9 @@ export class IPC {
}

const logsPath = await filenLogsPath()
const dir = await fs.readdir(logsPath, {
recursive: false,
encoding: "utf-8"
})
const dir = await getLocalDirectorySize(logsPath)

if (dir.length === 0) {
if (dir.items === 0) {
return
}

Expand Down
74 changes: 74 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import axios from "axios"
import https from "https"
import { exec } from "child_process"
import pathModule from "path"
import fs from "fs-extra"
import FastGlob, { type Entry } from "fast-glob"

/**
* Chunk large Promise.all executions.
Expand Down Expand Up @@ -365,3 +367,75 @@ export function findLinuxDiskByPath(drives: LinuxDrive[], filePath: string): Lin

return null
}

export async function getLocalDirectorySize(path: string): Promise<{
items: number
size: number
}> {
return new Promise<{
items: number
size: number
// eslint-disable-next-line no-async-promise-executor
}>(async (resolve, reject) => {
try {
let didError = false
let didErrorErr: Error = new Error("Could not read local directory.")
let size = 0
let items = 0
const stream = FastGlob.stream("**/*", {
dot: true,
onlyDirectories: false,
onlyFiles: false,
throwErrorOnBrokenSymbolicLink: false,
cwd: path,
followSymbolicLinks: false,
deep: Infinity,
fs,
suppressErrors: false,
stats: true,
unique: true,
objectMode: true
})

stream.on("error", err => {
didError = true
didErrorErr = err

reject(err)
})

if (didError) {
reject(didErrorErr)

return
}

for await (const entry of stream) {
if (didError) {
break
}

const entryItem = entry as unknown as Required<Entry>

if (entryItem.dirent.isFile()) {
size += entryItem.stats.size
}

items += 1
}

if (didError) {
reject(didErrorErr)

return
}

resolve({
items,
size
})
} catch (e) {
reject(e)
}
})
}
31 changes: 5 additions & 26 deletions src/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import WebDAV from "./webdav"
import S3 from "./s3"
import NetworkDrive from "./networkDrive"
import diskusage from "diskusage-ng"
import { promiseAllChunked, serializeError } from "../utils"
import { serializeError, getLocalDirectorySize } from "../utils"
import pathModule from "path"
import fs from "fs-extra"
import Sync from "./sync"
Expand Down Expand Up @@ -116,27 +116,9 @@ export class Worker {
return 0
}

const dir = await fs.readdir(cachePath, {
recursive: true,
encoding: "utf-8"
})
let total = 0

await promiseAllChunked(
dir.map(async entry => {
try {
const stat = await fs.stat(pathModule.join(cachePath, entry))

if (stat.isFile()) {
total += stat.size
}
} catch {
// Noop
}
})
)
const dir = await getLocalDirectorySize(cachePath)

return total
return dir.size
}

public async networkDriveCleanupLocalDir(): Promise<void> {
Expand Down Expand Up @@ -487,12 +469,9 @@ export class Worker {
throw new Error(`Cannot read at path ${path}.`)
}

const dir = await fs.readdir(path, {
recursive: true,
encoding: "utf-8"
})
const dir = await getLocalDirectorySize(path)

this.invokeResponse(message.data.id, message.data.channel, dir.length)
this.invokeResponse(message.data.id, message.data.channel, dir.items)
} catch (e) {
this.invokeError(message.data.id, message.data.channel, e instanceof Error ? e : new Error(JSON.stringify(e)))
}
Expand Down

0 comments on commit 2b76990

Please sign in to comment.