提交 c493cfca 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

code clean

上级 6dd4446e
......@@ -20,6 +20,7 @@
// import { inspect } from 'util'
import {
createWriteStream,
statSync,
// writeFileSync,
} from 'fs'
......@@ -33,7 +34,7 @@ import * as qrcodeTerminal from 'qrcode-terminal'
*/
import {
config,
Message,
MediaMessage,
MsgType,
Wechaty,
} from '../'
......@@ -62,13 +63,15 @@ bot
|| m.type() === MsgType.APP
|| (m.type() === MsgType.TEXT && m.typeSub() === MsgType.LOCATION) // LOCATION
) {
saveMediaFile(m)
if (m instanceof MediaMessage) {
saveMediaFile(m)
}
}
})
.init()
.catch(e => console.error('bot.init() error: ' + e))
async function saveMediaFile(message: Message) {
async function saveMediaFile(message: MediaMessage) {
const filename = message.filename()
console.log('IMAGE local filename: ' + filename)
......@@ -79,7 +82,10 @@ async function saveMediaFile(message: Message) {
const netStream = await message.readyStream()
netStream
.pipe(fileStream)
.on('close', _ => console.log('finish readyStream()'))
.on('close', _ => {
const stat = statSync(filename)
console.log('finish readyStream() for ', filename, ' size: ', stat.size)
})
} catch (e) {
console.error('stream error:', e)
}
......
......@@ -1093,7 +1093,7 @@ export class MediaMessage extends Message {
// }
/**
* @private
* Get the read stream for attachment file
*/
public async readyStream(): Promise<Readable> {
if (this.filePath)
......@@ -1106,6 +1106,7 @@ export class MediaMessage extends Message {
if (!this.obj.url) {
throw new Error('no url')
}
log.verbose('MediaMessage', 'stream() url: %s', this.obj.url)
return UtilLib.urlStream(this.obj.url, cookies)
} catch (e) {
log.warn('MediaMessage', 'stream() exception: %s', e.stack)
......
......@@ -113,7 +113,7 @@ export class UtilLib {
href = href.replace(/^https/i, 'http') // use http instead of https, because https will only success on the very first request!
const u = url.parse(href)
const protocol: 'https:'|'http:' = u.protocol as any
const protocol = u.protocol as 'https:'|'http:'
let options
// let request
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册