add checkFileExists method in file storage service (#12229)

This commit is contained in:
Etienne
2025-05-22 17:09:21 +02:00
committed by GitHub
parent 08e32017fb
commit 7cc0a7ae72
6 changed files with 57 additions and 27 deletions

View File

@ -21,4 +21,8 @@ export interface StorageDriver {
from: { folderPath: string; filename?: string };
to: { folderPath: string; filename?: string };
}): Promise<void>;
checkFileExists(params: {
folderPath: string;
filename: string;
}): Promise<boolean>;
}

View File

@ -3,11 +3,11 @@ import * as fs from 'fs/promises';
import { dirname, join } from 'path';
import { Readable } from 'stream';
import { StorageDriver } from 'src/engine/core-modules/file-storage/drivers/interfaces/storage-driver.interface';
import {
FileStorageException,
FileStorageExceptionCode,
} from 'src/engine/core-modules/file-storage/interfaces/file-storage-exception';
import { StorageDriver } from 'src/engine/core-modules/file-storage/drivers/interfaces/storage-driver.interface';
export interface LocalDriverOptions {
storagePath: string;
@ -162,4 +162,17 @@ export class LocalDriver implements StorageDriver {
}): Promise<void> {
await this.copy(params, true);
}
async checkFileExists(params: {
folderPath: string;
filename: string;
}): Promise<boolean> {
const filePath = join(
this.options.storagePath,
params.folderPath,
params.filename,
);
return existsSync(filePath);
}
}

View File

@ -395,4 +395,26 @@ export class S3Driver implements StorageDriver {
return this.s3Client.createBucket(args);
}
async checkFileExists(params: {
folderPath: string;
filename: string;
}): Promise<boolean> {
try {
await this.s3Client.send(
new HeadObjectCommand({
Bucket: this.bucketName,
Key: `${params.folderPath}/${params.filename}`,
}),
);
} catch (error) {
if (error instanceof NotFound) {
return false;
}
throw error;
}
return true;
}
}

View File

@ -47,4 +47,11 @@ export class FileStorageService implements StorageDriver {
}): Promise<void> {
return this.driver.download(params);
}
checkFileExists(params: {
folderPath: string;
filename: string;
}): Promise<boolean> {
return this.driver.checkFileExists(params);
}
}