Skip to content

Commit 82e47cf

Browse files
authored
Merge branch 'feature-cache' into feature-cache-1
2 parents 643b7d5 + a6da056 commit 82e47cf

File tree

3 files changed

+43
-6
lines changed

3 files changed

+43
-6
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: 5 additions & 1 deletion
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);
@@ -36,7 +39,8 @@ export class FileHelper {
3639
return true;
3740
}
3841

39-
static extractFileContent(file: Buffer): Promise<JSZip> {
42+
static extractFileContent(file: Buffer, cache?: CacheHelper): Promise<JSZip> {
43+
cache?.store();
4044
const zip = new JSZip();
4145
return zip.loadAsync(file as unknown as InputType);
4246
}

0 commit comments

Comments
 (0)