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

Commit

Permalink
feat: updates deps, uses HttpService to export binary files (enables …
Browse files Browse the repository at this point in the history
…retry strategy for exporting assets). Fixes #18
  • Loading branch information
Enngage committed Jun 15, 2021
1 parent 2814951 commit 9b7a14c
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 32 deletions.
32 changes: 17 additions & 15 deletions lib/zip/zip.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { AssetContracts } from '@kentico/kontent-management';
import { HttpService } from '@kentico/kontent-core';
import * as JSZip from 'jszip';
import axios, {} from 'axios';

import { IExportAllResult } from '../export';
import { IBinaryFile, IImportSource } from '../import';
import { IZipServiceConfig } from './zip.models';

export class ZipService {

private readonly delayBetweenAssetRequestsMs: number;

private readonly contentTypesName: string = 'contentTypes.json';
Expand All @@ -23,6 +22,8 @@ export class ZipService {
private readonly workflowStepsName: string = 'workflowSteps.json';
private readonly validationName: string = 'validation.json';

private readonly httpService: HttpService = new HttpService();

constructor(private config: IZipServiceConfig) {
this.delayBetweenAssetRequestsMs = config?.delayBetweenAssetDownloadRequestsMs ?? 150;
}
Expand All @@ -47,12 +48,12 @@ export class ZipService {
contentItems: await this.readAndParseJsonFile(unzippedFile, this.contentItemsName),
contentTypeSnippets: await this.readAndParseJsonFile(unzippedFile, this.contentTypeSnippetsName),
taxonomies: await this.readAndParseJsonFile(unzippedFile, this.taxonomiesName),
workflowSteps: await this.readAndParseJsonFile(unzippedFile, this.workflowStepsName),
workflowSteps: await this.readAndParseJsonFile(unzippedFile, this.workflowStepsName)
},
assetFolders: await this.readAndParseJsonFile(unzippedFile, this.assetFoldersName),
binaryFiles: await this.extractBinaryFilesAsync(unzippedFile, assets),
validation: await this.readAndParseJsonFile(unzippedFile, this.validationName),
metadata: await this.readAndParseJsonFile(unzippedFile, this.metadataName),
metadata: await this.readAndParseJsonFile(unzippedFile, this.metadataName)
};

if (this.config.enableLog) {
Expand Down Expand Up @@ -130,7 +131,7 @@ export class ZipService {

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

private async extractBinaryFilesAsync(
zip: JSZip,
Expand Down Expand Up @@ -186,22 +187,23 @@ export class ZipService {
return JSON.parse(text);
}

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

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

return (
await this.httpService.getAsync(
{
url
},
{
responseType: 'arraybuffer'
}
return response.data;
}
);
)
).data;
}
}
34 changes: 20 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@
],
"license": "MIT",
"dependencies": {
"@kentico/kontent-management": "1.0.0-next.3",
"@kentico/kontent-management": "1.0.0-next.5",
"jszip": "3.6.0",
"yargs": "17.0.1"
},
"devDependencies": {
"tslib": "2.2.0",
"tslib": "2.3.0",
"@types/jszip": "3.4.1",
"@types/node": "15.12.1",
"@types/node": "15.12.2",
"@types/yargs": "17.0.0",
"standard-version": "9.3.0",
"ts-node": "10.0.0",
Expand Down

0 comments on commit 9b7a14c

Please sign in to comment.