/// <summary> /// Save the image locally and then add to the epub manifest /// </summary> /// <param name="epub">The epub to add the image to</param> /// <param name="url">The address the image is located at</param> /// <param name="imgName"></param> /// <returns></returns> public string SaveImage(Epub epub, string url, string imgName) { WebClient webClient = new WebClient(); try { if (!File.Exists(epub.location + EpubStructure.EPUBLOCATION + EpubStructure.CONTENTLOCATION + EpubStructure.IMAGELOCATION + imgName)) { EpubParser.Retry(1, TimeSpan.FromSeconds(60), () => { webClient.DownloadFile(url, epub.location + EpubStructure.EPUBLOCATION + EpubStructure.CONTENTLOCATION + EpubStructure.IMAGELOCATION + imgName); }); } epub.AddToManifest(imgName, EpubStructure.IMAGELOCATION + imgName); } catch (Exception ex) { Logger.LogError("Failed to download: " + url + " - " + imgName); Logger.LogError(ex.Message); } finally { webClient.Dispose(); } return((EpubStructure.IMAGELOCATION + imgName).Replace("\\", "/")); }
/// <summary> /// Create the cover if we need to /// </summary> public void CreateCover() { Logger.LogInfo("CreateCover"); string imgFileName = epub.cover.Split('\\')[epub.cover.Split('\\').Length - 1]; StreamWriter writer = new StreamWriter( File.Create(epub.location + EpubStructure.EPUBLOCATION + EpubStructure.CONTENTLOCATION + EpubStructure.COVERLOCATION)); writer.WriteLine( string.Format(EpubStructure.COMMONCOVER, epub.title, (EpubStructure.IMAGELOCATION + imgFileName).Replace("\\", "/"))); writer.Close(); File.Copy(epub.cover, epub.location + EpubStructure.EPUBLOCATION + EpubStructure.CONTENTLOCATION + EpubStructure.IMAGELOCATION + imgFileName, true); epub.AddToSpine(EpubStructure.COVERLOCATION); epub.AddToManifest(EpubStructure.COVERLOCATION, EpubStructure.COVERLOCATION); epub.AddToManifest(imgFileName, EpubStructure.IMAGELOCATION + imgFileName); }