From 48a36390d9ac8b02c82d279547673fd453930509 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Wed, 22 Jan 2025 13:56:03 +0100 Subject: [PATCH] fix counting functionality for directory rename --- src/s3.ts | 13 +++++++++---- src/s3contents.ts | 20 +++++++++++++++++--- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index 0254e22..9c35ea1 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -355,14 +355,15 @@ export const checkS3Object = async ( s3Client: S3Client, bucketName: string, root: string, - path?: string + path?: string, + isDir?: boolean ): Promise => { // checking the existance of an S3 object if (path) { await s3Client.send( new HeadObjectCommand({ Bucket: bucketName, - Key: PathExt.join(root, path) + Key: PathExt.join(root, path) + (isDir === true ? '/' : '') }) ); } @@ -622,8 +623,12 @@ export const countS3ObjectNameAppearances = async ( if (Contents) { Contents.forEach(c => { - const fileName = c - .Key!.replace((root ? root + '/' : '') + (path ? path + '/' : ''), '') + let fileName = + c.Key![c.Key!.length - 1] === '/' + ? c.Key!.substring(0, c.Key!.length - 1) + : c.Key!; + fileName = fileName + .replace((root ? root + '/' : '') + (path ? path + '/' : ''), '') .split('/')[0]; if ( fileName.substring(0, originalName.length + 1).includes(originalName) diff --git a/src/s3contents.ts b/src/s3contents.ts index 7eba626..9fde6a0 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -447,9 +447,20 @@ 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); + await checkS3Object( + this._s3Client, + this._name, + this._root, + newLocalPath, + isDir + ); newFileName = await this.incrementName(newLocalPath, this._name); } catch (error) { // HEAD request failed for this file name, continue, as name doesn't already exist. @@ -493,8 +504,11 @@ export class Drive implements Contents.IDrive { let originalName: string = ''; // check if we are dealing with a directory - if (isDir) { - localPath = localPath.substring(0, localPath.length - 1); + if (isDir === true) { + localPath = + localPath[localPath.length - 1] === '/' + ? localPath.substring(0, localPath.length - 1) + : localPath; originalName = PathExt.basename(localPath); } // dealing with a file