Skip to content

Commit

Permalink
feat: webdav存储忽略空文件
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Apr 19, 2024
1 parent b78095b commit 3dcbf38
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/storage/alist-webdav.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions src/storage/webdav.storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class WebdavStorage implements IStorage {
protected readonly basePath: string

protected files = new Map<string, {size: number; path: string}>()
protected emptyFiles = new Set<string>()

constructor(storageConfig: unknown) {
try {
Expand Down Expand Up @@ -50,6 +51,10 @@ export class WebdavStorage implements IStorage {
}

public async writeFile(path: string, content: Buffer, fileInfo: IFileInfo): Promise<void> {
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})
}
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3dcbf38

Please sign in to comment.