#coding=utf-8
from __future__ import division
from flask import request, session, g, send_file, url_for, abort, render_template, flash, jsonify, make_response, Response, redirect
from werkzeug.utils import secure_filename
from werkzeug.exceptions import NotFound
from flask import Flask, request, make_response, send_file, safe_join, redirect, Response
import sys, os, cgi, urllib, posixpath, argparse
from StringIO import StringIO
from pypinyin import lazy_pinyin
import commands
import json
def init_router(app):
def show_directory(path):
list = os.listdir(path)
list.sort(key=lambda a: a.lower())
list = eval(json.dumps(list, ensure_ascii=False))
f = StringIO()
print path
path = str(path).strip(".")
displaypath = cgi.escape(path)
f.write('<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">')
f.write("<html>\n<title>产品文档</title>\n")
encoding = sys.getfilesystemencoding()
f.write('<meta http-equiv="Content-Type" content="text/html; charset=%s">' % encoding)
f.write("<body>\n<h2>最新产品文档列表</h2>\n")
f.write("<hr>\n<ul>\n")
for name in list:
fullname = path + name
displayname = linkname = name
if os.path.splitext(name)[1] in app.config["IGNORED"]:
continue
if name in app.config["IGNORED"]:
continue
if os.path.isdir(fullname):
displayname = name + "/"
linkname = name + "/"
if os.path.islink(fullname):
displayname = name + "@"
if os.path.isdir(path + "/"+name):
if "index.html" in os.listdir(path + "/"+name):
linkname = name + "/index.html"
f.write('<li><a href="%s">%s</a>\n'
% (urllib.quote(linkname), cgi.escape(displayname)))
else:
f.write('<li><a href="%s">%s</a>\n'
% (urllib.quote(name), cgi.escape(displayname)))
f.write("</ul>\n<hr>\n</body>\n</html>\n")
length = f.tell()
f.seek(0)
resp = make_response(f.read())
return resp
@app.route('/<path:filename>')
@app.route('/', methods=['POST', 'GET'])
def show_file(filename=''):
global ROOT_DIR
ROOT_DIR = os.path.abspath('../../../share/')
print ROOT_DIR
if request.method == 'GET':
filename = safe_join(ROOT_DIR, "out_put/"+filename)
print filename
if os.path.isdir(filename):
return show_directory(filename)
if not os.path.isfile(filename):
raise NotFound()
return send_file(filename, as_attachment=False)
@app.before_request
def before_request():
create_path()
@app.after_request
def after_request(response):
return response
@app.errorhandler(404)
def url_not_found(error):
"""重新定义404报错"""
return make_response(jsonify({'error': 'URL occurs errors'}), 404)
@app.errorhandler(400)
def params_not_found(error):
"""重新定义400报错"""
return make_response(jsonify({'error': 'Params occurs errors'}), 400)
def get_locale():
return request.accept_languages.best_match(app.config['LANGUAGES'].keys())
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1] in app.config['ALLOWED_EXTENSIONS']
def create_path():
upload_path = app.config["UPLOAD_FOLDER"]
if not os.path.exists(upload_path):
os.makedirs(upload_path)