|
|
@ -19,6 +19,91 @@ def index(): |
|
|
|
return render_template('default/files.html') |
|
|
|
return render_template('default/files.html') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@files.route('get_body', methods=['POST']) |
|
|
|
|
|
|
|
def getBody(): |
|
|
|
|
|
|
|
path = request.form.get('path', '').encode('utf-8') |
|
|
|
|
|
|
|
if not os.path.exists(path): |
|
|
|
|
|
|
|
return public.returnJson(False, '文件不存在', (path,)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if os.path.getsize(path) > 2097152: |
|
|
|
|
|
|
|
return public.returnJson(False, u'不能在线编辑大于2MB的文件!') |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fp = open(path, 'rb') |
|
|
|
|
|
|
|
data = {} |
|
|
|
|
|
|
|
data['status'] = True |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
if fp: |
|
|
|
|
|
|
|
from chardet.universaldetector import UniversalDetector |
|
|
|
|
|
|
|
detector = UniversalDetector() |
|
|
|
|
|
|
|
srcBody = b"" |
|
|
|
|
|
|
|
for line in fp.readlines(): |
|
|
|
|
|
|
|
detector.feed(line) |
|
|
|
|
|
|
|
srcBody += line |
|
|
|
|
|
|
|
detector.close() |
|
|
|
|
|
|
|
char = detector.result |
|
|
|
|
|
|
|
data['encoding'] = char['encoding'] |
|
|
|
|
|
|
|
if char['encoding'] == 'GB2312' or not char['encoding'] or char[ |
|
|
|
|
|
|
|
'encoding'] == 'TIS-620' or char['encoding'] == 'ISO-8859-9': |
|
|
|
|
|
|
|
data['encoding'] = 'GBK' |
|
|
|
|
|
|
|
if char['encoding'] == 'ascii' or char[ |
|
|
|
|
|
|
|
'encoding'] == 'ISO-8859-1': |
|
|
|
|
|
|
|
data['encoding'] = 'utf-8' |
|
|
|
|
|
|
|
if char['encoding'] == 'Big5': |
|
|
|
|
|
|
|
data['encoding'] = 'BIG5' |
|
|
|
|
|
|
|
if not char['encoding'] in ['GBK', 'utf-8', |
|
|
|
|
|
|
|
'BIG5']: |
|
|
|
|
|
|
|
data['encoding'] = 'utf-8' |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
if sys.version_info[0] == 2: |
|
|
|
|
|
|
|
data['data'] = srcBody.decode( |
|
|
|
|
|
|
|
data['encoding']).encode('utf-8', errors='ignore') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
data['data'] = srcBody.decode(data['encoding']) |
|
|
|
|
|
|
|
except: |
|
|
|
|
|
|
|
data['encoding'] = char['encoding'] |
|
|
|
|
|
|
|
if sys.version_info[0] == 2: |
|
|
|
|
|
|
|
data['data'] = srcBody.decode( |
|
|
|
|
|
|
|
data['encoding']).encode('utf-8', errors='ignore') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
data['data'] = srcBody.decode(data['encoding']) |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
if sys.version_info[0] == 2: |
|
|
|
|
|
|
|
data['data'] = srcBody.decode('utf-8').encode('utf-8') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
data['data'] = srcBody.decode('utf-8') |
|
|
|
|
|
|
|
data['encoding'] = u'utf-8' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return public.returnJson(True, 'OK', data) |
|
|
|
|
|
|
|
except Exception as ex: |
|
|
|
|
|
|
|
return public.returnJson(False, u'文件编码不被兼容,无法正确读取文件!' + str(ex)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@files.route('save_body', methods=['POST']) |
|
|
|
|
|
|
|
def saveBody(): |
|
|
|
|
|
|
|
path = request.form.get('path', '').encode('utf-8') |
|
|
|
|
|
|
|
data = request.form.get('data', '').encode('utf-8') |
|
|
|
|
|
|
|
encoding = request.form.get('encoding', '').encode('utf-8') |
|
|
|
|
|
|
|
if not os.path.exists(path): |
|
|
|
|
|
|
|
return public.returnJson(False, '文件不存在') |
|
|
|
|
|
|
|
try: |
|
|
|
|
|
|
|
if encoding == 'ascii': |
|
|
|
|
|
|
|
encoding = 'utf-8' |
|
|
|
|
|
|
|
if sys.version_info[0] == 2: |
|
|
|
|
|
|
|
data = data.encode(encoding, errors='ignore') |
|
|
|
|
|
|
|
fp = open(path, 'w+') |
|
|
|
|
|
|
|
else: |
|
|
|
|
|
|
|
data = data.encode( |
|
|
|
|
|
|
|
encoding, errors='ignore').decode(encoding) |
|
|
|
|
|
|
|
fp = open(path, 'w+', encoding=encoding) |
|
|
|
|
|
|
|
fp.write(data) |
|
|
|
|
|
|
|
fp.close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public.writeLog('TYPE_FILE', '文件保存成功', (path,)) |
|
|
|
|
|
|
|
return public.returnJson(True, '文件保存成功') |
|
|
|
|
|
|
|
except Exception as ex: |
|
|
|
|
|
|
|
return public.returnJson(False, 'FILE_SAVE_ERR:' + str(ex)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@files.route('/get_dir', methods=['POST']) |
|
|
|
@files.route('/get_dir', methods=['POST']) |
|
|
|
def getDir(): |
|
|
|
def getDir(): |
|
|
|
path = request.form.get('path', '').encode('utf-8') |
|
|
|
path = request.form.get('path', '').encode('utf-8') |
|
|
|