From 511302f72da8984361392b3c837e974574a9a10a Mon Sep 17 00:00:00 2001 From: yutent Date: Tue, 1 Aug 2023 15:25:13 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=81=8D=E5=8E=86=E7=9B=AE?= =?UTF-8?q?=E5=BD=95=E7=9A=84=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inject.js | 23 ++++++++++++++++++----- main.py | 35 ++++++++++++++++++++--------------- webview/app.js | 17 ++++++++++++++--- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/inject.js b/inject.js index ae4bf10..29a87ac 100644 --- a/inject.js +++ b/inject.js @@ -186,20 +186,33 @@ Object.assign(native, { return handler('quit', {}, null) }, fs: { - read(filepath) { - return handler('fs', { action: 'read', filepath }) + read(filepath, mode = 'r') { + return handler('fs', { action: 'read', mode, filepath }).then(r => + mode.includes('b') ? new Uint8Array(r) : r + ) }, - write(filepath, content = '', append = false) { + write(filepath, content = '', mode = 'w') { return handler('fs', { action: 'write', + append: false, filepath, - content, - append + content + }) + }, + append(filepath, content = '', mode = 'w') { + return handler('fs', { + action: 'write', + append: true, + filepath, + content }) }, exists(filepath) { return handler('fs', { action: 'exists', filepath }) }, + list(filepath) { + return handler('fs', { action: 'list', filepath }) + }, isfile(filepath) { return handler('fs', { action: 'isfile', filepath }) }, diff --git a/main.py b/main.py index 2e397e8..be88d50 100755 --- a/main.py +++ b/main.py @@ -197,30 +197,35 @@ class WebKitWindow(Gtk.Window): filepath = params.get('filepath') if params['action'] == 'read': - file = open(filepath) - output = file.read() + with open(filepath, params.get('mode')) as file: + output = file.read() + if params.get('mode').find('b') > -1: + output = list(output) if params['action'] == 'write': - try: + # 调整以支持二进制数据写入 - buff = params['content'] - if type(buff) == list: - buff = bytes(buff) - file = open(filepath, 'ab' if params['append'] else 'wb') - else: - file = open(filepath, 'a' if params['append'] else 'w') + with open(filepath, params.get('mode')) as file: + buff = params['content'] - output = file.write(buff) - finally: - if file: - file.close() + if params.get('mode').find('b') > -1: + buff = bytes(buff) + + output = file.write(buff) if params['action'] == 'exists': output = os.path.exists(filepath) if params['action'] == 'list': - file = open(filepath) - output = file.read() + with os.scandir(filepath) as entries: + output = [{ + "name": it.name, + "path": os.path.join(filepath, it.name), + "is_dir": it.is_dir(), + "size": it.stat().st_size, + "atime": int(it.stat().st_atime), + "mtime": int(it.stat().st_mtime), + } for it in entries] if params['action'] == 'remove': if os.path.isfile(filepath): diff --git a/webview/app.js b/webview/app.js index 6261834..b8f9b99 100644 --- a/webview/app.js +++ b/webview/app.js @@ -53,13 +53,24 @@ class App extends Component { +