def check_static_image(file_path): app.logger.debug('check_static_image file_path = %s' % file_path) filename, suffix = os.path.splitext(file_path) if suffix != '.png' and suffix != '.jpg' and suffix != '.gif': return category = split_category(file_path) if category is None: return is_origin = True if filename[len(filename) - 7:] != '_origin': is_origin = False else: filename = filename[:len(filename) - 7] markdown_image_path = utils.fix_sep( os.path.join(MARKDOWN_ROOT, filename + suffix), os.sep) if not os.path.isfile(markdown_image_path): return static_image_path = utils.fix_sep( os.path.join(STATIC_ROOT, filename + suffix), os.sep) if not os.path.exists(static_image_path) \ or os.path.getmtime(markdown_image_path) != os.path.getmtime(static_image_path): utils.check_dirs(static_image_path) # 从markdown目录拷贝图片到static目录,并保留原始的修改时间戳 shutil.copy2(markdown_image_path, static_image_path) app.logger.debug('copy image success') thumb_path = '%s_thumb%s' % os.path.splitext(static_image_path) if not os.path.exists(thumb_path) \ or os.path.getmtime(markdown_image_path) != os.path.getmtime(thumb_path): # 制作缩略图 img = Image.open(static_image_path) width, height = img.size if width <= IMAGE_MAX_WIDTH: return app.send_static_file(filename + suffix) if os.path.exists(thumb_path): os.remove(thumb_path) rate = IMAGE_MAX_WIDTH / width img.thumbnail((math.floor(width * rate), math.floor(height * rate))) img.save(thumb_path) os.utime(thumb_path, (os.path.getatime(markdown_image_path), os.path.getmtime(markdown_image_path))) app.logger.debug('make thumb success') if is_origin: # 返回原图 return app.send_static_file(filename + suffix) # 返回缩略图 return app.send_static_file('%s_thumb%s' % (filename, suffix))
def update_markdown_thumbnail(md_path, insert=True): with codecs.open(md_path, 'r', 'utf-8') as infile: comments = match_comment(infile.read()) title = comments.get('title', '') date = comments.get('date', '').replace('/', '-') dates = date.split('-') if len(dates) == 3: date = dates[0] date += '-' date += dates[1] if len(dates[1]) != 1 else '0' + dates[1] date += '-' date += dates[2] if len(dates[2]) != 1 else '0' + dates[2] label = comments.get('label', '') desc = comments.get('desc', '') md_path = utils.fix_sep(md_path, '/') if insert: mtime = os.path.getmtime(md_path) category = md_path.split('/')[1] hits = random.randint(128, 512) data = [(category, title, date, label, desc, md_path, mtime, hits)] helper.save(data) else: # 更新db内数据时,只更新markdown内容数据,不更新mtime。 # 当请求某个markdown网页时,db内的mtime与markdown文件的mtime做对比, # 如果对比不一致,则重新生成markdown网页并保存,然后更新db内的mtime data = [(title, date, label, desc, md_path)] helper.update_thumbnail(data)
def check_markdown(md_path): md_path = utils.fix_sep(md_path, '/') result = helper.fetchone(md_path) if result is None: return if len(result) == 0: update_markdown_thumbnail(md_path) return if result[0][helper.MODIFIED_TIME] != os.path.getmtime(md_path): update_markdown_thumbnail(md_path, False) return
def show_markdown(file_path): app.logger.debug('show_markdown file_path = %s' % file_path) filename, suffix = os.path.splitext(file_path) if suffix != '.md': return show_static(file_path) markdown_path = utils.fix_sep(os.path.join(MARKDOWN_ROOT, file_path), os.sep) if not os.path.isfile(markdown_path): return page_not_found('not found [%s]', file_path) else: html_path = utils.fix_sep( os.path.join(STATIC_ROOT, '%s.html' % filename), os.sep) if not os.path.exists(html_path): make_markdown_html(file_path) data = thumbnail.fetchone('markdown/' + file_path) mtime = os.path.getmtime(markdown_path) if len(data) != 0 and data[0][thumbnail.MODIFIED_TIME] != mtime: make_markdown_html(file_path) thumbnail.update_modified_time('markdown/' + file_path, mtime) thumbnail.update_hits('markdown/' + file_path) return app.send_static_file('%s.html' % filename)
def make_markdown_html(file_path): app.logger.debug('make_markdown_html file_path = %s' % file_path) category = split_category(file_path) blog_list = calc_markdown_thumbnail_list(category=category) # 转换markdown为html,并存入文件 markdown_path = utils.fix_sep(os.path.join(MARKDOWN_ROOT, file_path), os.sep) html_path = utils.fix_sep( os.path.join(STATIC_ROOT, os.path.splitext(file_path)[0] + '.html'), os.sep) data = thumbnail.fetchone(utils.fix_sep(markdown_path, '/')) title = data[0][thumbnail.TITLE] if len(data) != 0 else '博客' with codecs.open(markdown_path, 'r', 'utf-8') as infile: utils.check_dirs(html_path) with codecs.open(html_path, 'w', 'utf-8', errors='xmlcharrefreplace') as outfile: blog_html = markdown_to_html(infile.read()) outfile.write( render_html(title, category, blog_list['labels'], blog_list['dates'], blog_html)) app.logger.info('convert and write "%s" success' % file_path)
def split_category(file_path): file_path = utils.fix_sep(file_path, '/') split = file_path.split('/') return split[len(split) - 2]