From e5f4a5479718cbcb361ab0dfcd1e3fde23f5eff5 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Tue, 22 Oct 2024 13:03:23 +0200 Subject: [PATCH] apply promise handling suggestion for renaming --- src/s3contents.ts | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/s3contents.ts b/src/s3contents.ts index 0d8822b..1b1fc5a 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -427,30 +427,22 @@ export class Drive implements Contents.IDrive { ): Promise { let newFileName = PathExt.basename(newLocalPath); - await checkS3Object(this._s3Client, this._name, this._root, newLocalPath) - .then(async () => { - console.log('Name already exists, constructing new name for object.'); - // construct new incremented name - newFileName = await this.incrementName(newLocalPath, this._name); - }) - .catch(() => { - // function throws error as the file name doesn't exist - console.log( - "Name doesn't already exist, so it can be used to rename object." - ); - }) - .finally(async () => { - // once the name has been incremented if needed, proceed with the renaming - data = await renameS3Objects( - this._s3Client, - this._name, - this._root, - oldLocalPath, - newLocalPath, - newFileName, - this._registeredFileTypes - ); - }); + try { + await checkS3Object(this._s3Client, this._name, this._root, newLocalPath); + newFileName = await this.incrementName(newLocalPath, this._name); + } catch (error) { + // HEAD request failed for this file name, continue, as name doesn't already exist. + } finally { + data = await renameS3Objects( + this._s3Client, + this._name, + this._root, + oldLocalPath, + newLocalPath, + newFileName, + this._registeredFileTypes + ); + } Contents.validateContentsModel(data); this._fileChanged.emit({