public ActionResult Download(int gutBookId, bool images)
 {
     using (var db = new EbooksContext()) {
         var gutBook = db.GutBooks.Single(b => b.GutBookId == gutBookId);
         // Check whether the book has already been downloaded
         var ef = db.EpubFiles.SingleOrDefault(e => e.GutBookId == gutBook.GutBookId && e.GutBookWithImages == images);
         Epub ep;
         if (ef != null) {
             ep = new Epub(ef.Contents);
         } else {
             ep = new Epub(images ? gutBook.GetEpubImages() : gutBook.GetEpubNoImages());
         }
         try {
             var user = db.GetEbooksUser(User);
             var book = Book.Load(db, ep, user.UserId);
             if (book.Publisher == null) book.Publisher = Publisher.Get(db, "Project Gutenberg");
             if (ef == null) {
                 book.EpubFile.GutBookId = gutBook.GutBookId;
                 book.EpubFile.GutBookWithImages = images;
             }
             db.SaveChanges();
         } finally {
             ep.Dispose();
         }
     }
     return RedirectToAction("Index", "Ebooks");
 }
 public ActionResult ReadContent()
 {
     int bookId = Convert.ToInt32(Request.Url.Segments[3].TrimEnd('/'));
     string file = HttpUtility.UrlDecode(string.Join("", Request.Url.Segments.Skip(4)));
     using (var db = new EbooksContext()) {
         var userId = db.GetEbooksUser(User).UserId;
         var book = db.Books.Single(b => b.BookId == bookId && b.UserId == userId);
         if (book == null) return null;
         using (var ep = new Epub(book)) {
             return new FileStreamResult(ep.GetContentFile(file), "text/html");
         }
     }
 }
        // GET: ReadBook
        public ActionResult Index(int bookId = 0, int spineIndex = 0)
        {
            using (var db = new EbooksContext()) {
                var user = db.GetEbooksUser(User);
                var book = db.Books.SingleOrDefault(b => b.BookId == bookId && b.UserId == user.UserId);
                if (book == null) return RedirectToAction("Index", "Ebooks");
                using (var ep = new Epub(book)) {
                    var spine = ep.SpineRefs.ToArray();
                    spineIndex = Math.Max(0, Math.Min(spineIndex, spine.Length - 1));
                    string url = Url.Action($"ReadContent/{bookId}/{spine[spineIndex]}");

                    var model = new ReadBookViewModel {
                        SpineRefs = ep.SpineRefs.ToArray(),
                        Title = book.Title,
                        Url = url,
                        BookId = bookId,
                        Toc = ep.Toc
                    };

                    return View(model);
                }
            }
        }
 public ActionResult UploadFiles()
 {
     using (var db = new EbooksContext()) {
         var user = db.GetEbooksUser(User);
         foreach (string f in Request.Files) {
             HttpPostedFileBase hpf = Request.Files[f] as HttpPostedFileBase;
             if (hpf.ContentLength == 0) continue;
             using (var m = new MemoryStream(hpf.ContentLength)) {
                 hpf.InputStream.CopyTo(m);
                 var epub = new Epub(m);
                 Book.Load(db, epub, user.UserId);
                 db.SaveChanges();
             }
         }
     }
     return Redirect("~/Ebooks");
 }