|
|
@ -548,45 +548,45 @@ def read_book(book_id,format):
|
|
|
|
book_dir = os.path.join(config.MAIN_DIR, "cps","static", str(book_id))
|
|
|
|
book_dir = os.path.join(config.MAIN_DIR, "cps","static", str(book_id))
|
|
|
|
if not os.path.exists(book_dir):
|
|
|
|
if not os.path.exists(book_dir):
|
|
|
|
os.mkdir(book_dir)
|
|
|
|
os.mkdir(book_dir)
|
|
|
|
if format.lower() == "epub":
|
|
|
|
if format.lower() == "epub":
|
|
|
|
#check if mimetype file is exists
|
|
|
|
#check if mimetype file is exists
|
|
|
|
mime_file = str(book_id) +"/mimetype"
|
|
|
|
mime_file = str(book_id) +"/mimetype"
|
|
|
|
if os.path.exists(mime_file) == False:
|
|
|
|
if os.path.exists(mime_file) == False:
|
|
|
|
epub_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".epub"
|
|
|
|
epub_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".epub"
|
|
|
|
if not os.path.isfile(epub_file):
|
|
|
|
if not os.path.isfile(epub_file):
|
|
|
|
raise ValueError('Error opening eBook. File does not exist: ', epub_file)
|
|
|
|
raise ValueError('Error opening eBook. File does not exist: ', epub_file)
|
|
|
|
zfile = zipfile.ZipFile(epub_file)
|
|
|
|
zfile = zipfile.ZipFile(epub_file)
|
|
|
|
for name in zfile.namelist():
|
|
|
|
for name in zfile.namelist():
|
|
|
|
(dirName, fileName) = os.path.split(name)
|
|
|
|
(dirName, fileName) = os.path.split(name)
|
|
|
|
newDir = os.path.join(book_dir, dirName)
|
|
|
|
newDir = os.path.join(book_dir, dirName)
|
|
|
|
if not os.path.exists(newDir):
|
|
|
|
if not os.path.exists(newDir):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
os.makedirs(newDir)
|
|
|
|
os.makedirs(newDir)
|
|
|
|
except OSError as exception:
|
|
|
|
except OSError as exception:
|
|
|
|
if exception.errno == errno.EEXIST:
|
|
|
|
if exception.errno == errno.EEXIST:
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
raise
|
|
|
|
raise
|
|
|
|
if fileName:
|
|
|
|
if fileName:
|
|
|
|
fd = open(os.path.join(newDir, fileName), "wb")
|
|
|
|
fd = open(os.path.join(newDir, fileName), "wb")
|
|
|
|
fd.write(zfile.read(name))
|
|
|
|
fd.write(zfile.read(name))
|
|
|
|
fd.close()
|
|
|
|
fd.close()
|
|
|
|
zfile.close()
|
|
|
|
zfile.close()
|
|
|
|
return render_template('read.html', bookid=book_id, title="Read a Book")
|
|
|
|
return render_template('read.html', bookid=book_id, title="Read a Book")
|
|
|
|
elif format.lower() == "pdf":
|
|
|
|
elif format.lower() == "pdf":
|
|
|
|
all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".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"
|
|
|
|
tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".pdf"
|
|
|
|
if os.path.exists(tmp_file) == False:
|
|
|
|
if os.path.exists(tmp_file) == False:
|
|
|
|
pdf_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".pdf"
|
|
|
|
pdf_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".pdf"
|
|
|
|
copyfile(pdf_file,tmp_file)
|
|
|
|
copyfile(pdf_file,tmp_file)
|
|
|
|
return render_template('readpdf.html', pdffile=all_name, title="Read a Book")
|
|
|
|
return render_template('readpdf.html', pdffile=all_name, title="Read a Book")
|
|
|
|
elif format.lower() == "txt":
|
|
|
|
elif format.lower() == "txt":
|
|
|
|
all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".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"
|
|
|
|
tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".txt"
|
|
|
|
if os.path.exists(all_name) == False:
|
|
|
|
if os.path.exists(all_name) == False:
|
|
|
|
txt_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".txt"
|
|
|
|
txt_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".txt"
|
|
|
|
copyfile(txt_file,tmp_file)
|
|
|
|
copyfile(txt_file,tmp_file)
|
|
|
|
return render_template('readtxt.html', txtfile=all_name, title="Read a Book")
|
|
|
|
return render_template('readtxt.html', txtfile=all_name, title="Read a Book")
|
|
|
|
else :
|
|
|
|
else :
|
|
|
|
flash("Error opening eBook. File does not exist or file is not accessible:", category="error")
|
|
|
|
flash("Error opening eBook. File does not exist or file is not accessible:", category="error")
|
|
|
|
return redirect('/' or url_for("index", _external=True))
|
|
|
|
return redirect('/' or url_for("index", _external=True))
|
|
|
|