public void StageEpub(bool publishWithoutAudio = false)
{
_publishWithoutAudio = publishWithoutAudio;
Debug.Assert(_stagingFolder == null, "EpubMaker should only be used once");
//I (JH) kept having trouble making epubs because this kept getting locked.
SIL.IO.DirectoryUtilities.DeleteDirectoryRobust(Path.Combine(Path.GetTempPath(), kEPUBExportFolder));
_stagingFolder = new TemporaryFolder(kEPUBExportFolder);
// The readium control remembers the current page for each book.
// So it is useful to have a unique name for each one.
// However, it needs to be something we can put in a URL without complications,
// so a guid is better than say the book's own folder name.
StagingDirectory = Path.Combine(_stagingFolder.FolderPath, _book.ID);
// in case of previous versions // Enhance: delete when done? Generate new name if conflict?
var contentFolderName = "content";
_contentFolder = Path.Combine(StagingDirectory, contentFolderName);
Directory.CreateDirectory(_contentFolder); // also creates parent staging directory
var pageIndex = 0;
_manifestItems = new List<string>();
_spineItems = new List<string>();
int firstContentPageIndex = Book.GetIndexLastFrontkMatterPage() + 2; // pageIndex starts at 1
_firstContentPageItem = null;
foreach (XmlElement pageElement in Book.GetPageElements())
{
++pageIndex;
var pageDom = MakePageFile(pageElement, pageIndex, firstContentPageIndex);
// for now, at least, all Bloom book pages currently have the same stylesheets, so we only neeed
//to look at those stylesheets on the first page
if (pageIndex == 1)
CopyStyleSheets(pageDom);
}
string coverPageImageFile = "thumbnail-256.png";
// This thumbnail is otherwise only made when uploading, so it may be out of date.
// Just remake it every time.
ApplicationException thumbNailException = null;
try
{
_thumbNailer.MakeThumbnailOfCover(Book, 256, Form.ActiveForm);
}
catch (ApplicationException e)
{
thumbNailException = e;
}
var coverPageImagePath = Path.Combine(Book.FolderPath, coverPageImageFile);
if (thumbNailException != null || !RobustFile.Exists(coverPageImagePath))
{
NonFatalProblem.Report(ModalIf.All, PassiveIf.All, "Bloom failed to make a high-quality cover page for your book (BL-3209)",
"We will try to make the book anyway, but you may want to try again.",
thumbNailException);
coverPageImageFile = "thumbnail.png"; // Try a low-res image, which should always exist
coverPageImagePath = Path.Combine(Book.FolderPath, coverPageImageFile);
if (!RobustFile.Exists(coverPageImagePath))
{
// I don't think we can make an epub without a cover page so at this point we've had it.
// I suppose we could recover without actually crashing but it doesn't seem worth it unless this
// actually happens to real users.
throw new FileNotFoundException("Could not find or create thumbnail for cover page (BL-3209)", coverPageImageFile);
}
}
CopyFileToEpub(coverPageImagePath);
EmbedFonts(); // must call after copying stylesheets
MakeNavPage();
//supporting files
// Fixed requirement for all epubs
RobustFile.WriteAllText(Path.Combine(StagingDirectory, "mimetype"), @"application/epub+zip");
var metaInfFolder = Path.Combine(StagingDirectory, "META-INF");
Directory.CreateDirectory(metaInfFolder);
var containerXmlPath = Path.Combine(metaInfFolder, "container.xml");
RobustFile.WriteAllText(containerXmlPath, @"<?xml version='1.0' encoding='utf-8'?>
<container version='1.0' xmlns='urn:oasis:names:tc:opendocument:xmlns:container'>
<rootfiles>
<rootfile full-path='content/content.opf' media-type='application/oebps-package+xml'/>
</rootfiles>
</container>");
MakeManifest(coverPageImageFile);
}