Skip to content

Commit a6da056

Browse files
committed
feature(cache): write temporary files to disk; use system zip/unzip
1 parent e9c6d91 commit a6da056

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/classes/template.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { XmlTemplateHelper } from '../helper/xml-template-helper';
1111
import { SlideInfo } from '../types/xml-types';
1212
import { XmlHelper } from '../helper/xml-helper';
1313
import { vd } from '../helper/general-helper';
14+
import CacheHelper from '../helper/cache-helper';
1415

1516
export class Template implements ITemplate {
1617
/**
@@ -52,23 +53,26 @@ export class Template implements ITemplate {
5253
creationIds: SlideInfo[];
5354
existingSlides: number;
5455

55-
constructor(location: string) {
56+
constructor(location: string, cache?: CacheHelper) {
5657
this.location = location;
5758
const file = FileHelper.readFile(location);
58-
this.archive = FileHelper.extractFileContent(file as unknown as Buffer);
59+
this.archive = FileHelper.extractFileContent(
60+
file as unknown as Buffer,
61+
cache?.setLocation(location),
62+
);
5963
}
6064

6165
static import(
6266
location: string,
6367
name?: string,
68+
cache?: CacheHelper,
6469
): PresTemplate | RootPresTemplate {
6570
let newTemplate: PresTemplate | RootPresTemplate;
66-
6771
if (name) {
68-
newTemplate = new Template(location) as PresTemplate;
72+
newTemplate = new Template(location, cache) as PresTemplate;
6973
newTemplate.name = name;
7074
} else {
71-
newTemplate = new Template(location) as RootPresTemplate;
75+
newTemplate = new Template(location, cache) as RootPresTemplate;
7276
newTemplate.slides = [];
7377
newTemplate.counter = [
7478
new CountHelper('slides', newTemplate),

src/helper/cache-helper.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import JSZip from 'jszip';
4+
import { vd } from './general-helper';
5+
const extract = require('extract-zip');
6+
7+
export default class CacheHelper {
8+
dir: string;
9+
currentLocation: string;
10+
currentSubDir: string;
11+
12+
constructor(dir: string) {
13+
this.dir = dir;
14+
}
15+
16+
setLocation(location: string) {
17+
this.currentLocation = location;
18+
const baseName = path.basename(this.currentLocation);
19+
this.currentSubDir = this.dir + '/' + baseName;
20+
return this;
21+
}
22+
23+
store() {
24+
extract(this.currentLocation, { dir: this.currentSubDir }).catch((err) => {
25+
throw err;
26+
});
27+
return this;
28+
}
29+
}

src/helper/file-helper.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import JSZip, { InputType, OutputType } from 'jszip';
44

55
import { AutomizerSummary } from '../types/types';
66
import { IPresentationProps } from '../interfaces/ipresentation-props';
7+
import CacheHelper from './cache-helper';
8+
import { vd } from './general-helper';
79

810
export class FileHelper {
911
static readFile(location: string): Promise<Buffer> {
@@ -17,6 +19,7 @@ export class FileHelper {
1719
archive: JSZip,
1820
file: string,
1921
type?: OutputType,
22+
cache?: CacheHelper,
2023
): Promise<string | number[] | Uint8Array | ArrayBuffer | Blob | Buffer> {
2124
if (archive === undefined) {
2225
throw new Error('No files found, expected: ' + file);
@@ -29,9 +32,10 @@ export class FileHelper {
2932
return archive.files[file].async(type || 'string');
3033
}
3134

32-
static extractFileContent(file: Buffer): Promise<JSZip> {
35+
static extractFileContent(file: Buffer, cache?: CacheHelper): Promise<JSZip> {
36+
cache?.store();
3337
const zip = new JSZip();
34-
return zip.loadAsync((file as unknown) as InputType);
38+
return zip.loadAsync(file as unknown as InputType);
3539
}
3640

3741
static getFileExtension(filename: string): string {

0 commit comments

Comments
 (0)