From 6bad25fe95f5bcae12e16e69892882e6dd615cbf Mon Sep 17 00:00:00 2001 From: getlarge Date: Fri, 12 Jul 2024 12:12:29 +0200 Subject: [PATCH] feat(nestjs-tools-file-storage): resolve boolean for fileExists --- packages/file-storage/src/lib/file-storage-fs.class.ts | 2 +- packages/file-storage/src/lib/file-storage-s3.class.ts | 8 ++++++-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/packages/file-storage/src/lib/file-storage-fs.class.ts b/packages/file-storage/src/lib/file-storage-fs.class.ts index cbb2e0d..84192eb 100644 --- a/packages/file-storage/src/lib/file-storage-fs.class.ts +++ b/packages/file-storage/src/lib/file-storage-fs.class.ts @@ -69,7 +69,7 @@ export class FileStorageLocal implements FileStorage { async fileExists(args: FileStorageLocalFileExists): Promise { const { filePath, options = {}, request } = args; const fileName = await this.transformFilePath(filePath, MethodTypes.READ, request, options); - return new Promise((resolve, reject) => stat(fileName, (err) => (err ? reject(err) : resolve(true)))); + return new Promise((resolve) => stat(fileName, (err) => (err ? resolve(false) : resolve(true)))); } async uploadFile(args: FileStorageLocalUploadFile): Promise { diff --git a/packages/file-storage/src/lib/file-storage-s3.class.ts b/packages/file-storage/src/lib/file-storage-s3.class.ts index 9c0cf2f..4839fc9 100644 --- a/packages/file-storage/src/lib/file-storage-s3.class.ts +++ b/packages/file-storage/src/lib/file-storage-s3.class.ts @@ -87,8 +87,12 @@ export class FileStorageS3 implements FileStorage { const { filePath, options = {}, request } = args; const { s3, bucket: Bucket } = this.config; const Key = await this.transformFilePath(filePath, MethodTypes.READ, request, options); - await s3.headObject({ ...options, Key, Bucket }); - return true; + try { + await s3.headObject({ ...options, Key, Bucket }); + return true; + } catch (e) { + return false; + } } async uploadFile(args: FileStorageS3UploadFile): Promise {