From: Shuanglei Tao Date: Sun, 7 Jul 2019 11:44:01 +0000 (+0800) Subject: html: polish zmodem code X-Git-Url: http://git.prime8.dev/?a=commitdiff_plain;h=218b14943d1e788b8500b748c71fa3b1c06583a4;p=ttyd.git html: polish zmodem code --- diff --git a/html/src/components/zmodem/index.tsx b/html/src/components/zmodem/index.tsx index 6a931c0..8105970 100644 --- a/html/src/components/zmodem/index.tsx +++ b/html/src/components/zmodem/index.tsx @@ -53,7 +53,7 @@ export class ZmodemAddon extends Component implements ITerminalAdd try { sentry.consume(data); } catch (e) { - console.log(`[ttyd] zmodem consume: `, e); + console.error(`[ttyd] zmodem consume: `, e); terminal.setOption('disableStdin', false); } } @@ -79,6 +79,10 @@ export class ZmodemAddon extends Component implements ITerminalAdd terminal.setOption('disableStdin', true); this.session = detection.confirm(); + this.session.on('session_end', () => { + terminal.setOption('disableStdin', false); + }); + if (this.session.type === 'send') { this.setState({ modal: true }); } else { @@ -90,51 +94,51 @@ export class ZmodemAddon extends Component implements ITerminalAdd private sendFile(event: Event) { this.setState({ modal: false }); - const { terminal, session, writeProgress } = this; + const { session, writeProgress } = this; const files: FileList = (event.target as HTMLInputElement).files; Zmodem.Browser.send_files(session, files, { - on_progress: (_, xfer: any) => writeProgress(xfer), + on_progress: (_, offer: Zmodem.Offer) => writeProgress(offer), }) .then(() => { session.close(); - terminal.setOption('disableStdin', false); }) .catch(e => { - console.log(`[ttyd] zmodem send: `, e); + console.error(`[ttyd] zmodem send: `, e); }); } @bind private receiveFile() { - const { terminal, session, writeProgress } = this; + const { session, writeProgress } = this; - session.on('offer', (xfer: any) => { + session.on('offer', (offer: Zmodem.Offer) => { const fileBuffer = []; - xfer.on('input', payload => { - writeProgress(xfer); + offer.on('input', payload => { + writeProgress(offer); fileBuffer.push(new Uint8Array(payload)); }); - xfer.accept().then(() => { - Zmodem.Browser.save_to_disk(fileBuffer, xfer.get_details().name); - }); - }); - - session.on('session_end', () => { - terminal.setOption('disableStdin', false); + offer + .accept() + .then(() => { + Zmodem.Browser.save_to_disk(fileBuffer, offer.get_details().name); + }) + .catch(e => { + console.error(`[ttyd] zmodem receive: `, e); + }); }); session.start(); } @bind - private writeProgress(xfer: any) { + private writeProgress(offer: Zmodem.Offer) { const { terminal, bytesHuman } = this; - const file = xfer.get_details(); + const file = offer.get_details(); const name = file.name; const size = file.size; - const offset = xfer.get_offset(); + const offset = offer.get_offset(); const percent = ((100 * offset) / size).toFixed(2); terminal.write(`${name} ${percent}% ${bytesHuman(offset, 2)}/${bytesHuman(size, 2)}\r`); diff --git a/src/index.html b/src/index.html index 49a5b4d..2ae309a 100644 --- a/src/index.html +++ b/src/index.html @@ -1 +1 @@ -ttyd - Terminal \ No newline at end of file +ttyd - Terminal \ No newline at end of file