public void ProcessRequest(HttpContext context) { HttpRequest request = context.Request; HttpResponse response = context.Response; //Simple protection from external domain requests. if (request.UrlReferrer != null && !string.Equals(request.Url.Host, request.UrlReferrer.Host, StringComparison.InvariantCultureIgnoreCase)) { response.Write("<html>\r\n"); response.Write("<head><title>Access Denied</title></head>\r\n"); response.Write("<body>\r\n"); response.Write("<h1>Access Denied</h1>\r\n"); response.Write("</body>\r\n"); response.Write("</html>"); return; } if (request.QueryString["epub"] == null || request.QueryString["epub"].Trim() == string.Empty) { throw new FileNotFoundException(); } Epub epub = new Epub(ConfigurationManager.AppSettings["EpubFilesPath"] + request.QueryString["epub"]); ExtendedData extendedData = epub.ExtendedData[request.QueryString["filePath"]] as ExtendedData; if (extendedData == null) { return; } response.ContentType = extendedData.MimeType; response.BinaryWrite(extendedData.GetContentAsBinary()); }
private void LoadTableOfContents() { ExtendedData extendedData = ExtendedData[_TocFileName] as ExtendedData; if (extendedData == null) { return; } XElement xElement = XElement.Parse(extendedData.Content); XNamespace xNamespace = xElement.Attribute("xmlns") != null ? xElement.Attribute("xmlns").Value : XNamespace.None; //Developer: Brian Kenney //Date: 7/29/2012 // //some files have the namespace prefix of ncx //if it does then then xNamespace will evaluate to None if (xNamespace != XNamespace.None) { TOC = GetNavigationChildren(xElement.Element(xNamespace + "navMap").Elements(xNamespace + "navPoint"), xNamespace); } else { //Change: Brian Kenney //Date: 7/29/2012 //Change: duplicate dictionary key //Details: the file may have an ncx namespace prefix //romeve the ncx prefix itself XDocument xDocument = XDocument.Parse(@extendedData.Content); xDocument.Root.Add(new XAttribute("xmlns", "http://www.daisy.org/z3986/2005/ncx/")); xDocument.Root.Attributes(XNamespace.Xmlns + "ncx").Remove(); xElement = XElement.Parse(xDocument.ToString()); xNamespace = xElement.Attribute("xmlns") != null?xElement.Attribute("xmlns").Value : XNamespace.None; if (xNamespace != XNamespace.None) { TOC = GetNavigationChildren( xElement.Element(xNamespace + "navMap").Elements(xNamespace + "navPoint"), xNamespace); } } }
private void LoadManifestSectionFromOpfFile(XElement contentOpf, XNamespace xNamespace) { //NOTE: with the content.opf file //NOTE: grab the idref from the spine element and //NOTE: find a match corresponding to the elements listed under the manifest section HashSet <string> alreadyProcessedFiles = new HashSet <string>(StringComparer.CurrentCultureIgnoreCase); foreach (var spinElement in contentOpf.Elements(xNamespace + "spine").Elements()) { var itemElement = contentOpf.Elements(xNamespace + "manifest").Elements().FirstOrDefault( e => e.Attribute("id").Value == spinElement.Attribute("idref").Value); if (itemElement == null) { throw new Exception("Invalid epub file."); } else if (itemElement == null) { continue; } string fileName = Uri.UnescapeDataString(itemElement.Attribute("href").Value); ZipEntry contentZipEntry = _EpubFile.GetEntry(_ContentOpfPath + fileName); if (contentZipEntry == null) { throw new Exception("Invalid epub file."); } else if (contentZipEntry == null) { continue; } // //Developer: Brian Kenney //Date: 7/29/2012 //Change: duplicate dictionary key //Details: ran into a mis-packaged epub file added key check //to ensure that we don't crash should someone mispackage // //check to see if fileName has already been added to Content dictionary if (!Content.Values.Any(item => item.FileName == fileName)) { Content.Add(fileName, new ContentData(fileName, _EpubFile, contentZipEntry)); } if (!alreadyProcessedFiles.Contains(spinElement.Attribute("idref").Value)) { alreadyProcessedFiles.Add(spinElement.Attribute("idref").Value); } } //grab the rest of the elements not already processed in the manifest IEnumerable <XElement> manifestElements = contentOpf.Elements(xNamespace + "manifest") .Elements() .Where(e => !alreadyProcessedFiles.Contains(e.Attribute("id").Value)); foreach (var manifestElement in manifestElements) { string fileName = manifestElement.Attribute("href").Value; ZipEntry extendedZipEntry = _EpubFile.GetEntry(_ContentOpfPath + fileName); if (extendedZipEntry == null) { continue; } //check to see if fileName has already been added to Extended dictionary string trimmedFileName = GetTrimmedFileName(fileName, true); if (!ExtendedData.Keys.Contains(trimmedFileName)) { ExtendedData.Add(trimmedFileName, new ExtendedData(fileName, manifestElement.Attribute("media-type").Value, _EpubFile, extendedZipEntry)); } if (string.Equals(manifestElement.Attribute("media-type").Value, "application/x-dtbncx+xml", StringComparison.CurrentCultureIgnoreCase)) { _TocFileName = manifestElement.Attribute("href").Value; } } }