Skip to content

Commit

Permalink
Merge pull request #37 from QuantStack/fixCount
Browse files Browse the repository at this point in the history
Fix counting functionality for directory rename
  • Loading branch information
DenisaCG authored Jan 22, 2025
2 parents 205ab5e + 97ff7ec commit 91e0eee
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
13 changes: 9 additions & 4 deletions src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,15 @@ export const checkS3Object = async (
s3Client: S3Client,
bucketName: string,
root: string,
path?: string
path?: string,
isDir?: boolean
): Promise<void> => {
// 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 ? '/' : '')
})
);
}
Expand Down Expand Up @@ -657,8 +658,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)
Expand Down
13 changes: 11 additions & 2 deletions src/s3contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,13 @@ export class Drive implements Contents.IDrive {
);

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, isDir);
} catch (error) {
// HEAD request failed for this file name, continue, as name doesn't already exist.
Expand Down Expand Up @@ -471,7 +477,10 @@ export class Drive implements Contents.IDrive {

// check if we are dealing with a directory
if (isDir === true) {
localPath = localPath.substring(0, localPath.length - 1);
localPath =
localPath[localPath.length - 1] === '/'
? localPath.substring(0, localPath.length - 1)
: localPath;
originalName = PathExt.basename(localPath);
}
// dealing with a file
Expand Down

0 comments on commit 91e0eee

Please sign in to comment.