read PDF/TXT without temporary files (#197)
This commit is contained in:
parent
1366b36c32
commit
7ab8a5877b
|
@ -48,7 +48,7 @@ See https://github.com/adobe-type-tools/cmap-resources
|
|||
<!--<script src="pdf.js"></script>-->
|
||||
|
||||
<script type="text/javascript">
|
||||
var DEFAULT_URL = "{{ url_for('static', filename=pdffile) }}";
|
||||
var DEFAULT_URL = "{{ url_for('serve_book', book_id=pdffile,book_format='pdf') }}";
|
||||
var PDFWORKER_LOCATION="{{ url_for('static', filename='js/libs/pdf.worker.js') }}";
|
||||
// var IMAGE_LOCATION="{{ url_for('static', filename='css/../images') }}";
|
||||
var IMAGE_LOCATION="{{ url_for('static', filename='/images/') }}";
|
||||
|
|
|
@ -109,7 +109,7 @@
|
|||
$("#area").width($("#area").width());
|
||||
$("#content").width($("#content").width());
|
||||
//bind text
|
||||
$("#content").load("{{ url_for('static', filename=txtfile) }}",function(textStr) {
|
||||
$("#content").load("{{ url_for('serve_book', book_id=txtfile,book_format='txt') }}",function(textStr) {
|
||||
$(this).height($(this).parent().height()*0.95);
|
||||
$(this).text(textStr);
|
||||
});
|
||||
|
|
32
cps/web.py
32
cps/web.py
|
@ -1581,6 +1581,24 @@ def get_cover(cover_path):
|
|||
else:
|
||||
return send_from_directory(os.path.join(config.config_calibre_dir, cover_path), "cover.jpg")
|
||||
|
||||
@app.route("/show/<book_id>/<book_format>")
|
||||
@login_required_if_no_ano
|
||||
def serve_book(book_id,book_format):
|
||||
book_format = book_format.split(".")[0]
|
||||
book = db.session.query(db.Books).filter(db.Books.id == book_id).first()
|
||||
data = db.session.query(db.Data).filter(db.Data.book == book.id).filter(db.Data.format == book_format.upper()).first()
|
||||
app.logger.info(data.name)
|
||||
if config.config_use_google_drive:
|
||||
headers = Headers()
|
||||
try:
|
||||
headers["Content-Type"] = mimetypes.types_map['.' + book_format]
|
||||
except KeyError:
|
||||
headers["Content-Type"] = "application/octet-stream"
|
||||
df = gdriveutils.getFileFromEbooksFolder(Gdrive.Instance().drive, book.path, data.name + "." + book_format)
|
||||
return do_gdrive_download(df, headers)
|
||||
else:
|
||||
return send_from_directory(os.path.join(config.config_calibre_dir, book.path), data.name + "." + book_format)
|
||||
|
||||
|
||||
@app.route("/opds/thumb_240_240/<path:book_id>")
|
||||
@app.route("/opds/cover_240_240/<path:book_id>")
|
||||
|
@ -1680,19 +1698,9 @@ def read_book(book_id, book_format):
|
|||
zfile.close()
|
||||
return render_title_template('read.html', bookid=book_id, title=_(u"Read a Book"))
|
||||
elif book_format.lower() == "pdf":
|
||||
all_name = str(book_id) + "/" + book.data[0].name + ".pdf"
|
||||
tmp_file = os.path.join(book_dir, book.data[0].name) + ".pdf"
|
||||
if not os.path.exists(tmp_file):
|
||||
pdf_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + ".pdf"
|
||||
copyfile(pdf_file, tmp_file)
|
||||
return render_title_template('readpdf.html', pdffile=all_name, title=_(u"Read a Book"))
|
||||
return render_title_template('readpdf.html', pdffile=book_id, title=_(u"Read a Book"))
|
||||
elif book_format.lower() == "txt":
|
||||
all_name = str(book_id) + "/" + book.data[0].name + ".txt"
|
||||
tmp_file = os.path.join(book_dir, book.data[0].name) + ".txt"
|
||||
if not os.path.exists(all_name):
|
||||
txt_file = os.path.join(config.config_calibre_dir, book.path, book.data[0].name) + ".txt"
|
||||
copyfile(txt_file, tmp_file)
|
||||
return render_title_template('readtxt.html', txtfile=all_name, title=_(u"Read a Book"))
|
||||
return render_title_template('readtxt.html', txtfile=book_id, title=_(u"Read a Book"))
|
||||
elif book_format.lower() == "cbr":
|
||||
all_name = str(book_id) + "/" + book.data[0].name + ".cbr"
|
||||
tmp_file = os.path.join(book_dir, book.data[0].name) + ".cbr"
|
||||
|
|
Loading…
Reference in New Issue
Block a user