Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: adds ability to set artificial delay between asset download req…
Browse files Browse the repository at this point in the history
…uests & wait until asset is downloaded before proceeding to next
  • Loading branch information
Enngage committed Oct 21, 2020
1 parent 2df86f2 commit 3855127
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/zip/zip.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export type ZipContext = 'node.js' | 'browser';
export interface IZipServiceConfig {
enableLog: boolean;
context: ZipContext
delayBetweenAssetDownloadRequestsMs?: number
}
18 changes: 14 additions & 4 deletions src/zip/zip.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { IZipServiceConfig } from './zip.models';

export class ZipService {

private readonly delayBetweenAssetRequestsMs: number;

private readonly contentTypesName: string = 'contentTypes.json';
private readonly contentItemsName: string = 'contentItems.json';
private readonly taxonomiesName: string = 'taxonomies.json';
Expand All @@ -21,6 +23,7 @@ export class ZipService {
private readonly validationName: string = 'validation.json';

constructor(private config: IZipServiceConfig) {
this.delayBetweenAssetRequestsMs = config?.delayBetweenAssetDownloadRequestsMs ?? 150;
}

public async extractZipAsync(zipFile: any): Promise<IImportSource> {
Expand Down Expand Up @@ -101,9 +104,12 @@ export class ZipService {
}

const assetFilename = asset.file_name;
assetIdFolder.file(assetFilename, this.getBinaryDataFromUrl(asset.url, this.config.enableLog), {
assetIdFolder.file(assetFilename, await this.getBinaryDataFromUrlAsync(asset.url, this.config.enableLog), {
binary: true
});

// create artificial delay between requests as to prevent errors on network
await this.sleepAsync(this.delayBetweenAssetRequestsMs);
}

if (this.config.enableLog) {
Expand All @@ -119,6 +125,10 @@ export class ZipService {
return content;
}

private sleepAsync(ms: number): Promise<any> {
return new Promise((resolve: any) => setTimeout(resolve, ms));
}

private async extractBinaryFilesAsync(
zip: JSZip,
assets: AssetContracts.IAssetModelContract[]
Expand Down Expand Up @@ -173,19 +183,19 @@ export class ZipService {
return JSON.parse(text);
}

private getBinaryDataFromUrl(url: string, enableLog: boolean): Promise<any> {
private getBinaryDataFromUrlAsync(url: string, enableLog: boolean): Promise<any> {
// temp fix for Kontent Repository not validating url
url = url.replace('#', '%23');

if (enableLog) {
console.log(`Downloading asset: ${url}`);
console.log(`Start asset download: ${url}`);
}
return axios.get(url, {
responseType: 'arraybuffer',
}).then(
response => {
if (enableLog) {
console.log(`Downloading asset completed: ${url}`);
console.log(`Completed asset download: ${url}`);
}
return response.data;
}
Expand Down

0 comments on commit 3855127

Please sign in to comment.