def upload(): if not config.UPLOADING: abort(404) ## create the function for sorting... db.session.connection().connection.connection.create_function("title_sort",1,db.title_sort) db.session.connection().connection.connection.create_function('uuid4', 0, lambda : str(uuid4())) if request.method == 'POST' and 'btn-upload' in request.files: file = request.files['btn-upload'] filename = file.filename filename_root, fileextension = os.path.splitext(filename) if fileextension.upper() == ".PDF": title = filename_root author = "Unknown" else: flash("Upload is only available for PDF files", category="error") return redirect(url_for('index')) 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): try: os.makedirs(filepath) except OSError: flash("Failed to create path %s (Permission denied)." % filepath, category="error") return redirect(url_for('index')) try: file.save(saved_filename) except OSError: flash("Failed to store file %s (Permission denied)." % saved_filename, category="error") return redirect(url_for('index')) file_size = os.path.getsize(saved_filename) has_cover = 0 if fileextension.upper() == ".PDF": if use_generic_pdf_cover: basedir = os.path.dirname(__file__) print basedir copyfile(os.path.join(basedir, "static/generic_cover.jpg"), os.path.join(filepath, "cover.jpg")) else: 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: db_author = is_author else: db_author = db.Authors(author, "", "") db.session.add(db_author) db_book = db.Books(title, "", "", datetime.datetime.now(), datetime.datetime(101, 01,01), 1, datetime.datetime.now(), author_dir + "/" + title_dir, has_cover, db_author, []) db_book.authors.append(db_author) 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.commit() return render_template('edit_book.html', book=db_book)
def test_check_Limit_Length(self): helper.config.config_unicode_filename = False self.assertEqual(helper.get_valid_filename(u'1234567890123456789012345678901234567890123456789012345678' u'901234567890123456789012345678901234567890123456789012345678901234567890'), u'123456789012345678901' u'23456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789' u'012345678') self.assertEqual(helper.get_valid_filename(u'执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执' u'执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执', chars=96), '执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执') self.assertEqual(helper.get_valid_filename(u'1执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执' u'执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执', chars=96), '1执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执') self.assertEqual(helper.get_valid_filename(u'12执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执' u'执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执', chars=96), '12执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执执')
def get_opds_download_link(book_id, format): format = 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 == format.upper()).first() helper.update_download(book_id, int(current_user.id)) author = helper.get_normalized_author(book.author_sort) file_name = book.title if len(author) > 0: file_name = author+'-'+file_name file_name = helper.get_valid_filename(file_name) response = make_response(send_from_directory(os.path.join(config.DB_ROOT, book.path), data.name + "." +format)) response.headers["Content-Disposition"] = "attachment; filename=%s.%s" % (data.name, format) return response
def upload(): if not config.UPLOADING: abort(404) ## create the function for sorting... db.session.connection().connection.connection.create_function("title_sort",1,db.title_sort) db.session.connection().connection.connection.create_function('uuid4', 0, lambda : str(uuid4())) if request.method == 'POST' and 'btn-upload' in request.files: file = request.files['btn-upload'] meta = uploader.upload(file) title = meta.title author = meta.author 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 + meta.extension if not os.path.exists(filepath): try: os.makedirs(filepath) except OSError: flash("Failed to create path %s (Permission denied)." % filepath, category="error") return redirect(url_for('index')) try: move(meta.file_path, saved_filename) except OSError: flash("Failed to store file %s (Permission denied)." % saved_filename, category="error") return redirect(url_for('index')) file_size = os.path.getsize(saved_filename) if meta.cover is None: has_cover = 0 basedir = os.path.dirname(__file__) copyfile(os.path.join(basedir, "static/generic_cover.jpg"), os.path.join(filepath, "cover.jpg")) else: has_cover = 1 move(meta.cover, os.path.join(filepath, "cover.jpg")) is_author = db.session.query(db.Authors).filter(db.Authors.name == author).first() if is_author: db_author = is_author else: db_author = db.Authors(author, "", "") db.session.add(db_author) path = os.path.join(author_dir, title_dir) db_book = db.Books(title, "", "", datetime.datetime.now(), datetime.datetime(101, 01,01), 1, datetime.datetime.now(), path, has_cover, db_author, []) db_book.authors.append(db_author) db_data = db.Data(db_book, meta.extension.upper()[1:], file_size, data_name) db_book.data.append(db_data) db.session.add(db_book) db.session.commit() author_names = [] for author in db_book.authors: author_names.append(author.name) cc = db.session.query(db.Custom_Columns).filter(db.Custom_Columns.datatype.notin_(db.cc_exceptions)).all() if current_user.role_edit() or current_user.role_admin(): return render_template('edit_book.html', book=db_book, authors=author_names, cc=cc) book_in_shelfs = [] return render_template('detail.html', entry=db_book, cc=cc, title=db_book.title, books_shelfs=book_in_shelfs)
def test_check_deg_eur_replacement(self): helper.config.config_unicode_filename = True self.assertEqual(helper.get_valid_filename(u'°€'), u'degEUR')
def test_check_char_replacement(self): helper.config.config_unicode_filename = False self.assertEqual(helper.get_valid_filename(u'A*B+C:D"E/F<G>H?'), u'A_B_C_D_E_F_G_H_') self.assertEqual(helper.get_valid_filename(u'Alfaman| Name'), u'Alfaman, Name') self.assertEqual(helper.get_valid_filename(u'**++** Numi **++**'), u'_ Numi _')
def test_check_finish_Dot(self): helper.config.config_unicode_filename = False self.assertEqual(helper.get_valid_filename(u'Nameless.'), u'Nameless_')
def test_whitespaces(self): helper.config.config_unicode_filename = False self.assertEqual(helper.get_valid_filename(u' Alfaman '), u'Alfaman')
def test_check_chinese_Characters(self): helper.config.config_unicode_filename = True self.assertEqual(helper.get_valid_filename(u'执一'), u'Zhi Yi') helper.config.config_unicode_filename = False self.assertEqual(helper.get_valid_filename(u'执一'), u'执一')
def test_check_umlauts(self): helper.config.config_unicode_filename = True self.assertEqual(helper.get_valid_filename(u'ÄÜÖäöü'), u'AUOaou')
def test_check_doubleS(self): helper.config.config_unicode_filename = True self.assertEqual(helper.get_valid_filename(u'§ß'), u'SSss')
def test_check_high23(self): helper.config.config_unicode_filename = True self.assertEqual(helper.get_valid_filename(u'²³'), u'23')