improvements for pdf upload function

This commit is contained in:
Cervinko Cera 2016-04-04 23:25:53 +02:00
parent 748dd2ad1f
commit 3324669702

View File

@ -757,25 +757,36 @@ def upload():
file = request.files['btn-upload'] file = request.files['btn-upload']
filename = file.filename filename = file.filename
filename_root, fileextension = os.path.splitext(filename) filename_root, fileextension = os.path.splitext(filename)
title_dir = helper.get_valid_filename(filename_root, False) if fileextension.upper() == ".PDF":
filepath = config.DB_ROOT + "/Unknown/" + title_dir title = filename_root
print filepath author = "Unknown"
title_dir = helper.get_valid_filename(title, False)
author_dir = helper.get_valid_filename(author.decode('utf-8'), False)
data_name = title_dir
filepath = config.DB_ROOT + "/" + author_dir + "/" + title_dir
saved_filename = filepath + "/" + data_name + fileextension
if not os.path.exists(filepath): if not os.path.exists(filepath):
os.makedirs(filepath) os.makedirs(filepath)
file.save(os.path.join(filepath, filename)) file.save(saved_filename)
with Image(filename=os.path.join(filepath, filename)+"[0]", resolution=150) as img: file_size = os.path.getsize(saved_filename)
img.compression_quality = 88 has_cover = 0
img.save(filename=os.path.join(filepath, "cover.jpg")) if fileextension.upper() == ".PDF":
is_author = db.session.query(db.Authors).filter(db.Authors.name.like("Unknown")).first() with Image(filename=saved_filename + "[0]", resolution=150) as img:
img.compression_quality = 88
img.save(filename=os.path.join(filepath, "cover.jpg"))
has_cover = 1
is_author = db.session.query(db.Authors).filter(db.Authors.name == author).first()
if is_author: if is_author:
db_author = is_author db_author = is_author
else: else:
db_author = db.Authors("Unknown", "", "") db_author = db.Authors(author, "", "")
db.session.add(db_author) db.session.add(db_author)
db_book = db.Books(filename_root, "", "", datetime.datetime.now(), "", 1, datetime.datetime(101, 01,01), "Unknown/" + title_dir, 1, db_author, []) db_book = db.Books(title, "", "", datetime.datetime.now(), "", 1, datetime.datetime(101, 01,01), author_dir + "/" + title_dir, has_cover, db_author, [])
db_book.authors.append(db_author) db_book.authors.append(db_author)
#todo append data,... db_data = db.Data(db_book, fileextension.upper()[1:], file_size, data_name)
db_book.data.append(db_data)
db.session.add(db_book) db.session.add(db_book)
db.session.commit() db.session.commit()
print filename return render_template('edit_book.html', book=db_book)
return render_template('search.html', searchterm="")