From 350f4e21e6aa14b58e9de1a5fafecddaa2efa344 Mon Sep 17 00:00:00 2001 From: wuqi Date: Sat, 16 Jul 2016 16:44:47 +0800 Subject: [PATCH] Add txt reader add txt file reader,and add select for read the book --- cps/templates/detail.html | 19 ++++- cps/templates/read.html | 9 +++ cps/templates/readtxt.html | 152 +++++++++++++++++++++++++++++++++++++ cps/web.py | 95 +++++++++++++---------- vendor/flask/app.py | 2 +- 5 files changed, 236 insertions(+), 41 deletions(-) create mode 100644 cps/templates/readtxt.html diff --git a/cps/templates/detail.html b/cps/templates/detail.html index fd6b6fa7..40d1320a 100644 --- a/cps/templates/detail.html +++ b/cps/templates/detail.html @@ -104,8 +104,23 @@ {% if g.user.kindle_mail %} Send to Kindle {% endif %} - {% for format in entry.data if format.format|lower == 'epub' or format.format|lower == 'pdf' %} - Read in browser + {% for format in entry.data %} + {%if format.format|lower == 'epub' or format.format|lower == 'txt' or format.format|lower == 'pdf' %} +
+ + +
+ {% break %} + {% endif %} {%endfor%} diff --git a/cps/templates/read.html b/cps/templates/read.html index 6530fe35..badd2f2a 100644 --- a/cps/templates/read.html +++ b/cps/templates/read.html @@ -24,6 +24,15 @@ // fileStorage.filePath = EPUBJS.filePath; window.reader = ePubReader("{{ url_for('static', filename=bookid) }}/"); + //keybind + $(document).keydown(function(event){ + if(event.keyCode == 37){ + window.reader.book.prevPage(); + } + if(event.keyCode == 39){ + window.reader.book.nextPage(); + } + }); } }; diff --git a/cps/templates/readtxt.html b/cps/templates/readtxt.html new file mode 100644 index 00000000..379a3646 --- /dev/null +++ b/cps/templates/readtxt.html @@ -0,0 +1,152 @@ + + + + + + Basic txt Reader + + + + + + + + + + + + + + +
+ +
+ +
+ + + + \ No newline at end of file diff --git a/cps/web.py b/cps/web.py index 4ca6fecf..42f169f7 100755 --- a/cps/web.py +++ b/cps/web.py @@ -26,13 +26,16 @@ import json import urllib import datetime from uuid import uuid4 +import os.path +import shutil +import re try: from wand.image import Image use_generic_pdf_cover = False except ImportError, e: use_generic_pdf_cover = True from shutil import copyfile - +from cgi import escape app = (Flask(__name__)) formatter = logging.Formatter( @@ -492,49 +495,65 @@ def author(name): def get_cover(cover_path): return send_from_directory(os.path.join(config.DB_ROOT, cover_path), "cover.jpg") -@app.route("/read/") +@app.route("/read//") @login_required -def read_book(book_id): +def read_book(book_id,format): book = db.session.query(db.Books).filter(db.Books.id == book_id).first() book_dir = os.path.join(config.MAIN_DIR, "cps","static", str(book_id)) if not os.path.exists(book_dir): os.mkdir(book_dir) - for data in book.data: - if data.format.lower() == "epub": - epub_file = os.path.join(config.DB_ROOT, book.path, data.name) + ".epub" - if not os.path.isfile(epub_file): - raise ValueError('Error opening eBook. File does not exist: ', epub_file) - zfile = zipfile.ZipFile(epub_file) - for name in zfile.namelist(): - (dirName, fileName) = os.path.split(name) - newDir = os.path.join(book_dir, dirName) - if not os.path.exists(newDir): - try: - os.makedirs(newDir) - except OSError as exception: - if exception.errno == errno.EEXIST: - pass - else: - raise - if fileName: - fd = open(os.path.join(newDir, fileName), "wb") - fd.write(zfile.read(name)) - fd.close() - zfile.close() - return render_template('read.html', bookid=book_id, title="Read a Book") - elif data.format.lower() == "pdf": - pdf_file = os.path.join(config.DB_ROOT, book.path, data.name) + ".pdf" - tmp_file = os.path.join(book_dir,urllib.quote(data.name)) + ".pdf" - copyfile(pdf_file,tmp_file) - all_name = str(book_id) +"/"+ urllib.quote(data.name) +".pdf" - return render_template('readpdf.html', pdffile=all_name, title="Read a Book") + if format.lower() == "epub": + epub_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".epub" + if not os.path.isfile(epub_file): + raise ValueError('Error opening eBook. File does not exist: ', epub_file) + zfile = zipfile.ZipFile(epub_file) + for name in zfile.namelist(): + (dirName, fileName) = os.path.split(name) + newDir = os.path.join(book_dir, dirName) + if not os.path.exists(newDir): + try: + os.makedirs(newDir) + except OSError as exception: + if exception.errno == errno.EEXIST: + pass + else: + raise + if fileName: + fd = open(os.path.join(newDir, fileName), "wb") + fd.write(zfile.read(name)) + fd.close() + zfile.close() + return render_template('read.html', bookid=book_id, title="Read a Book") + elif format.lower() == "pdf": + pdf_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".pdf" + tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".pdf" + copyfile(pdf_file,tmp_file) + all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".pdf" + return render_template('readpdf.html', pdffile=all_name, title="Read a Book") + elif format.lower() == "txt": + #change txt to epub + txt_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".txt" + tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".txt" + copyfile(txt_file,tmp_file) + all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".txt" + return render_template('readtxt.html', txtfile=all_name, title="Read a Book") else: - for data in book.data: - if data.format.lower() == "epub": - return render_template('read.html', bookid=book_id, title="Read a Book") - elif data.format.lower() == "pdf": - all_name = str(book_id) +"/"+ urllib.quote(data.name) +".pdf" - return render_template('readpdf.html', pdffile=all_name, title="Read a Book") + if format.lower() == "epub": + return render_template('read.html', bookid=book_id, title="Read a Book") + elif format.lower() == "pdf": + all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".pdf" + tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".pdf" + if os.path.exists(tmp_file) == False: + pdf_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".pdf" + copyfile(pdf_file,tmp_file) + return render_template('readpdf.html', pdffile=all_name, title="Read a Book") + elif format.lower() == "txt": + all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".txt" + tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".txt" + if os.path.exists(all_name) == False: + txt_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".txt" + copyfile(txt_file,tmp_file) + return render_template('readtxt.html', txtfile=all_name, title="Read a Book") @app.route("/download//") @login_required diff --git a/vendor/flask/app.py b/vendor/flask/app.py index 98ecb106..44225a25 100644 --- a/vendor/flask/app.py +++ b/vendor/flask/app.py @@ -265,7 +265,7 @@ class Flask(_PackageBoundObject): #: Options that are passed directly to the Jinja2 environment. jinja_options = ImmutableDict( - extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_'] + extensions=['jinja2.ext.autoescape', 'jinja2.ext.with_','jinja2.ext.loopcontrols'] ) #: Default configuration parameters.