From a978c494c40513627e7e7f709a3c217f9c0daf9f Mon Sep 17 00:00:00 2001 From: Mr Chen Date: Wed, 23 Oct 2024 15:40:14 +0800 Subject: [PATCH] update --- web/admin/files/__init__.py | 66 ++++++++++++++++++++++++++++++++++++- web/core/mw.py | 53 +++++++++++++++++++++++++++++ 2 files changed, 118 insertions(+), 1 deletion(-) diff --git a/web/admin/files/__init__.py b/web/admin/files/__init__.py index 4b23164e9..019d368cc 100644 --- a/web/admin/files/__init__.py +++ b/web/admin/files/__init__.py @@ -8,10 +8,74 @@ # Author: midoks # --------------------------------------------------------------------------------- +import os from flask import Blueprint, render_template +from flask import request + +import core.mw as mw blueprint = Blueprint('files', __name__, url_prefix='/files', template_folder='../../templates/default') @blueprint.route('/index', endpoint='index') def index(): - return render_template('files.html', data={}) \ No newline at end of file + return render_template('files.html', data={}) + +@blueprint.route('/get_body', endpoint='getFileBody', methods=['POST']) +def getFileBody(): + path = request.form.get('path', '') + + if not os.path.exists(path): + return mw.returnData(False, '文件不存在', (path,)) + + if os.path.getsize(path) > 2097152: + return mw.returnData(False, '不能在线编辑大于2MB的文件!') + + if os.path.isdir(path): + return mw.returnData(False, '这不是一个文件!') + + fp = open(path, 'rb') + data = {} + data['status'] = True + if fp: + srcBody = fp.read() + fp.close() + + encoding_list = ['utf-8', 'GBK', 'BIG5'] + for el in encoding_list: + try: + data['encoding'] = el + data['data'] = srcBody.decode(data['encoding']) + break + except Exception as ex: + if el == 'BIG5': + return mw.returnData(False, '文件编码不被兼容,无法正确读取文件!' + str(ex)) + else: + return mw.returnData(False, '文件未正常打开!') + return mw.returnData(True, 'OK', data) + +@blueprint.route('/get_last_body', endpoint='getFileLastBody', methods=['POST']) +def getFileLastBody(): + path = request.form.get('path', '') + line = request.form.get('line', '100') + + if not os.path.exists(path): + return mw.returnData(False, '文件不存在', (path,)) + + try: + data = mw.getLastLine(path, int(line)) + return mw.returnData(True, 'OK', data) + except Exception as ex: + return mw.returnData(False, '无法正确读取文件!' + str(ex)) + + + + + + + + + + + + + diff --git a/web/core/mw.py b/web/core/mw.py index 2bb2b0332..9960b3673 100644 --- a/web/core/mw.py +++ b/web/core/mw.py @@ -212,6 +212,59 @@ def getSqitePrefix(): prefix = 'sqlite:////' return prefix +def getLastLine(path, num, p=1): + pyVersion = sys.version_info[0] + try: + import html + if not os.path.exists(path): + return "" + start_line = (p - 1) * num + count = start_line + num + fp = open(path, 'rb') + buf = "" + + fp.seek(0, 2) + if fp.read(1) == "\n": + fp.seek(0, 2) + data = [] + b = True + n = 0 + + for i in range(count): + while True: + newline_pos = str.rfind(str(buf), "\n") + pos = fp.tell() + if newline_pos != -1: + if n >= start_line: + line = buf[newline_pos + 1:] + try: + data.insert(0, html.escape(line)) + except Exception as e: + pass + buf = buf[:newline_pos] + n += 1 + break + else: + if pos == 0: + b = False + break + to_read = min(4096, pos) + fp.seek(-to_read, 1) + t_buf = fp.read(to_read) + if pyVersion == 3: + if type(t_buf) == bytes: + t_buf = t_buf.decode("utf-8", "ignore").strip() + buf = t_buf + buf + fp.seek(-to_read, 1) + if pos - to_read == 0: + buf = "\n" + buf + if not b: + break + fp.close() + except Exception as e: + return str(e) + + return "\n".join(data) def getPage(args, result='1,2,3,4,5,8'): data = getPageObject(args, result)