Skip to content

Commit

Permalink
Add method to unzip all files.
Browse files Browse the repository at this point in the history
  • Loading branch information
yannbolliger committed Feb 21, 2024
1 parent 858b4b1 commit f61c357
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/NotionExporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,25 @@ export class NotionExporter {
*
* @returns The ZIP as an 'AdmZip' object
*/
getZip = async (url: string): Promise<AdmZip> => {
private downloadZip = async (url: string): Promise<AdmZip> => {
const res = await this.client.get(url, { responseType: "arraybuffer" })
return new AdmZip(res.data)
}

getZip = (idOrUrl: string): Promise<AdmZip> =>
this.getZipUrl(idOrUrl).then(this.downloadZip)

/**
* Downloads and extracts all files in the exported zip to the given folder.
*
* @param idOrUrl BlockId or URL of the page/block/DB to export
* @param path Folder path where the files are unzipped
*/
getMdFiles = async (idOrUrl: string, path: string): Promise<void> => {
const zip = await this.getZip(idOrUrl)
zip.extractAllTo(path)
}

/**
* Exports the given block as ZIP and downloads it. Returns the matched file
* in the ZIP as a string.
Expand All @@ -113,7 +127,7 @@ export class NotionExporter {
idOrUrl: string,
predicate: (entry: AdmZip.IZipEntry) => boolean
): Promise<string> {
const zip = await this.getZipUrl(idOrUrl).then(this.getZip)
const zip = await this.getZip(idOrUrl)
const entry = zip.getEntries().find(predicate)
return (
entry?.getData().toString().trim() ||
Expand Down

0 comments on commit f61c357

Please sign in to comment.