add checkFileExists method in file storage service (#12229)
This commit is contained in:
@ -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>;
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user