Skip to content

Commit

Permalink
Better typing for storage-related modules
Browse files Browse the repository at this point in the history
  • Loading branch information
gingi committed Oct 31, 2023
1 parent 7299f41 commit c6efb9c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
import {
ControlValueAccessor, FormControl, NG_VALIDATORS, NG_VALUE_ACCESSOR,
} from "@angular/forms";
import { MatOptionSelectionChange } from "@angular/material/core";
import { FilterBuilder, ListView } from "@batch-flask/core";
import { Activity, DialogService } from "@batch-flask/ui";
import { FileGroupCreateFormComponent } from "app/components/data/action";
Expand Down
4 changes: 2 additions & 2 deletions desktop/src/app/models/blob-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface BlobContainerAttributes {
name: string;
publicAccessLevel: string;
metadata?: any;
lastModified: Date;
lastModified?: Date;
lease?: Partial<ContainerLeaseAttributes>;
}

Expand All @@ -25,7 +25,7 @@ export class BlobContainer extends Record<BlobContainerAttributes> implements Na

@Prop() public publicAccessLevel: string;
@Prop() public metadata: any;
@Prop() public lastModified: Date;
@Prop() public lastModified?: Date;
@Prop() public lease: ContainerLease;
@Prop() public storageAccountId: string;

Expand Down
7 changes: 5 additions & 2 deletions desktop/src/app/services/core/data/storage-list-getter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Type } from "@angular/core";
import { ContinuationToken, ListGetter, ListGetterConfig, Record, ServerError } from "@batch-flask/core";
import { BlobStorageClientProxy } from "app/services/storage";
import { StorageBlobResult } from "app/services/storage/models";
import { StorageClientService } from "app/services/storage/storage-client.service";
import { Observable, from, throwError } from "rxjs";
Expand All @@ -12,13 +13,15 @@ export interface StorageBaseParams {
export interface StorageListConfig<TEntity extends Record<any>, TParams extends StorageBaseParams>
extends ListGetterConfig<TEntity, TParams> {

getData: (client: any, params: TParams, options: any, token: any) => Promise<StorageBlobResult<TEntity[]>>;
getData: (client: BlobStorageClientProxy, params: TParams, options: any, token: any) =>
Promise<StorageBlobResult<TEntity[]>>;
}

export class StorageListGetter<TEntity extends Record<any>, TParams extends StorageBaseParams>
extends ListGetter<TEntity, TParams> {

private _getData: (client: any, params: TParams, options: any, token: any) => Promise<StorageBlobResult<TEntity[]>>;
private _getData: (client: BlobStorageClientProxy, params: TParams, options: any, token: any) =>
Promise<StorageBlobResult<TEntity[]>>;

constructor(
type: Type<TEntity>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { IpcEvent } from "common/constants";
import { SharedAccessPolicy } from "./models";
import * as blob from "./models/storage-blob";
import { BlobContentResult } from "./storage-blob.service";
import { StorageClient } from "./storage-client.service";

const storageIpc = IpcEvent.storageBlob;

Expand All @@ -18,7 +19,7 @@ export interface ListBlobResponse {
};
}

export class BlobStorageClientProxy {
export class BlobStorageClientProxy implements StorageClient {

private storageInfo: { url: string; account: string, key: string };

Expand Down
56 changes: 29 additions & 27 deletions desktop/src/app/services/storage/models/storage-blob.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
import { ContainerItem, BlobUploadCommonResponse, ContainerGetPropertiesResponse, CommonOptions } from "@azure/storage-blob";
import { BlobUploadCommonResponse, ContainerGetPropertiesResponse, CommonOptions, ContainerItem } from "@azure/storage-blob";
import { Model, Prop, Record } from "@batch-flask/core";
import { SharedAccessPolicy } from "./shared-access-policy";
import { BlobContainer } from "app/models";

// Placeholder; we don't use any options to storage-blob API requests
export type RequestOptions = Partial<CommonOptions>;

export interface BlobProperties {
name: string;
url: string;
isDirectory: boolean;
properties: {
contentLength: number;
contentType: string;
creationTime: Date | string;
lastModified?: Date | string;
};
}

export type ContainerProperties = ContainerGetPropertiesResponse;

@Model()
export class BlobItem extends Record<BlobProperties> {
@Prop() name: string;
@Prop() url: string;
@Prop() isDirectory: boolean;
@Prop() properties: {
contentLength: number;
contentType: string;
creationTime: Date | string;
lastModified?: Date | string;
}
}

export interface BaseParams {
url: string;
account: string;
Expand Down Expand Up @@ -94,32 +122,6 @@ export interface GetBlobContentResult {
content: string;
}

export interface BlobProperties {
name: string;
url: string;
isDirectory: boolean;
properties: {
contentLength: number;
contentType: string;
creationTime: Date | string;
lastModified: Date | string;
};
}

@Model()
export class BlobItem extends Record<BlobProperties> {
@Prop() name: string;
@Prop() url: string;
@Prop() isDirectory: boolean;
@Prop() properties: {
contentLength: number;
contentType: string;
creationTime: Date | string;
lastModified: Date | string;
}
}

export type ContainerProperties = ContainerGetPropertiesResponse;

export interface ListBlobOptions {
/**
Expand Down
18 changes: 8 additions & 10 deletions desktop/src/app/services/storage/storage-blob.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { Constants } from "common";
import { AsyncSubject, Observable, from, of, throwError } from "rxjs";
import { catchError, concat, concatMap, map, share, switchMap, take } from "rxjs/operators";
import { BlobStorageClientProxy } from "./blob-storage-client-proxy";
import { StorageClientService } from "./storage-client.service";
import { StorageClient, StorageClientService } from "./storage-client.service";

export interface ListBlobParams {
storageAccountId: string;
Expand All @@ -42,7 +42,10 @@ export interface BlobContentResult {
content: string;
}

export type StorageContainerProperties = ContainerProperties;
export interface StorageContainerProperties extends Omit<ContainerProperties, "lastModified" | "etag"> {
lastModified?: Date;
etag?: string;
}

export interface NavigateBlobsOptions {
/**
Expand Down Expand Up @@ -117,21 +120,16 @@ export class StorageBlobService {

this._blobListGetter = new StorageListGetter(FileRecord, this.storageClient, {
cache: (params) => this.getBlobFileCache(params),
getData: (client: BlobStorageClientProxy,
params, options, continuationToken) => {
getData: async (client: StorageClient, params, options, continuationToken) => {
const blobOptions: ListBlobOptions = {
folder: options.original.folder,
recursive: options.original.recursive,
maxPages: options.original.limit,
maxPageSize: this.maxBlobPageSize
};

// N.B. `BlobItem` and `FileRecord` are nearly identical
return client.listBlobs(
params.container,
blobOptions,
continuationToken,
) as Promise<StorageBlobResult<FileRecord[]>>;
const blobs = await client.listBlobs(params.container, blobOptions, continuationToken);
return { data: blobs.data.map(blob => new FileRecord(blob)) };
},
logIgnoreError: storageIgnoredErrors,
});
Expand Down
10 changes: 9 additions & 1 deletion desktop/src/app/services/storage/storage-client.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from "@angular/core";
import { ServerError } from "@batch-flask/core";
import { ElectronRemote } from "@batch-flask/electron";
import { StorageKeys } from "app/models";
import { BlobContainer, StorageKeys } from "app/models";
import { BatchExplorerService } from "app/services/batch-explorer.service";
import { ArmResourceUtils } from "app/utils";
import { Observable, throwError } from "rxjs";
Expand All @@ -10,6 +10,7 @@ import { BatchAccountService } from "../batch-account";
import { BlobStorageClientProxy } from "./blob-storage-client-proxy";
import { StorageAccountKeysService } from "./storage-account-keys.service";
import { StorageClientProxyFactory } from "./storage-client-proxy-factory";
import { ListBlobsResult, ListContainersResult, StorageBlobResult } from "./models";

export interface AutoStorageSettings {
lastKeySync: Date;
Expand All @@ -23,6 +24,13 @@ export interface StorageKeyCachedItem {
keys: StorageKeys;
}

export interface StorageClient {
listContainersWithPrefix(prefix: string, continuationToken?: string, options?: any):
Promise<ListContainersResult>;
listBlobs(containerName: string, options: any, continuationToken?: string):
Promise<ListBlobsResult>;
}

@Injectable({ providedIn: "root" })
export class StorageClientService {
public hasAutoStorage: Observable<boolean>;
Expand Down
4 changes: 2 additions & 2 deletions desktop/src/app/services/storage/storage-container.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { SharedAccessPolicy } from "app/services/storage/models";
import { Observable, Subject, from, throwError } from "rxjs";
import { catchError, flatMap, share } from "rxjs/operators";
import { BlobStorageClientProxy } from "./blob-storage-client-proxy";
import { StorageClientService } from "./storage-client.service";
import { StorageClient, StorageClientService } from "./storage-client.service";

export interface GetContainerParams {
storageAccountId: string;
Expand Down Expand Up @@ -62,7 +62,7 @@ export class StorageContainerService {
});
this._containerListGetter = new StorageListGetter(BlobContainer, this.storageClient, {
cache: params => this._containerCache.getCache(params),
getData: async (client, params, options, continuationToken) => {
getData: async (client: StorageClient, params, options, continuationToken) => {
let prefix = null;
if (options && options.filter) {
prefix = options.filter.value;
Expand Down

0 comments on commit c6efb9c

Please sign in to comment.