-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpptx-surgeon-2-pptx.js
157 lines (144 loc) · 6.61 KB
/
pptx-surgeon-2-pptx.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
** pptx-surgeon -- PowerPoint OpenXML File Surgeon
** Copyright (c) 2020-2021 Dr. Ralf S. Engelschall <rse@engelschall.com>
**
** Permission is hereby granted, free of charge, to any person obtaining
** a copy of this software and associated documentation files (the
** "Software"), to deal in the Software without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Software, and to
** permit persons to whom the Software is furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Software.
**
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
const path = require("path")
const fs = require("fs").promises
const chalk = require("chalk")
const JSZip = require("jszip")
const Tmp = require("tmp")
const mkdirp = require("mkdirp")
const rimraf = require("rimraf")
module.exports = class Archive {
/* configuration options */
constructor (options = {}) {
this.options = Object.assign({}, {
log: (level, msg) => undefined,
xml: null,
basedir: null,
keep: false,
tool: "pptx-surgeon"
}, options)
}
/* load PPTX file */
async load (pptxfile) {
/* determine base directory */
this.tmp = null
this.basedir = this.options.basedir
if (this.basedir === null) {
this.tmp = Tmp.dirSync()
this.basedir = this.tmp.name
this.options.log(1, `created temporary directory ${chalk.blue(this.basedir)}`)
}
/* read PPTX file */
const data = await fs.readFile(pptxfile, { encoding: null })
this.options.log(1, `read PPTX file ${chalk.blue(pptxfile)}: ${data.length} bytes`)
/* parse and unpack PPTX file */
this.options.log(1, `parsing PPTX file ${chalk.blue(pptxfile)}`)
this.zip = new JSZip()
const contents = await this.zip.loadAsync(data)
this.manifest = []
for (const filename of Object.keys(contents.files)) {
/* unpack OpenXML content part */
const file = this.zip.file(filename)
const content = await file.async("nodebuffer")
const filepath = `${this.basedir}/${filename}`
const filedir = path.dirname(filepath)
await mkdirp.sync(filedir, { mode: 0o755 })
await fs.writeFile(`${this.basedir}/${filename}`, content, { encoding: null })
this.manifest.push(filename)
this.options.log(2, `retrieved OpenXML part ${chalk.blue(filename)}: ${content.length} bytes`)
}
}
/* determine OpenXML part(s) */
async parts (type, single = false) {
if (this.options.xml === null)
throw new Error("require XML manipulation instance")
const xml = await this.options.xml.load(`${this.basedir}/[Content_Types].xml`)
let result = this.options.xml.query(xml, `
// Override [
@ContentType = 'application/vnd.openxmlformats-officedocument.${type}+xml'
] / @PartName
`, { single, type: "string" })
if (typeof result === "string")
result = result.replace(/^\//, "")
else if (typeof result === "object" && result instanceof Array)
result = result.map((f) => f.replace(/^\//, ""))
return result
}
/* determine size of OpenXML part */
async partSize (basePart, target) {
const file = path.resolve(path.resolve(this.basedir, path.dirname(basePart)), target)
const base = path.resolve(this.basedir)
if (!(file.length >= base.length && file.substr(0, base.length) === base))
throw new Error("fatal internal error: part not under base directory")
const stats = await (fs.stat(file).catch(() => undefined))
return (stats ? stats.size : 0)
}
/* delete OpenXML part */
async partDelete (basePart, target) {
const file = path.resolve(path.resolve(this.basedir, path.dirname(basePart)), target)
const base = path.resolve(this.basedir)
if (!(file.length >= base.length && file.substr(0, base.length) === base))
throw new Error("fatal internal error: part not under base directory")
const part = file.substr(base.length).replace(/\\/g, "/").replace(/^\//, "")
this.options.log(2, `removing PPTX part ${chalk.blue(part)}`)
await (fs.unlink(file).catch(() => undefined))
this.manifest = this.manifest.filter((filename) => filename !== part)
}
/* backup PPTX file */
async backup (pptxfile, backupfile) {
await fs.copyFile(pptxfile, backupfile)
}
/* save PPTX file */
async save (pptxfile) {
/* pack PPTX file */
this.options.log(1, "packing PPTX content")
const nzip = new JSZip()
for (const filename of this.manifest) {
const content = await fs.readFile(`${this.basedir}/${filename}`, { encoding: null })
this.options.log(2, `storing OpenXML part ${chalk.blue(filename)}: ${content.length} bytes`)
nzip.file(filename, content, {
date: this.zip.file(filename).date,
unixPermissions: this.zip.file(filename).unixPermissions,
comment: this.zip.file(filename).comment,
createFolders: false
})
}
const data = await nzip.generateAsync({
type: "nodebuffer",
compression: "DEFLATE",
compressionLevel: { level: 9 },
comment: `manipulated with ${this.options.tool}`,
platform: process.platform,
streamFiles: false
})
/* write PPTX file */
this.options.log(1, `write PPTX file ${chalk.blue(pptxfile)}: ${data.length} bytes`)
await fs.writeFile(pptxfile, data, { encoding: null })
/* delete temporary filesystem area */
if (this.tmp !== null && !this.options.keep) {
rimraf.sync(this.basedir, { disableGlob: true })
this.tmp = null
}
}
}