message-media.ts 2.6 KB
Newer Older
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
1 2 3 4 5 6 7 8
/**
 *
 * wechaty: Wechat for Bot. and for human who talk to bot/robot
 *
 * Licenst: ISC
 * https://github.com/zixia/wechaty
 *
 */
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
9 10 11 12 13 14 15 16
import {
    Config
  , log
}                     from './config'
import { Message }    from './message'
import { UtilLib }    from './util-lib'
import { PuppetWeb }  from './puppet-web/puppet-web'
import { Bridge }     from './puppet-web/bridge'
17

18
export class MediaMessage extends Message {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
19
  private bridge: Bridge
20

Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
21 22
  constructor(rawObj) {
    super(rawObj)
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
23
    // FIXME: decoupling needed
24
    this.bridge = (Config.puppetInstance() as PuppetWeb)
25
                        .bridge
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
26
  }
27 28

  public async ready(): Promise<void> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
29 30
    log.silly('MediaMessage', 'ready()')

31
    try {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
      await super.ready()

      let url: string
      switch (this.type()) {
        case Message.TYPE['EMOTICON']:
          url = await this.bridge.getMsgEmoticon(this.id)
          break
        case Message.TYPE['IMAGE']:
          url = await this.bridge.getMsgImg(this.id)
          break
        case Message.TYPE['VIDEO']:
        case Message.TYPE['MICROVIDEO']:
          url = await this.bridge.getMsgVideo(this.id)
          break
        case Message.TYPE['VOICE']:
          url = await this.bridge.getMsgVoice(this.id)
          break
        default:
          throw new Error('not support message type for MediaMessage')
      }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
52 53
      this.obj.url = url

54 55
      // return this // IMPORTANT!

56
    } catch (e) {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
57 58
      log.warn('MediaMessage', 'ready() exception: %s', e.message)
      throw e
59
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
60
  }
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  public ext(): string {
    switch (this.type()) {
      case Message.TYPE['EMOTICON']:
        return '.gif'

      case Message.TYPE['IMAGE']:
        return '.jpg'

      case Message.TYPE['VIDEO']:
      case Message.TYPE['MICROVIDEO']:
        return '.mp4'

      case Message.TYPE['VOICE']:
        return '.mp3'

      default:
        throw new Error('not support type: ' + this.type())
    }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
80 81
  }

82 83 84 85 86 87 88 89
  // private getMsgImg(id: string): Promise<string> {
  //   return this.bridge.getMsgImg(id)
  //   .catch(e => {
  //     log.warn('MediaMessage', 'getMsgImg(%d) exception: %s', id, e.message)
  //     throw e
  //   })
  // }

90
  public readyStream(): Promise<NodeJS.ReadableStream> {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
91 92
    return this.ready()
    .then(() => {
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
93
      // FIXME: decoupling needed
94
      return (Config.puppetInstance() as PuppetWeb)
95
                    .browser.readCookie()
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
96 97
    })
    .then(cookies => {
98 99 100
      if (!this.obj.url) {
        throw new Error('no url')
      }
Huan (李卓桓)'s avatar
Huan (李卓桓) 已提交
101 102 103 104 105 106 107 108
      return UtilLib.downloadStream(this.obj.url, cookies)
    })
    .catch(e => {
      log.warn('MediaMessage', 'stream() exception: %s', e.message)
      throw e
    })
  }
}