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