diff --git a/src/storage/alist-webdav.storage.ts b/src/storage/alist-webdav.storage.ts index 79d1700..b1be090 100644 --- a/src/storage/alist-webdav.storage.ts +++ b/src/storage/alist-webdav.storage.ts @@ -17,6 +17,10 @@ export class AlistWebdavStorage extends WebdavStorage { }) public async express(hashPath: string, req: Request, res: Response): Promise<{bytes: number; hits: number}> { + if (this.emptyFiles.has(hashPath)) { + res.end() + return {bytes: 0, hits: 1} + } const cachedUrl = await this.redirectUrlCache.get(hashPath) const size = this.getSize(this.files.get(req.params.hash)?.size ?? 0, req.headers.range) if (cachedUrl) { diff --git a/src/storage/webdav.storage.ts b/src/storage/webdav.storage.ts index f57372d..8b82de4 100644 --- a/src/storage/webdav.storage.ts +++ b/src/storage/webdav.storage.ts @@ -23,6 +23,7 @@ export class WebdavStorage implements IStorage { protected readonly basePath: string protected files = new Map() + protected emptyFiles = new Set() constructor(storageConfig: unknown) { try { @@ -50,6 +51,10 @@ export class WebdavStorage implements IStorage { } public async writeFile(path: string, content: Buffer, fileInfo: IFileInfo): Promise { + if (content.length === 0) { + this.emptyFiles.add(path) + return + } await this.client.putFileContents(join(this.basePath, path), content) this.files.set(fileInfo.hash, {size: content.length, path: fileInfo.path}) } @@ -122,6 +127,10 @@ export class WebdavStorage implements IStorage { // eslint-disable-next-line @typescript-eslint/require-await public async express(hashPath: string, req: Request, res: Response): Promise<{bytes: number; hits: number}> { + if (this.emptyFiles.has(hashPath)) { + res.end() + return {bytes: 0, hits: 1} + } const path = join(this.basePath, hashPath) const file = this.client.getFileDownloadLink(path) res.redirect(file)