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

Commit

Permalink
feat: uses axios to download files instead of node.js 'https'. This m…
Browse files Browse the repository at this point in the history
…akes it compatible with browsers as well as node.js.
  • Loading branch information
Enngage committed May 19, 2020
1 parent 2ac3669 commit dbb9e57
Showing 1 changed file with 12 additions and 19 deletions.
31 changes: 12 additions & 19 deletions src/zip/zip.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetContracts } from '@kentico/kontent-management';
import * as fs from 'fs';
import { get } from 'https';
import JSZip = require('jszip');
import axios, {} from 'axios';

import { IExportAllResult } from '../export';
import { IBinaryFile, IImportSource } from '../import';
Expand Down Expand Up @@ -44,7 +44,6 @@ export class ZipService {
console.log(`Parsing zip contents`);
}
const assets = await this.readAndParseJsonFile(unzippedFile, this.assetsName);

const result = {
importData: {
assets,
Expand Down Expand Up @@ -156,24 +155,18 @@ export class ZipService {
}

private getBinaryDataFromUrl(url: string, enableLog: boolean): Promise<any> {
return new Promise((resolve, reject) => {
get(url, res => {
if (enableLog) {
console.log(`Downloading asset: ${url}`);
}
return axios.get(url, {
responseType: 'arraybuffer',
}).then(
response => {
if (enableLog) {
console.log(`Downloading asset: ${url}`);
console.log(`Downloading asset completed: ${url}`);
}
const data: any[] = [];

res.on('data', chunk => {
data.push(chunk);
})
.on('end', () => {
const buffer = Buffer.concat(data);
resolve(buffer);
})
.on('error', error => {
reject(error);
});
});
});
return response.data;
}
);
}
}

0 comments on commit dbb9e57

Please sign in to comment.