media.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:henet 作者: AcrDijon 项目源码 文件源码
def get_media_thumbnail(size, filename):
    filename = filename.decode('utf8')

    ext = os.path.splitext(filename)[-1].lower()

    if ext == '.pdf':
        redirect('/resources/images/pdf-200x200.png')
        return

    if ext not in  ('.jpg', '.png', '.jpeg', '.bmp'):
        redirect('/resources/images/blank.png')
        return

    media_dir = app._config['henet']['media_dir']
    thumbnails_dir = app._config['henet']['thumbnails_dir']

    thumbname = size + '-' + filename
    thumbnail_file = os.path.join(thumbnails_dir, thumbname)

    if not os.path.exists(thumbnail_file):
        image_file = os.path.join(media_dir, filename)
        size = [int(i) for i in size.split('x')]
        image = Image.open(image_file)
        image.thumbnail(size)
        image.save(thumbnail_file, 'JPEG')


    mimetype = guess_type(thumbnail_file)[0]
    return static_file(thumbname, root=thumbnails_dir,
                       mimetype=mimetype)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号