Skip to content

Commit

Permalink
fix: don’t crash when an image is missing in the EPUB
Browse files Browse the repository at this point in the history
When an image resource is missing in the EPUB, the copy
operation is ignored and a warning message is logged.

Closes #44
  • Loading branch information
rdeltour committed Oct 5, 2017
1 parent 4d816fe commit cec84b1
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/report/report.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,17 @@ module.exports = class Report {
const fromPath = img.path;
const toPath = path.join(outdir, 'data', img.src);
delete img.path;
return fs.copy(fromPath, toPath, {
overwrite: false,
});
return fs.pathExists(fromPath)
.then((exists) => {
if (exists) {
return fs.copy(fromPath, toPath, {
overwrite: false,
});
} else {
winston.warn(`Couldn’t copy resource '${img.src}'`);
return Promise.resolve();
}
});
});
}
});
Expand Down
9 changes: 9 additions & 0 deletions tests/__tests__/report_files.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ test('files don’t leak outside the report dir', async () => {
expect(fs.existsSync(path.join(outpath, 'report.html'))).toBeTruthy();
expect(fs.existsSync(path.join(outpath, 'data/EPUB/images/img_001.jpg'))).toBeTruthy();
});

test('don’t crash when a resource isn’t in the EPUB', async () => {
// Add another directory level to prevent any leak in the user's temp dir
const outpath = path.join(outdir.name, 'report');
fs.mkdirSync(outpath);
expect.assertions(1);
await ace(path.join(__dirname, '../data/fs-resource-missing'), { outdir: outpath });
expect(fs.existsSync(path.join(outpath, 'report.html'))).toBeTruthy();
});
10 changes: 10 additions & 0 deletions tests/data/fs-resource-missing/EPUB/content_001.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title>Minimal EPUB</title>
</head>
<body>
<h1>Loomings</h1>
<p>Call me Ishmael.</p>
<img src="image_001.jpg" alt="dummy"/>
</body>
</html>
12 changes: 12 additions & 0 deletions tests/data/fs-resource-missing/EPUB/nav.xhtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="en">
<head>
<title>Minimal Nav</title>
</head>
<body>
<nav epub:type="toc">
<ol>
<li><a href="content_001.xhtml">content 001</a></li>
</ol>
</nav>
</body>
</html>
24 changes: 24 additions & 0 deletions tests/data/fs-resource-missing/EPUB/package.opf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<package xmlns="http://www.idpf.org/2007/opf" version="3.0" xml:lang="en" unique-identifier="uid">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
<dc:title id="title">Minimal EPUB 3.0</dc:title>
<dc:language>en</dc:language>
<dc:identifier id="uid">NOID</dc:identifier>
<meta property="dcterms:modified">2017-01-01T00:00:01Z</meta>
<meta property="dcterms:modified">2017-01-01T00:00:01Z</meta>
<meta property="schema:accessibilityFeature">structuralNavigation</meta>
<meta property="schema:accessibilitySummary">everything OK!</meta>
<meta property="schema:accessibilityHazard">noFlashingHazard</meta>
<meta property="schema:accessibilityHazard">noSoundHazard</meta>
<meta property="schema:accessibilityHazard">noMotionSimulationHazard</meta>
<meta property="schema:accessMode">textual</meta>
<meta property="schema:accessModeSufficient">textual</meta>
</metadata>
<manifest>
<item id="nav" href="nav.xhtml" media-type="application/xhtml+xml" properties="nav"/>
<item id="content_001" href="content_001.xhtml" media-type="application/xhtml+xml"/>
</manifest>
<spine>
<itemref idref="content_001" />
</spine>
</package>
6 changes: 6 additions & 0 deletions tests/data/fs-resource-missing/META-INF/container.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8" ?>
<container version="1.0" xmlns="urn:oasis:names:tc:opendocument:xmlns:container">
<rootfiles>
<rootfile full-path="EPUB/package.opf" media-type="application/oebps-package+xml"/>
</rootfiles>
</container>
1 change: 1 addition & 0 deletions tests/data/fs-resource-missing/mimetype
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
application/epub+zip

0 comments on commit cec84b1

Please sign in to comment.