Skip to content

Commit

Permalink
feat: 回收文件给个完成提示
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Sep 19, 2024
1 parent cb762de commit f5299ae
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
4 changes: 1 addition & 3 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ export async function bootstrap(version: string): Promise<void> {
throw e
}
logger.info('回收文件')
cluster.storage.gc(files.files).catch((e: unknown) => {
logger.error({err: e}, 'gc error')
})
cluster.gcBackground(files)

cluster.connect()
const proto = config.byoc ? 'http' : 'https'
Expand Down
16 changes: 16 additions & 0 deletions src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {tmpdir} from 'os'
import pMap from 'p-map'
import pRetry from 'p-retry'
import {dirname, join} from 'path'
import prettyBytes from 'pretty-bytes'
import {connect, Socket} from 'socket.io-client'
import {Tail} from 'tail'
import {fileURLToPath} from 'url'
Expand Down Expand Up @@ -516,6 +517,21 @@ export class Cluster {
process.exit(code)
}

public gcBackground(files: IFileList): void {
this.storage
.gc(files.files)
.then((res) => {
if (res.count === 0) {
logger.info('没有过期文件')
} else {
logger.info(`文件回收完成,共删除${res.count}个文件,释放空间${prettyBytes(res.size)}`)
}
})
.catch((e: unknown) => {
logger.error({err: e}, 'gc error')
})
}

private async _enable(): Promise<void> {
let err: unknown
let ack: unknown
Expand Down
4 changes: 2 additions & 2 deletions src/storage/base.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {NextFunction, Request, Response} from 'express'
import {join} from 'path'
import type {Config} from '../config.js'
import {logger} from '../logger.js'
import type {IFileInfo} from '../types.js'
import {IFileInfo, IGCCounter} from '../types.js'
import {AlistWebdavStorage} from './alist-webdav.storage.js'
import {FileStorage} from './file.storage.js'

Expand All @@ -19,7 +19,7 @@ export interface IStorage {

getMissingFiles(files: IFileInfo[]): Promise<IFileInfo[]>

gc(files: {path: string; hash: string; size: number}[]): Promise<void>
gc(files: {path: string; hash: string; size: number}[]): Promise<IGCCounter>

express(hashPath: string, req: Request, res: Response, next?: NextFunction): Promise<{bytes: number; hits: number}>
}
Expand Down
8 changes: 6 additions & 2 deletions src/storage/file.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {readdir, rm, stat, unlink, writeFile} from 'fs/promises'
import {min} from 'lodash-es'
import {join, sep} from 'path'
import {logger} from '../logger.js'
import type {IFileInfo} from '../types.js'
import {IFileInfo, IGCCounter} from '../types.js'
import {hashToFilename} from '../util.js'
import type {IStorage} from './base.storage.js'

Expand Down Expand Up @@ -51,7 +51,8 @@ export class FileStorage implements IStorage {
)
}

public async gc(files: {path: string; hash: string; size: number}[]): Promise<void> {
public async gc(files: {path: string; hash: string; size: number}[]): Promise<IGCCounter> {
const counter = {count: 0, size: 0}
const fileSet = new Set<string>()
for (const file of files) {
fileSet.add(hashToFilename(file.hash))
Expand All @@ -72,9 +73,12 @@ export class FileStorage implements IStorage {
if (!fileSet.has(p.replace(cacheDirWithSep, ''))) {
logger.info(colors.gray(`delete expire file: ${p}`))
await unlink(p)
counter.count++
counter.size += s.size
}
}
} while (queue.length !== 0)
return counter
}

public async express(hashPath: string, req: Request, res: Response): Promise<{bytes: number; hits: number}> {
Expand Down
8 changes: 6 additions & 2 deletions src/storage/webdav.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {createClient, type FileStat, type WebDAVClient} from 'webdav'
import {z} from 'zod'
import {fromZodError} from 'zod-validation-error'
import {logger} from '../logger.js'
import type {IFileInfo} from '../types.js'
import {IFileInfo, IGCCounter} from '../types.js'
import type {IStorage} from './base.storage.js'

const storageConfigSchema = z.object({
Expand Down Expand Up @@ -141,7 +141,8 @@ export class WebdavStorage implements IStorage {
return [...remoteFileList.values()]
}

public async gc(files: {path: string; hash: string; size: number}[]): Promise<void> {
public async gc(files: {path: string; hash: string; size: number}[]): Promise<IGCCounter> {
const counter = {count: 0, size: 0}
const fileSet = new Set<string>()
for (const file of files) {
fileSet.add(file.hash)
Expand All @@ -161,9 +162,12 @@ export class WebdavStorage implements IStorage {
logger.info(colors.gray(`delete expire file: ${entry.filename}`))
await this.client.deleteFile(entry.filename)
this.files.delete(entry.basename)
counter.count++
counter.size += entry.size
}
}
} while (queue.length !== 0)
return counter
}

// eslint-disable-next-line @typescript-eslint/require-await
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ export interface IFileInfo {
size: number
mtime: number
}

export interface IGCCounter {
count: number
size: number
}

0 comments on commit f5299ae

Please sign in to comment.