Skip to content

Commit d4a964a

Browse files
committed
refactor: silent removeFile if file not exist (#528)
1 parent e8086b6 commit d4a964a

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

packages/core/src/storages/disk-storage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export class DiskStorage extends BaseStorage<DiskFile> {
105105
async delete({ id }: FilePart): Promise<DiskFile[]> {
106106
try {
107107
const file = await this.getMeta(id);
108-
await removeFile(this.getFilePath(file));
108+
removeFile(this.getFilePath(file)).catch(() => null);
109109
await this.deleteMeta(id);
110110
return [{ ...file, status: 'deleted' }];
111111
} catch {}

packages/core/src/utils/fs.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ export async function accessCheck(dir: string): Promise<void> {
2828
* Removes the specified file from the local file system
2929
*/
3030
export async function removeFile(path: string): Promise<void> {
31-
if (fsp.rm) return fsp.rm(path);
32-
return fsp.unlink(path);
31+
if (fsp.rm) return fsp.rm(path, { force: true });
32+
return fsp.unlink(path).catch((err: NodeJS.ErrnoException): void => {
33+
if (err.code !== 'ENOENT') throw err;
34+
});
3335
}
3436

3537
export function truncateFile(path: string, length = 0): Promise<void> {

test/util.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ describe('utils', () => {
8181
it('getWriteStream(not exist , 0)', () => {
8282
expect(() => utils.getWriteStream('', 0)).toThrow();
8383
});
84+
85+
it('removeFile(path)', async () => {
86+
await utils.ensureFile(filepath);
87+
await utils.removeFile(filepath);
88+
expect(fs.existsSync(filepath)).toBe(false);
89+
});
90+
91+
it('removeFile(not exist)', async () => {
92+
await utils.removeFile(filepath);
93+
expect(fs.existsSync(filepath)).toBe(false);
94+
});
8495
});
8596

8697
describe('http', () => {

0 commit comments

Comments
 (0)