增加遍历目录的接口
parent
031255857d
commit
511302f72d
23
inject.js
23
inject.js
|
@ -186,20 +186,33 @@ Object.assign(native, {
|
||||||
return handler('quit', {}, null)
|
return handler('quit', {}, null)
|
||||||
},
|
},
|
||||||
fs: {
|
fs: {
|
||||||
read(filepath) {
|
read(filepath, mode = 'r') {
|
||||||
return handler('fs', { action: 'read', filepath })
|
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', {
|
return handler('fs', {
|
||||||
action: 'write',
|
action: 'write',
|
||||||
|
append: false,
|
||||||
filepath,
|
filepath,
|
||||||
content,
|
content
|
||||||
append
|
})
|
||||||
|
},
|
||||||
|
append(filepath, content = '', mode = 'w') {
|
||||||
|
return handler('fs', {
|
||||||
|
action: 'write',
|
||||||
|
append: true,
|
||||||
|
filepath,
|
||||||
|
content
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
exists(filepath) {
|
exists(filepath) {
|
||||||
return handler('fs', { action: 'exists', filepath })
|
return handler('fs', { action: 'exists', filepath })
|
||||||
},
|
},
|
||||||
|
list(filepath) {
|
||||||
|
return handler('fs', { action: 'list', filepath })
|
||||||
|
},
|
||||||
isfile(filepath) {
|
isfile(filepath) {
|
||||||
return handler('fs', { action: 'isfile', filepath })
|
return handler('fs', { action: 'isfile', filepath })
|
||||||
},
|
},
|
||||||
|
|
37
main.py
37
main.py
|
@ -197,30 +197,35 @@ class WebKitWindow(Gtk.Window):
|
||||||
filepath = params.get('filepath')
|
filepath = params.get('filepath')
|
||||||
|
|
||||||
if params['action'] == 'read':
|
if params['action'] == 'read':
|
||||||
file = open(filepath)
|
with open(filepath, params.get('mode')) as file:
|
||||||
output = file.read()
|
output = file.read()
|
||||||
|
if params.get('mode').find('b') > -1:
|
||||||
|
output = list(output)
|
||||||
|
|
||||||
if params['action'] == 'write':
|
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')
|
|
||||||
|
|
||||||
output = file.write(buff)
|
# 调整以支持二进制数据写入
|
||||||
finally:
|
with open(filepath, params.get('mode')) as file:
|
||||||
if file:
|
buff = params['content']
|
||||||
file.close()
|
|
||||||
|
if params.get('mode').find('b') > -1:
|
||||||
|
buff = bytes(buff)
|
||||||
|
|
||||||
|
output = file.write(buff)
|
||||||
|
|
||||||
if params['action'] == 'exists':
|
if params['action'] == 'exists':
|
||||||
output = os.path.exists(filepath)
|
output = os.path.exists(filepath)
|
||||||
|
|
||||||
if params['action'] == 'list':
|
if params['action'] == 'list':
|
||||||
file = open(filepath)
|
with os.scandir(filepath) as entries:
|
||||||
output = file.read()
|
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 params['action'] == 'remove':
|
||||||
if os.path.isfile(filepath):
|
if os.path.isfile(filepath):
|
||||||
|
|
|
@ -53,13 +53,24 @@ class App extends Component {
|
||||||
</button>
|
</button>
|
||||||
<button
|
<button
|
||||||
@click=${async function () {
|
@click=${async function () {
|
||||||
let data = await native.fs.read('/code/gtk/webkit/demo.txt')
|
let data = await native.fs.read('/code/gtk/webkit/red.png', 'rb')
|
||||||
console.log(data)
|
console.log(data)
|
||||||
this.content = data
|
// this.content = data
|
||||||
|
this.img = URL.createObjectURL(
|
||||||
|
new Blob([data], { type: 'image/png' })
|
||||||
|
)
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
cat file
|
cat file
|
||||||
</button>
|
</button>
|
||||||
|
<button
|
||||||
|
@click=${async function () {
|
||||||
|
let data = await native.fs.list('/code/gtk/webkit/')
|
||||||
|
console.log(data)
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
list dir
|
||||||
|
</button>
|
||||||
<button
|
<button
|
||||||
@click=${async function () {
|
@click=${async function () {
|
||||||
await native.fs.write(
|
await native.fs.write(
|
||||||
|
@ -107,7 +118,7 @@ class App extends Component {
|
||||||
await native.fs.write(
|
await native.fs.write(
|
||||||
'/code/gtk/webkit/demo.png',
|
'/code/gtk/webkit/demo.png',
|
||||||
Array.from(u8.slice(i, i + 2048)),
|
Array.from(u8.slice(i, i + 2048)),
|
||||||
true
|
'wb'
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
|
|
Reference in New Issue