Skip to content

Commit 91e0eee

Browse files
authored
Merge pull request #37 from QuantStack/fixCount
Fix counting functionality for directory rename
2 parents 205ab5e + 97ff7ec commit 91e0eee

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

src/s3.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,15 @@ export const checkS3Object = async (
373373
s3Client: S3Client,
374374
bucketName: string,
375375
root: string,
376-
path?: string
376+
path?: string,
377+
isDir?: boolean
377378
): Promise<void> => {
378379
// checking the existance of an S3 object
379380
if (path) {
380381
await s3Client.send(
381382
new HeadObjectCommand({
382383
Bucket: bucketName,
383-
Key: PathExt.join(root, path)
384+
Key: PathExt.join(root, path) + (isDir === true ? '/' : '')
384385
})
385386
);
386387
}
@@ -657,8 +658,12 @@ export const countS3ObjectNameAppearances = async (
657658

658659
if (Contents) {
659660
Contents.forEach(c => {
660-
const fileName = c
661-
.Key!.replace((root ? root + '/' : '') + (path ? path + '/' : ''), '')
661+
let fileName =
662+
c.Key![c.Key!.length - 1] === '/'
663+
? c.Key!.substring(0, c.Key!.length - 1)
664+
: c.Key!;
665+
fileName = fileName
666+
.replace((root ? root + '/' : '') + (path ? path + '/' : ''), '')
662667
.split('/')[0];
663668
if (
664669
fileName.substring(0, originalName.length + 1).includes(originalName)

src/s3contents.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,13 @@ export class Drive implements Contents.IDrive {
430430
);
431431

432432
try {
433-
await checkS3Object(this._s3Client, this._name, this._root, newLocalPath);
433+
await checkS3Object(
434+
this._s3Client,
435+
this._name,
436+
this._root,
437+
newLocalPath,
438+
isDir
439+
);
434440
newFileName = await this.incrementName(newLocalPath, this._name, isDir);
435441
} catch (error) {
436442
// HEAD request failed for this file name, continue, as name doesn't already exist.
@@ -471,7 +477,10 @@ export class Drive implements Contents.IDrive {
471477

472478
// check if we are dealing with a directory
473479
if (isDir === true) {
474-
localPath = localPath.substring(0, localPath.length - 1);
480+
localPath =
481+
localPath[localPath.length - 1] === '/'
482+
? localPath.substring(0, localPath.length - 1)
483+
: localPath;
475484
originalName = PathExt.basename(localPath);
476485
}
477486
// dealing with a file

0 commit comments

Comments
 (0)