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

Commit

Permalink
feat: adds ZipService for creating packages and stores assets as file…
Browse files Browse the repository at this point in the history
…s inside
  • Loading branch information
Enngage committed Jan 13, 2020
1 parent 5eb91f5 commit df0aff6
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 8 deletions.
11 changes: 8 additions & 3 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
],
"license": "MIT",
"dependencies": {
"@kentico/kontent-management": "0.3.9",
"@kentico/kontent-management": "0.3.10",
"jszip": "3.2.2",
"rxjs": "6.5.4",
"yargs": "15.1.0",
Expand Down
10 changes: 6 additions & 4 deletions src/export/export.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export interface IExportData {
assets: AssetContracts.IAssetModelContract[];
}

export interface IExportMetadata {
projectId: string;
timestamp: Date;
}

export interface IExportAllResult {
metadata: {
timestamp: Date;
projectId: string;
};
metadata: IExportMetadata;
data: IExportData;
}
2 changes: 2 additions & 0 deletions src/zip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './zip.service';
export * from './zip.models';
3 changes: 3 additions & 0 deletions src/zip/zip.models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IZipServiceConfig {
filename: string;
}
71 changes: 71 additions & 0 deletions src/zip/zip.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import * as fs from 'fs';
import { get } from 'https';
import JSZip = require('jszip');

import { IExportData, IExportMetadata } from '../export';
import { IZipServiceConfig } from './zip.models';

export class ZipService {
private readonly zipExtension: string = '.zip';

private readonly contentTypesName: string = 'contentTypes.json';
private readonly contentItemsName: string = 'contentItems.json';
private readonly taxonomiesName: string = 'taxonomies.json';
private readonly assetsName: string = 'assets.json';
private readonly languageVariantsName: string = 'languageVariants.json';
private readonly metadataName: string = 'metadata.json';
private readonly languages: string = 'languages.json';
private readonly assetsFolderName: string = 'files.json';

private readonly filenameWithExtension: string;

constructor(config: IZipServiceConfig) {
this.filenameWithExtension = config.filename + this.zipExtension;
}

public async createZipAsync(exportData: IExportData, metadata: IExportMetadata): Promise<void> {
const zip = new JSZip();

zip.file(this.contentTypesName, JSON.stringify(exportData.contentTypes));
zip.file(this.contentItemsName, JSON.stringify(exportData.contentItems));
zip.file(this.taxonomiesName, JSON.stringify(exportData.taxonomies));
zip.file(this.assetsName, JSON.stringify(exportData.assets));
zip.file(this.languageVariantsName, JSON.stringify(exportData.languageVariants));
zip.file(this.metadataName, JSON.stringify(metadata));
zip.file(this.languages, JSON.stringify(exportData.languages));

const assetsFolder = zip.folder(this.assetsFolderName);

for (const asset of exportData.assets) {
const assetIdShortFolder = assetsFolder.folder(asset.id.substr(0, 3));
const assetIdFolder = assetIdShortFolder.folder(asset.id);
const assetFilename = asset.file_name;
assetIdFolder.file(assetFilename, this.getBinaryDataFromUrl(asset.url), {
binary: true
});
}

const content = await zip.generateAsync({ type: 'nodebuffer' });

await fs.promises.writeFile('./' + this.filenameWithExtension, content);
}

private getBinaryDataFromUrl(url: string): Promise<any> {
return new Promise((resolve, reject) => {
get(url, res => {
const data: any[] = [];

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

0 comments on commit df0aff6

Please sign in to comment.