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 { +