Skip to content

Commit

Permalink
feat: cleanup local trash
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Aug 7, 2024
1 parent 27b4c9e commit 95e3430
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/sync",
"version": "0.1.30",
"version": "0.1.31",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
53 changes: 51 additions & 2 deletions src/lib/sync.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import FilenSDK, { type PauseSignal } from "@filen/sdk"
import { type SyncPair, type SyncMode } from "../types"
import { SYNC_INTERVAL } from "../constants"
import { SYNC_INTERVAL, LOCAL_TRASH_NAME } from "../constants"
import { LocalFileSystem, LocalTree, type LocalTreeError } from "./filesystems/local"
import { RemoteFileSystem, RemoteTree } from "./filesystems/remote"
import Deltas from "./deltas"
import Tasks, { type TaskError } from "./tasks"
import State from "./state"
import { postMessageToMain } from "./ipc"
import Ignorer from "../ignorer"
import { serializeError } from "../utils"
import { serializeError, promiseAllChunked } from "../utils"
import type SyncWorker from ".."
import Lock from "./lock"
import pathModule from "path"
import fs from "fs-extra"

/**
* Sync
Expand Down Expand Up @@ -51,6 +53,7 @@ export class Sync {
public taskErrors: TaskError[] = []
public localTrashDisabled: boolean
public localTreeErrors: LocalTreeError[] = []
public cleaningLocalTrash: boolean = false

/**
* Creates an instance of Sync.
Expand All @@ -77,6 +80,8 @@ export class Sync {
this.state = new State(this)
this.ignorer = new Ignorer(this, "ignorer")
this.lock = new Lock(this)

this.cleanupLocalTrash()
}

public async smokeTest(): Promise<void> {
Expand Down Expand Up @@ -119,6 +124,50 @@ export class Sync {
}
}

public cleanupLocalTrash(): void {
setInterval(async () => {
if (this.cleaningLocalTrash) {
return
}

this.cleaningLocalTrash = true

try {
const localTrashPath = pathModule.join(this.syncPair.localPath, LOCAL_TRASH_NAME)

if (await fs.exists(localTrashPath)) {
const now = Date.now()
const dir = await fs.readdir(localTrashPath, {
recursive: false,
encoding: "utf-8"
})

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

if (stat.atimeMs + 86400000 * 30 < now) {
await fs.rm(entryPath, {
force: true,
maxRetries: 60 * 10,
recursive: true,
retryDelay: 100
})
}
})
)
}

this.worker.logger.log("info", "Local trash cleaned", this.syncPair.localPath)
} catch (e) {
this.worker.logger.log("error", e, "sync.cleanupLocalTrash")
} finally {
this.cleaningLocalTrash = false
}
}, 300000)
}

public async initialize(): Promise<void> {
if (this.isInitialized) {
return
Expand Down

0 comments on commit 95e3430

Please sign in to comment.