提交 6c576c53 编写于 作者: 杉木树下's avatar 杉木树下 提交者: Huan (李卓桓)

fix(puppet-web): send any type file. (#714)

* fix(puppet-web): send any type file.

Fix can't send pdf and more type file.
Now, you can use `m.say(new MediaMessage(file))` send any type file.

Fix #710

* fix(puppet-web): Delete unused comments

* fix(puppet-web): add interface MediaData

* fix(puppet-web): formData.id Allocation error

* fix(wechaty-bro): remove trailing comma
上级 aa39d7c0
...@@ -23,6 +23,17 @@ import { log } from '../config' ...@@ -23,6 +23,17 @@ import { log } from '../config'
import PuppetWeb from './puppet-web' import PuppetWeb from './puppet-web'
export interface MediaData {
ToUserName: string,
MsgType: number,
MediaId: string,
FileName: string,
FileSize: number,
FileMd5?: string,
MMFileId: string,
MMFileExt: string,
}
export class Bridge { export class Bridge {
constructor( constructor(
...@@ -397,15 +408,14 @@ export class Bridge { ...@@ -397,15 +408,14 @@ export class Bridge {
} }
} }
public sendMedia(toUserName: string, mediaId: string, type: number): Promise<boolean> { public sendMedia(mediaData: MediaData): Promise<boolean> {
if (!toUserName) { if (!mediaData.ToUserName) {
throw new Error('UserName not found') throw new Error('UserName not found')
} }
if (!mediaId) { if (!mediaData.MediaId) {
throw new Error('cannot say nothing') throw new Error('cannot say nothing')
} }
return this.proxyWechaty('sendMedia', mediaData)
return this.proxyWechaty('sendMedia', toUserName, mediaId, type)
.catch(e => { .catch(e => {
log.error('PuppetWebBridge', 'sendMedia() exception: %s', e.message) log.error('PuppetWebBridge', 'sendMedia() exception: %s', e.message)
throw e throw e
......
...@@ -34,7 +34,10 @@ import Puppet from '../puppet' ...@@ -34,7 +34,10 @@ import Puppet from '../puppet'
import Room from '../room' import Room from '../room'
import UtilLib from '../util-lib' import UtilLib from '../util-lib'
import Bridge from './bridge' import {
Bridge,
MediaData,
} from './bridge'
import Browser from './browser' import Browser from './browser'
import Event from './event' import Event from './event'
import Server from './server' import Server from './server'
...@@ -65,6 +68,7 @@ export class PuppetWeb extends Puppet { ...@@ -65,6 +68,7 @@ export class PuppetWeb extends Puppet {
public scan: ScanInfo | null public scan: ScanInfo | null
private port: number private port: number
private fileId: number
public lastScanEventTime: number public lastScanEventTime: number
public watchDogLastSaveSession: number public watchDogLastSaveSession: number
...@@ -73,6 +77,7 @@ export class PuppetWeb extends Puppet { ...@@ -73,6 +77,7 @@ export class PuppetWeb extends Puppet {
constructor(public setting: PuppetWebSetting = {}) { constructor(public setting: PuppetWebSetting = {}) {
super() super()
this.fileId = 0
if (!setting.head) { if (!setting.head) {
setting.head = config.head setting.head = config.head
...@@ -336,7 +341,7 @@ export class PuppetWeb extends Puppet { ...@@ -336,7 +341,7 @@ export class PuppetWeb extends Puppet {
} }
} }
private async uploadMedia(mediaMessage: MediaMessage, toUserName: string): Promise<string> { private async uploadMedia(mediaMessage: MediaMessage, toUserName: string): Promise<MediaData> {
if (!mediaMessage) if (!mediaMessage)
throw new Error('require mediaMessage') throw new Error('require mediaMessage')
...@@ -384,6 +389,8 @@ export class PuppetWeb extends Puppet { ...@@ -384,6 +389,8 @@ export class PuppetWeb extends Puppet {
const first = cookie.find(c => c.name === 'webwx_data_ticket') const first = cookie.find(c => c.name === 'webwx_data_ticket')
const webwxDataTicket = first && first.value const webwxDataTicket = first && first.value
const size = buffer.length const size = buffer.length
const id = 'WU_FILE_' + this.fileId
this.fileId++
const hostname = await this.browser.hostname() const hostname = await this.browser.hostname()
const uploadMediaRequest = { const uploadMediaRequest = {
...@@ -399,16 +406,26 @@ export class PuppetWeb extends Puppet { ...@@ -399,16 +406,26 @@ export class PuppetWeb extends Puppet {
TotalLen: size, TotalLen: size,
} }
const mediaData = <MediaData> {
ToUserName: toUserName,
MediaId: '',
FileName: filename,
FileSize: size,
FileMd5: md5,
MMFileId: id,
MMFileExt: ext,
}
const formData = { const formData = {
id: 'WU_FILE_1', id,
name: filename, name: filename,
type: contentType, type: contentType,
lastModifiedDate: Date().toString(), lastModifiedDate: Date().toString(),
size: size, size,
mediatype, mediatype,
uploadmediarequest: JSON.stringify(uploadMediaRequest), uploadmediarequest: JSON.stringify(uploadMediaRequest),
webwx_data_ticket: webwxDataTicket, webwx_data_ticket: webwxDataTicket,
pass_ticket: passTicket || '', pass_ticket: passTicket,
filename: { filename: {
value: buffer, value: buffer,
options: { options: {
...@@ -437,7 +454,7 @@ export class PuppetWeb extends Puppet { ...@@ -437,7 +454,7 @@ export class PuppetWeb extends Puppet {
}) })
if (!mediaId) if (!mediaId)
throw new Error('upload fail') throw new Error('upload fail')
return mediaId as string return Object.assign(mediaData, { MediaId: mediaId as string})
} }
public async sendMedia(message: MediaMessage): Promise<boolean> { public async sendMedia(message: MediaMessage): Promise<boolean> {
...@@ -455,17 +472,16 @@ export class PuppetWeb extends Puppet { ...@@ -455,17 +472,16 @@ export class PuppetWeb extends Puppet {
destinationId = to.id destinationId = to.id
} }
const mediaId = await this.uploadMedia(message, destinationId) const mediaData = await this.uploadMedia(message, destinationId)
const msgType = UtilLib.msgType(message.ext()) mediaData.MsgType = UtilLib.msgType(message.ext())
log.silly('PuppetWeb', 'send() destination: %s, mediaId: %s)', log.silly('PuppetWeb', 'send() destination: %s, mediaId: %s)',
destinationId, destinationId,
mediaId, mediaData.MediaId,
) )
let ret = false let ret = false
try { try {
ret = await this.bridge.sendMedia(destinationId, mediaId, msgType) ret = await this.bridge.sendMedia(mediaData)
} catch (e) { } catch (e) {
log.error('PuppetWeb', 'send() exception: %s', e.message) log.error('PuppetWeb', 'send() exception: %s', e.message)
Raven.captureException(e) Raven.captureException(e)
......
...@@ -475,7 +475,7 @@ ...@@ -475,7 +475,7 @@
return confFactory.API_webwxuploadmedia return confFactory.API_webwxuploadmedia
} }
function sendMedia(ToUserName, MediaId,Type) { function sendMedia(data) {
var chatFactory = WechatyBro.glue.chatFactory var chatFactory = WechatyBro.glue.chatFactory
var confFactory = WechatyBro.glue.confFactory var confFactory = WechatyBro.glue.confFactory
...@@ -485,11 +485,17 @@ ...@@ -485,11 +485,17 @@
} }
try { try {
var m = chatFactory.createMessage({ var d = {
ToUserName: ToUserName ToUserName: data.ToUserName,
, MediaId: MediaId MediaId: data.MediaId,
, MsgType: Type MsgType: data.MsgType,
}) FileName: data.FileName,
FileSize: data.FileSize,
MMFileExt: data.MMFileExt,
MMFileId: data.MMFileId
}
var m = chatFactory.createMessage(d)
chatFactory.appendMessage(m) chatFactory.appendMessage(m)
chatFactory.sendMessage(m) chatFactory.sendMessage(m)
} catch (e) { } catch (e) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册