From 939a9e8e9833021e303f786681d1e3dcf5704afd Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Tue, 21 Jan 2025 13:09:52 +0100 Subject: [PATCH] fix directory copy functionality --- src/s3.ts | 3 ++- src/s3contents.ts | 24 ++++++++++++++---------- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index a83da95..889616b 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -674,7 +674,8 @@ export async function isDirectory( // listing contents given a path, to check if it is a directory const command = new ListObjectsV2Command({ Bucket: bucketName, - Prefix: objectPath + '/' + Prefix: + objectPath[objectPath.length - 1] === '/' ? objectPath : objectPath + '/' }); const { Contents } = await s3Client.send(command); diff --git a/src/s3contents.ts b/src/s3contents.ts index ebb1c1f..29b792c 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -423,10 +423,15 @@ export class Drive implements Contents.IDrive { options: Contents.ICreateOptions = {} ): Promise { let newFileName = PathExt.basename(newLocalPath); + const isDir: boolean = await isDirectory( + this._s3Client, + this._name, + oldLocalPath + ); try { await checkS3Object(this._s3Client, this._name, this._root, newLocalPath); - newFileName = await this.incrementName(newLocalPath, this._name); + newFileName = await this.incrementName(newLocalPath, this._name, isDir); } catch (error) { // HEAD request failed for this file name, continue, as name doesn't already exist. } finally { @@ -457,19 +462,14 @@ export class Drive implements Contents.IDrive { * * @param bucketName - The name of the bucket where content is moved. * - * @param root - The root of the bucket, if it exists. + * @param isDir - Whether the object is a directory or a file. */ - async incrementName(localPath: string, bucketName: string) { - const isDir: boolean = await isDirectory( - this._s3Client, - bucketName, - localPath - ); + async incrementName(localPath: string, bucketName: string, isDir: boolean) { let fileExtension: string = ''; let originalName: string = ''; // check if we are dealing with a directory - if (isDir) { + if (isDir === true) { localPath = localPath.substring(0, localPath.length - 1); originalName = PathExt.basename(localPath); } @@ -571,7 +571,11 @@ export class Drive implements Contents.IDrive { (isDir ? '/' : ''); // getting incremented name of Copy in case of duplicates - const incrementedName = await this.incrementName(newFilePath, bucketName); + const incrementedName = await this.incrementName( + newFilePath, + bucketName, + isDir + ); return incrementedName; }