fix(storage): remove empty local embedding directories (#527)

This commit is contained in:
Yiling-J
2026-11-12 09:22:48 +08:00
committed by GitHub
parent 00fa5fed75
commit 0325435125
3 changed files with 32 additions and 1 deletions
+12
View File
@@ -651,6 +651,18 @@ describe('S3Service implementations', () => {
expect(size).toBe(11)
})
it('removes empty embedding chunk directories after deleting the last chunk', async () => {
const key = 'files/asset-1/tmp-embedding-chunks/chunk.mp4'
await localS3.putObject('test-bucket', key, 'chunk', 5)
expect(await localS3.deleteObject('test-bucket', key)).toBe(1)
expect(fs.existsSync(path.join(TEST_BASE_PATH, 'test-bucket', 'files', 'asset-1'))).toBe(
false,
)
expect(fs.existsSync(path.join(TEST_BASE_PATH, 'test-bucket', 'files'))).toBe(true)
})
it('should write Node.js Readable stream in putObject without corrupting to [object Object]', async () => {
const { Readable } = await import('stream')
const stream = Readable.from(['hello ', 'node ', 'stream'])
+18 -1
View File
@@ -939,7 +939,6 @@ export class LocalStorageService implements S3Service {
const filePath = this.getFilePath(bucket, key)
try {
await fs.promises.unlink(filePath)
return 1
} catch (e: unknown) {
if (e instanceof Error && (e as NodeJS.ErrnoException).code === 'ENOENT') {
// Ignore if file doesn't exist
@@ -947,6 +946,24 @@ export class LocalStorageService implements S3Service {
}
throw e
}
const bucketPath = path.resolve(this.basePath, bucket)
let directory = path.dirname(filePath)
// Preserve the bucket and its top-level directories, such as files/.
while (
directory.startsWith(`${bucketPath}${path.sep}`) &&
path.dirname(directory) !== bucketPath
) {
try {
await fs.promises.rmdir(directory)
} catch (e: unknown) {
const code = e instanceof Error ? (e as NodeJS.ErrnoException).code : undefined
if (code === 'ENOTEMPTY' || code === 'EEXIST') break
if (code !== 'ENOENT') throw e
}
directory = path.dirname(directory)
}
return 1
}
async deletePrefix(bucket: string, prefix: string): Promise<number> {