This repository has been archived by the owner on Feb 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds ZipService for creating packages and stores assets as file…
…s inside
- Loading branch information
Showing
6 changed files
with
91 additions
and
8 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './zip.service'; | ||
export * from './zip.models'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface IZipServiceConfig { | ||
filename: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); | ||
} | ||
} |