Skip to content

Commit

Permalink
fix directory copy functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisaCG committed Jan 21, 2025
1 parent 34a5ce3 commit 939a9e8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/s3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,8 @@ export async function isDirectory(
// listing contents given a path, to check if it is a directory
const command = new ListObjectsV2Command({
Bucket: bucketName,
Prefix: objectPath + '/'
Prefix:
objectPath[objectPath.length - 1] === '/' ? objectPath : objectPath + '/'
});

const { Contents } = await s3Client.send(command);
Expand Down
24 changes: 14 additions & 10 deletions src/s3contents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,15 @@ export class Drive implements Contents.IDrive {
options: Contents.ICreateOptions = {}
): Promise<Contents.IModel> {
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);
newFileName = await this.incrementName(newLocalPath, this._name);
newFileName = await this.incrementName(newLocalPath, this._name, isDir);
} catch (error) {
// HEAD request failed for this file name, continue, as name doesn't already exist.
} finally {
Expand Down Expand Up @@ -457,19 +462,14 @@ export class Drive implements Contents.IDrive {
*
* @param bucketName - The name of the bucket where content is moved.
*
* @param root - The root of the bucket, if it exists.
* @param isDir - Whether the object is a directory or a file.
*/
async incrementName(localPath: string, bucketName: string) {
const isDir: boolean = await isDirectory(
this._s3Client,
bucketName,
localPath
);
async incrementName(localPath: string, bucketName: string, isDir: boolean) {
let fileExtension: string = '';
let originalName: string = '';

// check if we are dealing with a directory
if (isDir) {
if (isDir === true) {
localPath = localPath.substring(0, localPath.length - 1);
originalName = PathExt.basename(localPath);
}
Expand Down Expand Up @@ -571,7 +571,11 @@ export class Drive implements Contents.IDrive {
(isDir ? '/' : '');

// getting incremented name of Copy in case of duplicates
const incrementedName = await this.incrementName(newFilePath, bucketName);
const incrementedName = await this.incrementName(
newFilePath,
bucketName,
isDir
);

return incrementedName;
}
Expand Down

0 comments on commit 939a9e8

Please sign in to comment.