Skip to content

Commit

Permalink
export unzip
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed Dec 22, 2024
1 parent 6ce4a7e commit 21b1de5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
1 change: 1 addition & 0 deletions page/Fcitx5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export interface FCITX {
activateMenuAction: (id: number) => void
installPlugin: (buffer: ArrayBuffer) => string
getInstalledPlugins: () => string[]
unzip: (buffer: ArrayBuffer, dir: string) => void
Module: EM_MODULE
}

Expand Down
3 changes: 2 additions & 1 deletion page/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { blur, clickPanel, focus } from './focus'
import { currentInputMethod, getAllInputMethods, getInputMethods, setCurrentInputMethod, setInputMethods } from './input-method'
import { jsKeyToFcitxString, keyEvent } from './keycode'
import Module from './module'
import { getInstalledPlugins, installPlugin } from './plugin'
import { getInstalledPlugins, installPlugin, unzip } from './plugin'

let res: (value: any) => void

Expand Down Expand Up @@ -52,6 +52,7 @@ window.fcitx = {
activateMenuAction,
installPlugin,
getInstalledPlugins,
unzip,
enable() {
Module.ccall('init', 'void', [], [])
document.addEventListener('focus', focus, true)
Expand Down
33 changes: 23 additions & 10 deletions page/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,29 @@ function addDefaultIMs(byteArray: Uint8Array) {
window.fcitx.setInputMethods([...currentInputMethods, ...inputMethods.filter(im => !currentInputMethods.includes(im))])
}

export function installPlugin(buffer: ArrayBuffer) {
function distributeFiles(manifest: UZIP.UZIPFiles, dir: string) {
Object.entries(manifest).forEach(([path, data]) => {
const absolutePath = `${dir}/${path}`
if (path.endsWith('/')) {
mkdirP(absolutePath)
}
else {
Module.FS.writeFile(absolutePath, data)
}
})
}

export function unzip(buffer: ArrayBuffer, dir: string) {
// UZIP hangs on empty zip.
if (!buffer.byteLength) {
return
}
const manifest = UZIP.parse(buffer)
distributeFiles(manifest, dir)
}

// Like unzip, but do some sanity checks for plugins.
export function installPlugin(buffer: ArrayBuffer) {
if (buffer.byteLength < 1024) {
throw new Error('Invalid plugin')
}
Expand All @@ -29,15 +50,7 @@ export function installPlugin(buffer: ArrayBuffer) {
if (!byteArray) {
throw new Error('Invalid plugin')
}
Object.entries(manifest).forEach(([path, data]) => {
const absolutePath = `/usr/${path}`
if (path.endsWith('/')) {
mkdirP(absolutePath)
}
else {
Module.FS.writeFile(absolutePath, data)
}
})
distributeFiles(manifest, '/usr')
reload()
addDefaultIMs(byteArray)
return name
Expand Down

0 comments on commit 21b1de5

Please sign in to comment.