Skip to content

Commit 1734c9a

Browse files
authored
Merge branch 'feature-cache' into feature-content-tracker
2 parents 4ee8a59 + 7cc74c5 commit 1734c9a

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/classes/template.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SlideInfo } from '../types/xml-types';
1212
import { XmlHelper } from '../helper/xml-helper';
1313
import { vd } from '../helper/general-helper';
1414
import { ContentTracker } from '../helper/content-tracker';
15+
import CacheHelper from '../helper/cache-helper';
1516

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

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

6266
static import(
6367
location: string,
6468
name?: string,
69+
cache?: CacheHelper,
6570
): PresTemplate | RootPresTemplate {
6671
let newTemplate: PresTemplate | RootPresTemplate;
67-
6872
if (name) {
69-
newTemplate = new Template(location) as PresTemplate;
73+
newTemplate = new Template(location, cache) as PresTemplate;
7074
newTemplate.name = name;
7175
} else {
72-
newTemplate = new Template(location) as RootPresTemplate;
76+
newTemplate = new Template(location, cache) as RootPresTemplate;
7377
newTemplate.slides = [];
7478
newTemplate.counter = [
7579
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: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import JSZip, { InputType, JSZipObject, OutputType } from 'jszip';
55
import { AutomizerSummary, FileInfo } from '../types/types';
66
import { IPresentationProps } from '../interfaces/ipresentation-props';
77
import { contentTracker } from './content-tracker';
8+
import CacheHelper from './cache-helper';
89
import { vd } from './general-helper';
910

1011
export class FileHelper {
@@ -19,6 +20,7 @@ export class FileHelper {
1920
archive: JSZip,
2021
file: string,
2122
type?: OutputType,
23+
cache?: CacheHelper,
2224
): Promise<string | number[] | Uint8Array | ArrayBuffer | Blob | Buffer> {
2325
const exists = FileHelper.check(archive, file);
2426

@@ -50,7 +52,8 @@ export class FileHelper {
5052
return archive.remove(file);
5153
}
5254

53-
static extractFileContent(file: Buffer): Promise<JSZip> {
55+
static extractFileContent(file: Buffer, cache?: CacheHelper): Promise<JSZip> {
56+
cache?.store();
5457
const zip = new JSZip();
5558
return zip.loadAsync(file as unknown as InputType);
5659
}

0 commit comments

Comments
 (0)