Skip to content

Commit

Permalink
fix(storages): list by prefix (#552)
Browse files Browse the repository at this point in the history
* fix(s3): list by prefix

* fix(gcs): list by prefix

* refactor(storage): pullup metaname-id  methods
  • Loading branch information
kukhariev authored May 19, 2022
1 parent ea71ae0 commit 17f4f49
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
12 changes: 10 additions & 2 deletions packages/core/src/storages/meta-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ export interface MetaStorageOptions {
* Stores upload metadata
*/
export class MetaStorage<T> {
prefix = '.';
suffix = '.';
prefix = '';
suffix = '';

constructor(config?: MetaStorageOptions) {
this.prefix = config?.prefix || '';
Expand Down Expand Up @@ -69,6 +69,14 @@ export class MetaStorage<T> {
async list(prefix = ''): Promise<UploadList> {
return { items: [] };
}

getMetaName(id: string): string {
return this.prefix + id + this.suffix;
}

getIdFromMetaName(name: string): string {
return name.slice(this.prefix.length, -this.suffix.length);
}
}

/**
Expand Down
13 changes: 2 additions & 11 deletions packages/gcs/src/gcs-meta-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ export class GCSMetaStorage<T extends File = File> extends MetaStorage<T> {
this.authClient = new GoogleAuth(config);
}

getMetaName = (id: string): string => this.prefix + id + this.suffix;

/**
* Returns metafile url
* @param id - upload id
Expand All @@ -31,13 +29,6 @@ export class GCSMetaStorage<T extends File = File> extends MetaStorage<T> {
return `${this.storageBaseURI}/${this.getMetaName(id)}`;
}

/**
* Returns upload id from metafile url
* @internal
*/
getIdFromPath = (metaFilePath: string): string =>
metaFilePath.slice(`${this.prefix}`.length, -this.suffix.length);

async save(id: string, file: T): Promise<T> {
//TODO: use JSON API multipart POST?
await this.authClient.request({
Expand Down Expand Up @@ -65,15 +56,15 @@ export class GCSMetaStorage<T extends File = File> extends MetaStorage<T> {
async list(prefix: string): Promise<UploadList> {
const baseURL = this.storageBaseURI;
const url = '/';
const options = { baseURL, url, params: { prefix: encodeURIComponent(prefix) } };
const options = { baseURL, url, params: { prefix: encodeURIComponent(this.prefix + prefix) } };
const { data } = await this.authClient.request<{
items: { name: string; timeCreated: string; metadata?: T }[];
}>(options);
return {
items: data.items
.filter(item => item.name.endsWith(this.suffix))
.map(({ name, timeCreated }) => ({
id: this.getIdFromPath(name),
id: this.getIdFromMetaName(name),
createdAt: new Date(timeCreated)
}))
};
Expand Down
8 changes: 2 additions & 6 deletions packages/s3/src/s3-meta-storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ export class S3MetaStorage<T extends File = File> extends MetaStorage<T> {
this.client = new S3Client(config);
}

getMetaName(id: string): string {
return this.prefix + id + this.suffix;
}

async get(id: string): Promise<T> {
const params = { Bucket: this.bucket, Key: this.getMetaName(id) };
const { Metadata } = await this.client.send(new HeadObjectCommand(params));
Expand Down Expand Up @@ -60,7 +56,7 @@ export class S3MetaStorage<T extends File = File> extends MetaStorage<T> {
async list(prefix: string): Promise<UploadList> {
const params = {
Bucket: this.bucket,
Prefix: prefix
Prefix: this.prefix + prefix
};
const items = [];
const response = await this.client.send(new ListObjectsV2Command(params));
Expand All @@ -70,7 +66,7 @@ export class S3MetaStorage<T extends File = File> extends MetaStorage<T> {
LastModified &&
Key.endsWith(this.suffix) &&
items.push({
id: Key?.slice(this.prefix.length, -this.suffix.length),
id: this.getIdFromMetaName(Key),
createdAt: LastModified
});
}
Expand Down

0 comments on commit 17f4f49

Please sign in to comment.