Uploader is working on windows

This commit is contained in:
OzzieIsaacs 2016-10-30 11:44:02 +01:00
parent 12db39523c
commit 313116dca7
3 changed files with 63 additions and 54 deletions

View File

@ -106,7 +106,7 @@
{% endif %} {% endif %}
<div class="btn-group" role="group"> <div class="btn-group" role="group">
<button id="btnGroupDrop2" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"> <button id="btnGroupDrop2" type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-eye-open"></span>Read in browser <span class="glyphicon glyphicon-eye-open"></span> Read in browser
<span class="caret"></span> <span class="caret"></span>
</button> </button>
<ul class="dropdown-menu" aria-labelledby="btnGroupDrop2"> <ul class="dropdown-menu" aria-labelledby="btnGroupDrop2">

View File

@ -1,25 +1,24 @@
import os import os
from tempfile import gettempdir
import hashlib import hashlib
from collections import namedtuple from collections import namedtuple
import book_formats import book_formats
tmp_dir = "/tmp/calibre-web"
BookMeta = namedtuple('BookMeta', 'file_path, extension, title, author, cover, description, tags, series, series_id') BookMeta = namedtuple('BookMeta', 'file_path, extension, title, author, cover, description, tags, series, series_id')
""" """
:rtype: BookMeta :rtype: BookMeta
""" """
def upload(file): def upload(file):
tmp_dir = os.path.join(gettempdir(), 'calibre_web')
if not os.path.isdir(tmp_dir): if not os.path.isdir(tmp_dir):
os.mkdir(tmp_dir) os.mkdir(tmp_dir)
filename = file.filename filename = file.filename
filename_root, file_extension = os.path.splitext(filename) filename_root, file_extension = os.path.splitext(filename)
md5 = hashlib.md5() md5 = hashlib.md5()
md5.update(filename) md5.update(filename.encode('utf-8'))
tmp_file_path = os.path.join(tmp_dir, md5.hexdigest()) tmp_file_path = os.path.join(tmp_dir, md5.hexdigest())
file.save(tmp_file_path) file.save(tmp_file_path)
meta = book_formats.process(tmp_file_path, filename_root, file_extension) meta = book_formats.process(tmp_file_path, filename_root, file_extension)

View File

@ -544,48 +544,53 @@ def get_cover(cover_path):
@login_required @login_required
def read_book(book_id,format): def read_book(book_id,format):
book = db.session.query(db.Books).filter(db.Books.id == book_id).first() 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 book :
if not os.path.exists(book_dir): book_dir = os.path.join(config.MAIN_DIR, "cps","static", str(book_id))
os.mkdir(book_dir) if not os.path.exists(book_dir):
if format.lower() == "epub": os.mkdir(book_dir)
#check if mimetype file is exists if format.lower() == "epub":
mime_file = str(book_id) +"/mimetype" #check if mimetype file is exists
if os.path.exists(mime_file) == False: mime_file = str(book_id) +"/mimetype"
epub_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".epub" if os.path.exists(mime_file) == False:
if not os.path.isfile(epub_file): epub_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".epub"
raise ValueError('Error opening eBook. File does not exist: ', epub_file) if not os.path.isfile(epub_file):
zfile = zipfile.ZipFile(epub_file) raise ValueError('Error opening eBook. File does not exist: ', epub_file)
for name in zfile.namelist(): zfile = zipfile.ZipFile(epub_file)
(dirName, fileName) = os.path.split(name) for name in zfile.namelist():
newDir = os.path.join(book_dir, dirName) (dirName, fileName) = os.path.split(name)
if not os.path.exists(newDir): newDir = os.path.join(book_dir, dirName)
try: if not os.path.exists(newDir):
os.makedirs(newDir) try:
except OSError as exception: os.makedirs(newDir)
if exception.errno == errno.EEXIST: except OSError as exception:
pass if exception.errno == errno.EEXIST:
else: pass
raise else:
if fileName: raise
fd = open(os.path.join(newDir, fileName), "wb") if fileName:
fd.write(zfile.read(name)) fd = open(os.path.join(newDir, fileName), "wb")
fd.close() fd.write(zfile.read(name))
zfile.close() fd.close()
return render_template('read.html', bookid=book_id, title="Read a Book") zfile.close()
elif format.lower() == "pdf": return render_template('read.html', bookid=book_id, title="Read a Book")
all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".pdf" elif format.lower() == "pdf":
tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".pdf" all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".pdf"
if os.path.exists(tmp_file) == False: tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".pdf"
pdf_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".pdf" if os.path.exists(tmp_file) == False:
copyfile(pdf_file,tmp_file) pdf_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".pdf"
return render_template('readpdf.html', pdffile=all_name, title="Read a Book") copyfile(pdf_file,tmp_file)
elif format.lower() == "txt": return render_template('readpdf.html', pdffile=all_name, title="Read a Book")
all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".txt" elif format.lower() == "txt":
tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".txt" all_name = str(book_id) +"/"+ urllib.quote(book.data[0].name) +".txt"
if os.path.exists(all_name) == False: tmp_file = os.path.join(book_dir,urllib.quote(book.data[0].name)) + ".txt"
txt_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".txt" if os.path.exists(all_name) == False:
copyfile(txt_file,tmp_file) txt_file = os.path.join(config.DB_ROOT, book.path, book.data[0].name) + ".txt"
return render_template('readtxt.html', txtfile=all_name, title="Read a Book") copyfile(txt_file,tmp_file)
return render_template('readtxt.html', txtfile=all_name, title="Read a Book")
else :
flash("Error opening eBook. File does not exist or file is not accessible:", category="error")
return redirect('/' or url_for("index", _external=True))
@app.route("/download/<int:book_id>/<format>") @app.route("/download/<int:book_id>/<format>")
@login_required @login_required
@ -1167,14 +1172,15 @@ def upload():
file = request.files['btn-upload'] file = request.files['btn-upload']
meta = uploader.upload(file) meta = uploader.upload(file)
title = meta.title title = meta.title.encode('utf-8')
author = meta.author author = meta.author.encode('utf-8')
title_dir = helper.get_valid_filename(title.decode('utf-8'), False) title_dir = helper.get_valid_filename(title.decode('utf-8'), False)
author_dir = helper.get_valid_filename(author.decode('utf-8'), False) author_dir = helper.get_valid_filename(author.decode('utf-8'), False)
data_name = title_dir data_name = title_dir
filepath = config.DB_ROOT + "/" + author_dir + "/" + title_dir filepath = config.DB_ROOT + os.sep + author_dir + os.sep + title_dir
saved_filename = filepath + "/" + data_name + meta.extension saved_filename = filepath + os.sep + data_name + meta.extension
if not os.path.exists(filepath): if not os.path.exists(filepath):
try: try:
os.makedirs(filepath) os.makedirs(filepath)
@ -1182,10 +1188,14 @@ def upload():
flash("Failed to create path %s (Permission denied)." % filepath, category="error") flash("Failed to create path %s (Permission denied)." % filepath, category="error")
return redirect(url_for('index', _external=True)) return redirect(url_for('index', _external=True))
try: try:
move(meta.file_path, saved_filename) copyfile(meta.file_path, saved_filename)
except OSError: except OSError, e:
flash("Failed to store file %s (Permission denied)." % saved_filename, category="error") flash("Failed to store file %s (Permission denied)." % saved_filename, category="error")
return redirect(url_for('index', _external=True)) return redirect(url_for('index', _external=True))
try:
os.unlink(meta.file_path)
except OSError, e:
flash("Failed to delete file %s (Permission denied)." % meta.file_path, category="warning")
file_size = os.path.getsize(saved_filename) file_size = os.path.getsize(saved_filename)
if meta.cover is None: if meta.cover is None: