Skip to content

Commit

Permalink
fix: 同步前检查存储是否可以正常写入,避免无效同步
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed May 12, 2024
1 parent 93a8b57 commit 1ff4cb3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export async function bootstrap(version: string): Promise<void> {
const cluster = new Cluster(config.clusterSecret, version, tokenManager)
await cluster.init()

const storageReady = await cluster.storage.check()
if (!storageReady) {
throw new Error('存储异常')
}

const configuration = await cluster.getConfiguration()
const files = await cluster.getFileList()
logger.info(`${files.files.length} files`)
Expand Down
6 changes: 5 additions & 1 deletion src/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ interface ICounters {
bytes: number
}

const whiteListDomain = ['localhost', 'bangbang93.com']
const whiteListDomain = ['localhost', 'bangbang93.com', '192.168']

// eslint-disable-next-line @typescript-eslint/naming-convention
const __dirname = dirname(fileURLToPath(import.meta.url))
Expand Down Expand Up @@ -158,6 +158,10 @@ export class Cluster {
}

public async syncFiles(fileList: IFileList, syncConfig: OpenbmclapiAgentConfiguration['sync']): Promise<void> {
const storageReady = await this.storage.check()
if (!storageReady) {
throw new Error('存储异常')
}
const missingFiles = await this.storage.getMissingFiles(fileList.files)
if (missingFiles.length === 0) {
return
Expand Down
3 changes: 3 additions & 0 deletions src/storage/base.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import {FileStorage} from './file.storage.js'

export interface IStorage {
init?(): Promise<void>

check(): Promise<boolean>

writeFile(path: string, content: Buffer, fileInfo: IFileInfo): Promise<void>

exists(path: string): Promise<boolean>
Expand Down
21 changes: 17 additions & 4 deletions src/storage/file.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Bluebird from 'bluebird'
import colors from 'colors/safe.js'
import type {Request, Response} from 'express'
import fse from 'fs-extra'
import {readdir, stat, unlink} from 'fs/promises'
import {access, readdir, rm, stat, unlink, writeFile} from 'fs/promises'
import {min} from 'lodash-es'
import {join, sep} from 'path'
import {logger} from '../logger.js'
Expand All @@ -13,20 +13,33 @@ import type {IStorage} from './base.storage.js'
export class FileStorage implements IStorage {
constructor(public readonly cacheDir: string) {}

public async check(): Promise<boolean> {
try {
await access(this.cacheDir)
await writeFile(join(this.cacheDir, '.check'), '')
return true
} catch (e) {
logger.error(e, '存储检查异常')
return false
} finally {
await rm(join(this.cacheDir, '.check'), {recursive: true, force: true})
}
}

public async writeFile(path: string, content: Buffer): Promise<void> {
await fse.outputFile(join(this.cacheDir, path), content)
}

public async exists(path: string): Promise<boolean> {
return fse.pathExists(join(this.cacheDir, path))
return await fse.pathExists(join(this.cacheDir, path))
}

public getAbsolutePath(path: string): string {
return join(this.cacheDir, path)
}

public async getMissingFiles(files: IFileInfo[]): Promise<IFileInfo[]> {
return Bluebird.filter(
return await Bluebird.filter(
files,
async (file) => {
const st = await stat(join(this.cacheDir, hashToFilename(file.hash))).catch(() => null)
Expand Down Expand Up @@ -70,7 +83,7 @@ export class FileStorage implements IStorage {
res.attachment(name)
}
const path = this.getAbsolutePath(hashPath)
return new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {
res.sendFile(path, {maxAge: '30d'}, (err) => {
let bytes = res.socket?.bytesWritten ?? 0
if (!err || err?.message === 'Request aborted' || err?.message === 'write EPIPE') {
Expand Down
12 changes: 12 additions & 0 deletions src/storage/webdav.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@ export class WebdavStorage implements IStorage {
}
}

public async check(): Promise<boolean> {
try {
await this.client.putFileContents(join(this.basePath, '.check'), Buffer.from(Date.now().toString()))
return true
} catch (e) {
logger.error(e, '存储检查异常')
return false
} finally {
await this.client.deleteFile(join(this.basePath, '.check'))
}
}

public async writeFile(path: string, content: Buffer, fileInfo: IFileInfo): Promise<void> {
if (content.length === 0) {
this.emptyFiles.add(path)
Expand Down

0 comments on commit 1ff4cb3

Please sign in to comment.