feat(core): Add S3 endpoint normalization, directory pagination, improved metadata checks, trash support, and related tests
This commit is contained in:
@@ -120,19 +120,44 @@ export class Directory {
|
||||
return directories.some(dir => dir.name === dirNameArg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collects all ListObjectsV2 pages for a prefix.
|
||||
*/
|
||||
private async listObjectsV2AllPages(prefix: string, delimiter?: string) {
|
||||
const allContents: plugins.s3._Object[] = [];
|
||||
const allCommonPrefixes: plugins.s3.CommonPrefix[] = [];
|
||||
let continuationToken: string | undefined;
|
||||
|
||||
do {
|
||||
const command = new plugins.s3.ListObjectsV2Command({
|
||||
Bucket: this.bucketRef.name,
|
||||
Prefix: prefix,
|
||||
Delimiter: delimiter,
|
||||
ContinuationToken: continuationToken,
|
||||
});
|
||||
const response = await this.bucketRef.smartbucketRef.s3Client.send(command);
|
||||
|
||||
if (response.Contents) {
|
||||
allContents.push(...response.Contents);
|
||||
}
|
||||
if (response.CommonPrefixes) {
|
||||
allCommonPrefixes.push(...response.CommonPrefixes);
|
||||
}
|
||||
|
||||
continuationToken = response.IsTruncated ? response.NextContinuationToken : undefined;
|
||||
} while (continuationToken);
|
||||
|
||||
return { contents: allContents, commonPrefixes: allCommonPrefixes };
|
||||
}
|
||||
|
||||
/**
|
||||
* lists all files
|
||||
*/
|
||||
public async listFiles(): Promise<File[]> {
|
||||
const command = new plugins.s3.ListObjectsV2Command({
|
||||
Bucket: this.bucketRef.name,
|
||||
Prefix: this.getBasePath(),
|
||||
Delimiter: '/',
|
||||
});
|
||||
const response = await this.bucketRef.smartbucketRef.s3Client.send(command);
|
||||
const { contents } = await this.listObjectsV2AllPages(this.getBasePath(), '/');
|
||||
const fileArray: File[] = [];
|
||||
|
||||
response.Contents?.forEach((item) => {
|
||||
contents.forEach((item) => {
|
||||
if (item.Key && !item.Key.endsWith('/')) {
|
||||
const subtractedPath = item.Key.replace(this.getBasePath(), '');
|
||||
if (!subtractedPath.includes('/')) {
|
||||
@@ -154,16 +179,11 @@ export class Directory {
|
||||
*/
|
||||
public async listDirectories(): Promise<Directory[]> {
|
||||
try {
|
||||
const command = new plugins.s3.ListObjectsV2Command({
|
||||
Bucket: this.bucketRef.name,
|
||||
Prefix: this.getBasePath(),
|
||||
Delimiter: '/',
|
||||
});
|
||||
const response = await this.bucketRef.smartbucketRef.s3Client.send(command);
|
||||
const { commonPrefixes } = await this.listObjectsV2AllPages(this.getBasePath(), '/');
|
||||
const directoryArray: Directory[] = [];
|
||||
|
||||
if (response.CommonPrefixes) {
|
||||
response.CommonPrefixes.forEach((item) => {
|
||||
if (commonPrefixes) {
|
||||
commonPrefixes.forEach((item) => {
|
||||
if (item.Prefix) {
|
||||
const subtractedPath = item.Prefix.replace(this.getBasePath(), '');
|
||||
if (subtractedPath.endsWith('/')) {
|
||||
@@ -235,7 +255,7 @@ export class Directory {
|
||||
return returnDirectory;
|
||||
}
|
||||
if (optionsArg.getEmptyDirectory || optionsArg.createWithInitializerFile) {
|
||||
returnDirectory = new Directory(this.bucketRef, this, dirNameToSearch);
|
||||
returnDirectory = new Directory(this.bucketRef, directoryArg, dirNameToSearch);
|
||||
}
|
||||
if (isFinalDirectory && optionsArg.createWithInitializerFile) {
|
||||
returnDirectory?.createEmptyFile('00init.txt');
|
||||
|
||||
Reference in New Issue
Block a user