From 42a6c00956284e370e1220cd4743e4c55e4d69e0 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Tue, 15 Oct 2024 22:56:24 +0200 Subject: [PATCH 01/12] improve operations reliability --- src/s3.ts | 8 ++++++-- src/s3contents.ts | 24 ++++++++++++++++++++---- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index 1a79867..fba1f39 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -214,6 +214,7 @@ export const getS3FileContents = async ( writable: true, type: fileType }; + console.log('S3CONTENTS get file contents finished: ', fileContents); } return data; @@ -265,6 +266,7 @@ export const createS3Object = async ( CacheControl: options ? 'no-cache' : undefined }) ); + console.log('S3CONTENTS save finished'); data = { name: name, @@ -312,9 +314,9 @@ export const deleteS3Objects = async ( if (Contents) { await Promise.all( - Contents.map(c => { + Contents.map(async c => { // delete each file with given path - Private.deleteFile(s3Client, bucketName, c.Key!); + await Private.deleteFile(s3Client, bucketName, c.Key!); }) ); } @@ -323,6 +325,7 @@ export const deleteS3Objects = async ( } command.input.ContinuationToken = NextContinuationToken; } + console.log('S3CONTENTS DELETE multiple objects finished'); }; /** @@ -647,6 +650,7 @@ namespace Private { Key: filePath }) ); + console.log('S3CONTENTS delete file finished'); } /** diff --git a/src/s3contents.ts b/src/s3contents.ts index 9f76579..4bb946a 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -255,6 +255,7 @@ export class Drive implements Contents.IDrive { path: string, options?: Contents.IFetchOptions ): Promise { + console.log('GET: ', path); path = path.replace(this._name + '/', ''); // getting the list of files from the root @@ -291,6 +292,7 @@ export class Drive implements Contents.IDrive { } Contents.validateContentsModel(data); + console.log('GET validate data finish: ', data); return data; } @@ -305,6 +307,7 @@ export class Drive implements Contents.IDrive { async newUntitled( options: Contents.ICreateOptions = {} ): Promise { + console.log('NEW UNTITLED: ', options); // get current list of contents of drive const old_data = await listS3Contents( this._s3Client, @@ -331,12 +334,13 @@ export class Drive implements Contents.IDrive { } Contents.validateContentsModel(data); + console.log('NEW UNTITLED validate data: ', data); this._fileChanged.emit({ type: 'new', oldValue: null, newValue: data }); - + console.log('NEW UNTITLED file chnanged signal emitted finish!'); return data; } @@ -399,13 +403,16 @@ export class Drive implements Contents.IDrive { * @returns A promise which resolves when the file is deleted. */ async delete(localPath: string): Promise { + console.log('DELETE: ', localPath); await deleteS3Objects(this._s3Client, this._name, this._root, localPath); + console.log('DELETE finish!'); this._fileChanged.emit({ type: 'delete', oldValue: { path: localPath }, newValue: { path: undefined } }); + console.log('DELETE file changed signal finished!'); } /** @@ -426,6 +433,7 @@ export class Drive implements Contents.IDrive { newLocalPath: string, options: Contents.ICreateOptions = {} ): Promise { + console.log('RENAME: ', oldLocalPath, newLocalPath); let newFileName = PathExt.basename(newLocalPath); await checkS3Object(this._s3Client, this._name, this._root, newLocalPath) @@ -451,12 +459,14 @@ export class Drive implements Contents.IDrive { ); }); + Contents.validateContentsModel(data); + console.log('RENAME validate contents finish: ', data); this._fileChanged.emit({ type: 'rename', oldValue: { path: oldLocalPath }, newValue: data }); - Contents.validateContentsModel(data); + console.log('RENAME file changed signal emitted!'); return data; } @@ -521,6 +531,7 @@ export class Drive implements Contents.IDrive { localPath: string, options: Partial = {} ): Promise { + console.log('SAVE: ', localPath, options); const fileName = PathExt.basename(localPath); data = await createS3Object( @@ -534,12 +545,14 @@ export class Drive implements Contents.IDrive { options ); + Contents.validateContentsModel(data); + console.log('SAVE finish validate contents: ', data); this._fileChanged.emit({ type: 'save', oldValue: null, newValue: data }); - Contents.validateContentsModel(data); + console.log('SAVE file changed signal emitted'); return data; } @@ -592,6 +605,7 @@ export class Drive implements Contents.IDrive { toDir: string, options: Contents.ICreateOptions = {} ): Promise { + console.log('COPY: ', path, toDir, options); // construct new file or directory name for the copy const newFileName = await this.incrementCopyName(path, this._name); @@ -605,12 +619,14 @@ export class Drive implements Contents.IDrive { this._registeredFileTypes ); + Contents.validateContentsModel(data); + console.log('COPY finished validate contents: ', data); this._fileChanged.emit({ type: 'new', oldValue: null, newValue: data }); - Contents.validateContentsModel(data); + console.log('COPY file changed signal emitted finished'); return data; } From 5a8b6d3df2d6764251cb423f94899555b223ce21 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Wed, 16 Oct 2024 12:36:06 +0200 Subject: [PATCH 02/12] fix folder listing for copy --- src/s3.ts | 12 +++++++++++- src/s3contents.ts | 7 +++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index fba1f39..5d6ffa2 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -85,12 +85,19 @@ export const listS3Contents = async ( path?: string ): Promise => { const fileList: IContentsList = {}; + const prefix = path ? PathExt.join(root, path) : root; // listing contents of folder const command = new ListObjectsV2Command({ Bucket: bucketName, - Prefix: path ? PathExt.join(root, path) : root + Prefix: prefix + (prefix ? '/' : '') }); + console.log( + 'S3CONTENTS LIST FOLDER, path: ', + path ? PathExt.join(root, path) : root, + ' with root: ', + root + ); let isTruncated: boolean | undefined = true; @@ -99,6 +106,7 @@ export const listS3Contents = async ( await s3Client.send(command); if (Contents) { + console.log('S3CONTENTS LIST CONTENTS: ', Contents); Contents.forEach(c => { // check if we are dealing with the files inside a subfolder if ( @@ -452,6 +460,8 @@ export const renameS3Objects = async ( command.input.ContinuationToken = NextContinuationToken; } + console.log('S3CONTENTS rename objects finish'); + return data; }; diff --git a/src/s3contents.ts b/src/s3contents.ts index 4bb946a..d7af568 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -255,7 +255,7 @@ export class Drive implements Contents.IDrive { path: string, options?: Contents.IFetchOptions ): Promise { - console.log('GET: ', path); + // console.log('GET: ', path); path = path.replace(this._name + '/', ''); // getting the list of files from the root @@ -268,6 +268,7 @@ export class Drive implements Contents.IDrive { ); } else { const currentPath = PathExt.basename(path); + console.log('GET LIST FOLDER: ', currentPath); // listing contents of a folder if (PathExt.extname(currentPath) === '') { @@ -281,6 +282,7 @@ export class Drive implements Contents.IDrive { } // getting the contents of a specific file else { + console.log('GET file contents: ', path); data = await getS3FileContents( this._s3Client, this._name, @@ -288,11 +290,12 @@ export class Drive implements Contents.IDrive { path, this.registeredFileTypes ); + console.log('GET validate data finish: ', data); } } Contents.validateContentsModel(data); - console.log('GET validate data finish: ', data); + // console.log('GET validate data finish: ', data); return data; } From 727dfaf8d91687cffa0956d255668037a5889d96 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Wed, 16 Oct 2024 13:23:12 +0200 Subject: [PATCH 03/12] add logic for copying folder with suffix to name --- src/s3.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index 5d6ffa2..f2a5d54 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -492,12 +492,12 @@ export const copyS3Objects = async ( newBucketName?: string ): Promise => { const isDir: boolean = PathExt.extname(path) === ''; + let suffix: string = ''; path = PathExt.join(root, path); toDir = PathExt.join(root, toDir); - name = PathExt.join(toDir, name); - path = isDir ? path + '/' : path; + path = path + (isDir ? '/' : ''); // list contents of path - contents of directory or one file const command = new ListObjectsV2Command({ @@ -513,6 +513,9 @@ export const copyS3Objects = async ( if (Contents) { const promises = Contents.map(c => { + if (!suffix && c.Key!.search('/.emptyFolderPlaceholder') !== -1) { + suffix = '.emptyFolderPlaceholder'; + } const remainingFilePath = c.Key!.substring(path.length); // copy each file from old directory to new location return Private.copyFile( @@ -541,7 +544,7 @@ export const copyS3Objects = async ( const newFileContents = await s3Client.send( new GetObjectCommand({ Bucket: newBucketName ?? bucketName, - Key: name + Key: name + (suffix ? suffix : '') }) ); From 55aa118df1db3abdc12584abe7853327a50960f6 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Thu, 17 Oct 2024 13:14:05 +0200 Subject: [PATCH 04/12] retrieve last modified property after saving S3 object --- src/s3.ts | 39 +++++++++++++++++++++++---------------- src/s3contents.ts | 3 --- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index f2a5d54..97f2e13 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -92,12 +92,6 @@ export const listS3Contents = async ( Bucket: bucketName, Prefix: prefix + (prefix ? '/' : '') }); - console.log( - 'S3CONTENTS LIST FOLDER, path: ', - path ? PathExt.join(root, path) : root, - ' with root: ', - root - ); let isTruncated: boolean | undefined = true; @@ -106,7 +100,6 @@ export const listS3Contents = async ( await s3Client.send(command); if (Contents) { - console.log('S3CONTENTS LIST CONTENTS: ', Contents); Contents.forEach(c => { // check if we are dealing with the files inside a subfolder if ( @@ -265,22 +258,36 @@ export const createS3Object = async ( if (options) { body = Private.formatBody(options, fileFormat, fileType, fileMimeType); } + let lastModified; await s3Client.send( - new PutObjectCommand({ - Bucket: bucketName, - Key: path + (PathExt.extname(name) === '' ? '/' : ''), - Body: body, - CacheControl: options ? 'no-cache' : undefined + new PutObjectCommand({ + Bucket: bucketName, + Key: path + (PathExt.extname(name) === '' ? '/' : ''), + Body: body, + CacheControl: options ? 'no-cache' : undefined + }) + ) + .then(async () => { + console.log('S3CONTENTS save finished'); + const newFileInfo = await s3Client.send( + new HeadObjectCommand({ + Bucket: bucketName, + Key: path + (PathExt.extname(name) === '' ? '/' : '') + }) + ); + console.log('S3CONTENTS save head: ', newFileInfo); + lastModified = newFileInfo.LastModified?.toISOString(); }) - ); - console.log('S3CONTENTS save finished'); + .catch(error => { + console.error('Failed saving or creating the S3 object: ', error); + }); data = { name: name, path: PathExt.join(path, name), - last_modified: new Date().toISOString(), - created: new Date().toISOString(), + last_modified: lastModified ?? new Date().toISOString(), + created: lastModified ?? new Date().toISOString(), content: path.split('.').length === 1 ? [] : body, format: fileFormat as Contents.FileFormat, mimetype: fileMimeType, diff --git a/src/s3contents.ts b/src/s3contents.ts index d7af568..7404e49 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -255,7 +255,6 @@ export class Drive implements Contents.IDrive { path: string, options?: Contents.IFetchOptions ): Promise { - // console.log('GET: ', path); path = path.replace(this._name + '/', ''); // getting the list of files from the root @@ -268,7 +267,6 @@ export class Drive implements Contents.IDrive { ); } else { const currentPath = PathExt.basename(path); - console.log('GET LIST FOLDER: ', currentPath); // listing contents of a folder if (PathExt.extname(currentPath) === '') { @@ -282,7 +280,6 @@ export class Drive implements Contents.IDrive { } // getting the contents of a specific file else { - console.log('GET file contents: ', path); data = await getS3FileContents( this._s3Client, this._name, From bc3a86baf5447c037462d73cddc8b8290d9080e6 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Thu, 17 Oct 2024 13:16:48 +0200 Subject: [PATCH 05/12] update rename error handling --- src/s3.ts | 3 ++- src/s3contents.ts | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index 97f2e13..b83adf5 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -260,7 +260,8 @@ export const createS3Object = async ( } let lastModified; - await s3Client.send( + await s3Client + .send( new PutObjectCommand({ Bucket: bucketName, Key: path + (PathExt.extname(name) === '' ? '/' : ''), diff --git a/src/s3contents.ts b/src/s3contents.ts index 7404e49..8977159 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -444,7 +444,9 @@ export class Drive implements Contents.IDrive { }) .catch(() => { // function throws error as the file name doesn't exist - console.log("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 From 6e4b32d2797cefb3ddbbe800593ae6cac9bb6020 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Thu, 17 Oct 2024 14:01:38 +0200 Subject: [PATCH 06/12] retrieve object metadata after renaming --- src/s3.ts | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index b83adf5..8f03dfc 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -421,28 +421,14 @@ export const renameS3Objects = async ( await s3Client.send(command); if (Contents) { - // retrieve information of file or directory - const fileContents = await s3Client.send( + // retrieve content of file or directory + const oldFileContents = await s3Client.send( new GetObjectCommand({ Bucket: bucketName, Key: Contents[0].Key! }) ); - - const body = await fileContents.Body?.transformToString(); - - data = { - name: newFileName, - path: newLocalPath, - last_modified: fileContents.LastModified!.toISOString(), - created: '', - content: body ? body : [], - format: fileFormat as Contents.FileFormat, - mimetype: fileMimeType, - size: fileContents.ContentLength!, - writable: true, - type: fileType - }; + const body = await oldFileContents.Body?.transformToString(); const promises = Contents.map(async c => { const remainingFilePath = c.Key!.substring(oldLocalPath.length); @@ -461,6 +447,29 @@ export const renameS3Objects = async ( ); }); await Promise.all(promises); + + // retrieve last modified time for new file, does not apply to remaming directory + const newFileMetadata = await s3Client.send( + new HeadObjectCommand({ + Bucket: bucketName, + Key: newLocalPath + }) + ); + + data = { + name: newFileName, + path: newLocalPath, + last_modified: + newFileMetadata.LastModified!.toISOString() ?? + new Date().toISOString(), + created: '', + content: body ? body : [], + format: fileFormat as Contents.FileFormat, + mimetype: fileMimeType, + size: oldFileContents.ContentLength!, + writable: true, + type: fileType + }; } if (isTruncated) { isTruncated = IsTruncated; From bf532b6229bf776db82a44df1981c8262b837772 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Fri, 18 Oct 2024 15:03:48 +0200 Subject: [PATCH 07/12] fix renaming data path --- src/s3.ts | 5 +---- src/s3contents.ts | 3 +-- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index 8f03dfc..e51fef5 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -458,7 +458,7 @@ export const renameS3Objects = async ( data = { name: newFileName, - path: newLocalPath, + path: newLocalPath.replace(root, ''), last_modified: newFileMetadata.LastModified!.toISOString() ?? new Date().toISOString(), @@ -476,9 +476,6 @@ export const renameS3Objects = async ( } command.input.ContinuationToken = NextContinuationToken; } - - console.log('S3CONTENTS rename objects finish'); - return data; }; diff --git a/src/s3contents.ts b/src/s3contents.ts index 8977159..ebe0e4c 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -287,7 +287,6 @@ export class Drive implements Contents.IDrive { path, this.registeredFileTypes ); - console.log('GET validate data finish: ', data); } } @@ -791,7 +790,7 @@ export class Drive implements Contents.IDrive { root = PathExt.removeSlash(PathExt.normalize(root)); // check if directory exists within bucket try { - checkS3Object(this._s3Client, this._name, root); + await checkS3Object(this._s3Client, this._name, root); // the directory exists, root is formatted correctly return root; } catch (error) { From c32fa853c8d19182ef2ba9433cea3c64ebea9249 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Fri, 18 Oct 2024 15:12:31 +0200 Subject: [PATCH 08/12] remove dev comments --- src/s3.ts | 5 ----- src/s3contents.ts | 17 ----------------- 2 files changed, 22 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index e51fef5..7079cb3 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -215,7 +215,6 @@ export const getS3FileContents = async ( writable: true, type: fileType }; - console.log('S3CONTENTS get file contents finished: ', fileContents); } return data; @@ -270,14 +269,12 @@ export const createS3Object = async ( }) ) .then(async () => { - console.log('S3CONTENTS save finished'); const newFileInfo = await s3Client.send( new HeadObjectCommand({ Bucket: bucketName, Key: path + (PathExt.extname(name) === '' ? '/' : '') }) ); - console.log('S3CONTENTS save head: ', newFileInfo); lastModified = newFileInfo.LastModified?.toISOString(); }) .catch(error => { @@ -341,7 +338,6 @@ export const deleteS3Objects = async ( } command.input.ContinuationToken = NextContinuationToken; } - console.log('S3CONTENTS DELETE multiple objects finished'); }; /** @@ -677,7 +673,6 @@ namespace Private { Key: filePath }) ); - console.log('S3CONTENTS delete file finished'); } /** diff --git a/src/s3contents.ts b/src/s3contents.ts index ebe0e4c..15da84f 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -291,7 +291,6 @@ export class Drive implements Contents.IDrive { } Contents.validateContentsModel(data); - // console.log('GET validate data finish: ', data); return data; } @@ -306,7 +305,6 @@ export class Drive implements Contents.IDrive { async newUntitled( options: Contents.ICreateOptions = {} ): Promise { - console.log('NEW UNTITLED: ', options); // get current list of contents of drive const old_data = await listS3Contents( this._s3Client, @@ -333,13 +331,11 @@ export class Drive implements Contents.IDrive { } Contents.validateContentsModel(data); - console.log('NEW UNTITLED validate data: ', data); this._fileChanged.emit({ type: 'new', oldValue: null, newValue: data }); - console.log('NEW UNTITLED file chnanged signal emitted finish!'); return data; } @@ -402,16 +398,13 @@ export class Drive implements Contents.IDrive { * @returns A promise which resolves when the file is deleted. */ async delete(localPath: string): Promise { - console.log('DELETE: ', localPath); await deleteS3Objects(this._s3Client, this._name, this._root, localPath); - console.log('DELETE finish!'); this._fileChanged.emit({ type: 'delete', oldValue: { path: localPath }, newValue: { path: undefined } }); - console.log('DELETE file changed signal finished!'); } /** @@ -432,12 +425,10 @@ export class Drive implements Contents.IDrive { newLocalPath: string, options: Contents.ICreateOptions = {} ): Promise { - console.log('RENAME: ', oldLocalPath, newLocalPath); let newFileName = PathExt.basename(newLocalPath); await checkS3Object(this._s3Client, this._name, this._root, newLocalPath) .then(async () => { - console.log('File name already exists!'); // construct new incremented name newFileName = await this.incrementName(newLocalPath, this._name); }) @@ -461,13 +452,11 @@ export class Drive implements Contents.IDrive { }); Contents.validateContentsModel(data); - console.log('RENAME validate contents finish: ', data); this._fileChanged.emit({ type: 'rename', oldValue: { path: oldLocalPath }, newValue: data }); - console.log('RENAME file changed signal emitted!'); return data; } @@ -532,7 +521,6 @@ export class Drive implements Contents.IDrive { localPath: string, options: Partial = {} ): Promise { - console.log('SAVE: ', localPath, options); const fileName = PathExt.basename(localPath); data = await createS3Object( @@ -547,13 +535,11 @@ export class Drive implements Contents.IDrive { ); Contents.validateContentsModel(data); - console.log('SAVE finish validate contents: ', data); this._fileChanged.emit({ type: 'save', oldValue: null, newValue: data }); - console.log('SAVE file changed signal emitted'); return data; } @@ -606,7 +592,6 @@ export class Drive implements Contents.IDrive { toDir: string, options: Contents.ICreateOptions = {} ): Promise { - console.log('COPY: ', path, toDir, options); // construct new file or directory name for the copy const newFileName = await this.incrementCopyName(path, this._name); @@ -621,13 +606,11 @@ export class Drive implements Contents.IDrive { ); Contents.validateContentsModel(data); - console.log('COPY finished validate contents: ', data); this._fileChanged.emit({ type: 'new', oldValue: null, newValue: data }); - console.log('COPY file changed signal emitted finished'); return data; } From 6f6b1ce8c655166cfeaf8a60244110569550e8bf Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Fri, 18 Oct 2024 15:17:08 +0200 Subject: [PATCH 09/12] add log info for duplicate name in renaming operation --- src/s3contents.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/s3contents.ts b/src/s3contents.ts index 15da84f..0d8822b 100644 --- a/src/s3contents.ts +++ b/src/s3contents.ts @@ -429,6 +429,7 @@ export class Drive implements Contents.IDrive { 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); }) From c061ca5abb34af3f937678bab11a10ef7f586a2b Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Mon, 21 Oct 2024 20:06:07 +0200 Subject: [PATCH 10/12] return delete promise --- src/s3.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/s3.ts b/src/s3.ts index 7079cb3..1e7bb94 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -329,7 +329,7 @@ export const deleteS3Objects = async ( await Promise.all( Contents.map(async c => { // delete each file with given path - await Private.deleteFile(s3Client, bucketName, c.Key!); + return Private.deleteFile(s3Client, bucketName, c.Key!); }) ); } From e5f4a5479718cbcb361ab0dfcd1e3fde23f5eff5 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Tue, 22 Oct 2024 13:03:23 +0200 Subject: [PATCH 11/12] 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({ From 9aab2b567cfa93008a745f780df5de0c2c55a985 Mon Sep 17 00:00:00 2001 From: DenisaCG Date: Tue, 22 Oct 2024 13:16:23 +0200 Subject: [PATCH 12/12] iterate on last modified date logic for renaming operation --- src/s3.ts | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/src/s3.ts b/src/s3.ts index 1e7bb94..1de4e01 100644 --- a/src/s3.ts +++ b/src/s3.ts @@ -444,20 +444,22 @@ export const renameS3Objects = async ( }); await Promise.all(promises); - // retrieve last modified time for new file, does not apply to remaming directory - const newFileMetadata = await s3Client.send( - new HeadObjectCommand({ - Bucket: bucketName, - Key: newLocalPath - }) - ); + let lastModifiedDate = new Date().toISOString(); + if (!isDir) { + // retrieve last modified time for new file, does not apply to remaming directory + const newFileMetadata = await s3Client.send( + new HeadObjectCommand({ + Bucket: bucketName, + Key: newLocalPath + }) + ); + lastModifiedDate = newFileMetadata.LastModified!.toISOString(); + } data = { name: newFileName, path: newLocalPath.replace(root, ''), - last_modified: - newFileMetadata.LastModified!.toISOString() ?? - new Date().toISOString(), + last_modified: lastModifiedDate, created: '', content: body ? body : [], format: fileFormat as Contents.FileFormat, @@ -698,6 +700,7 @@ namespace Private { Key: PathExt.join(newPath, remainingFilePath) }) ); + console.log('copy: ', PathExt.join(newPath, remainingFilePath)); } /**