diff --git a/apps/server/src/integrations/storage/drivers/local.driver.ts b/apps/server/src/integrations/storage/drivers/local.driver.ts index f2553733..e3da7700 100644 --- a/apps/server/src/integrations/storage/drivers/local.driver.ts +++ b/apps/server/src/integrations/storage/drivers/local.driver.ts @@ -25,6 +25,16 @@ export class LocalDriver implements StorageDriver { } } + async copy(fromFilePath: string, toFilePath: string): Promise { + try { + if (await this.exists(fromFilePath)) { + await fs.copy(fromFilePath, toFilePath); + } + } catch (err) { + throw new Error(`Failed to copy file: ${(err as Error).message}`); + } + } + async read(filePath: string): Promise { try { return await fs.readFile(this._fullPath(filePath)); diff --git a/apps/server/src/integrations/storage/drivers/s3.driver.ts b/apps/server/src/integrations/storage/drivers/s3.driver.ts index 78f7548c..41feb365 100644 --- a/apps/server/src/integrations/storage/drivers/s3.driver.ts +++ b/apps/server/src/integrations/storage/drivers/s3.driver.ts @@ -1,5 +1,6 @@ import { S3StorageConfig, StorageDriver, StorageOption } from '../interfaces'; import { + CopyObjectCommand, DeleteObjectCommand, GetObjectCommand, HeadObjectCommand, @@ -39,6 +40,22 @@ export class S3Driver implements StorageDriver { } } + async copy(fromFilePath: string, toFilePath: string): Promise { + try { + if (await this.exists(fromFilePath)) { + await this.s3Client.send( + new CopyObjectCommand({ + Bucket: this.config.bucket, + CopySource: `${this.config.bucket}/${fromFilePath}`, + Key: toFilePath, + }), + ); + } + } catch (err) { + throw new Error(`Failed to copy file: ${(err as Error).message}`); + } + } + async read(filePath: string): Promise { try { const command = new GetObjectCommand({ diff --git a/apps/server/src/integrations/storage/interfaces/storage-driver.interface.ts b/apps/server/src/integrations/storage/interfaces/storage-driver.interface.ts index 419587f4..6f18ff45 100644 --- a/apps/server/src/integrations/storage/interfaces/storage-driver.interface.ts +++ b/apps/server/src/integrations/storage/interfaces/storage-driver.interface.ts @@ -1,6 +1,8 @@ export interface StorageDriver { upload(filePath: string, file: Buffer): Promise; + copy(fromFilePath: string, toFilePath: string): Promise; + read(filePath: string): Promise; exists(filePath: string): Promise;