示例#1
0
文件: main.py 项目: raorn/librarian
def images(book_id, ext):
    ext = '.' + ext
    filepath = get_image_filepath(current_app.config['IMAGE_ROOT_DIR'], book_id, ext=ext)
    mimetype = mimetypes.types_map[ext]
    if not path.exists(filepath):
        abort(404)
    return send_file(filepath, mimetype=mimetype)
示例#2
0
文件: tasks.py 项目: raorn/librarian
def fill_cover_images(zip_path):
    total = updated = 0
    with ZipFile(zip_path, 'r') as zip_file:
        for fb2_filename in zip_file.namelist():
            fb2_file = zip_file.open(fb2_filename)
            id_ = int(fb2_filename.split('.')[0])
            image_info = extract_cover_image(fb2_file)

            if image_info is not None:
                extension = mimetypes.guess_extension(image_info['mimetype'])
                extension = extension or '.jpeg'
                filepath = get_image_filepath(app.config['IMAGE_ROOT_DIR'],
                                              id_,
                                              ext=extension)
                directory = os.path.dirname(filepath)
                create_ifnot_exists(directory)
                with open(filepath, 'wb') as file:
                    file.write(image_info['body'])
                Book.query.filter_by(id=id_).update({'cover_image': extension[1:]})
                updated += 1
            total += 1
        db.session.commit()

    log.info("Finish parsing images from %s: total(%d), updated(%d)",
             zip_path, total, updated)