Skip to content

Commit

Permalink
feat: webdav并行检查
Browse files Browse the repository at this point in the history
  • Loading branch information
bangbang93 committed Jun 2, 2024
1 parent 84bcd2d commit 82f0792
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions src/storage/webdav.storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import colors from 'colors/safe.js'
import type {Request, Response} from 'express'
import {Agent} from 'node:https'
import pMap from 'p-map'
import {join} from 'path'
import rangeParser from 'range-parser'
import {createClient, type FileStat, type WebDAVClient} from 'webdav'
Expand Down Expand Up @@ -94,24 +95,32 @@ export class WebdavStorage implements IStorage {
}
return [...manifest.values()]
}
const queue = [this.basePath]
let queue = [this.basePath]
do {
const dir = queue.pop()
if (!dir) break
const entries = (await this.client.getDirectoryContents(dir)) as FileStat[]
entries.sort((a, b) => a.basename.localeCompare(b.basename))
logger.trace(`checking ${dir}`)
for (const entry of entries) {
if (entry.type === 'directory') {
queue.push(entry.filename)
continue
}
const file = manifest.get(entry.basename)
if (file && file.size === entry.size) {
this.files.set(entry.basename, {size: entry.size, path: entry.filename})
manifest.delete(entry.basename)
}
}
const nextQueue = [] as string[]
await pMap(
queue,
async (dir) => {
const entries = (await this.client.getDirectoryContents(dir)) as FileStat[]
entries.sort((a, b) => a.basename.localeCompare(b.basename))
logger.trace(`checking ${dir}`)
for (const entry of entries) {
if (entry.type === 'directory') {
nextQueue.push(entry.filename)
continue
}
const file = manifest.get(entry.basename)
if (file && file.size === entry.size) {
this.files.set(entry.basename, {size: entry.size, path: entry.filename})
manifest.delete(entry.basename)
}
}
},
{
concurrency: 10,
},
)
queue = nextQueue
} while (queue.length !== 0)
return [...manifest.values()]
}
Expand Down

0 comments on commit 82f0792

Please sign in to comment.