Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that can't send file. #106

Merged
merged 6 commits into from
Jul 5, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 79 additions & 41 deletions src/puppet-puppeteer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1427,51 +1427,89 @@ export class PuppetPuppeteer extends Puppet {
log.verbose('PuppetPuppeteer', 'uploadMedia() webwx_data_ticket: %s', webwxDataTicket)
log.verbose('PuppetPuppeteer', 'uploadMedia() pass_ticket: %s', passTicket)

const formData = {
filename: {
options: {
contentType,
filename,
size,
/**
* If FILE.SIZE > 1M, file buffer need to split for upload.
* Split strategy:
* BASE_LENGTH: 512 * 1024
* chunks: split number
* chunk: the index of chunks
*/
const BASE_LENGTH = 512 * 1024
const chunks = Math.ceil(buffer.length / BASE_LENGTH)

const bufferData = []
for (let i = 0; i < chunks; i++) {
let tempBuffer
if (i === chunks - 1) {
tempBuffer = buffer.slice(i * BASE_LENGTH)
} else {
tempBuffer = buffer.slice(i * BASE_LENGTH, (i + 1) * BASE_LENGTH)
}
bufferData.push(tempBuffer)
}

async function getMediaId (buffer: Buffer, index: number) : Promise <string> {
const formData = {
chunk: index,
chunks,
filename: {
options: {
contentType,
filename,
size,
},
value: buffer,
},
value: buffer,
},
id,
lastModifiedDate: Date().toString(),
mediatype,
name: filename,
pass_ticket: passTicket || '',
size,
type: contentType,
uploadmediarequest: JSON.stringify(uploadMediaRequest),
webwx_data_ticket: webwxDataTicket,
id,
lastModifiedDate: Date().toString(),
mediatype,
name: filename,
pass_ticket: passTicket || '',
size,
type: contentType,
uploadmediarequest: JSON.stringify(uploadMediaRequest),
webwx_data_ticket: webwxDataTicket,
}
try {
return await new Promise<string>((resolve, reject) => {
try {
request.post({
formData,
headers,
url: uploadMediaUrl + '?f=json',
}, (err, _, body) => {
if (err) {
reject(err)
} else {
let obj = body
if (typeof body !== 'object') {
obj = JSON.parse(body)
}
resolve(obj.MediaId || '')
}
})
} catch (e) {
reject(e)
}
})
} catch (e) {
log.error('PuppetPuppeteer', 'uploadMedia() uploadMedia exception: %s', e.message)
throw new Error('uploadMedia err: ' + e.message)
}
}
let funcList = []
for (let i = 0; i < bufferData.length - 1; i++) {
funcList.push(await getMediaId(bufferData[i], i))
}
// use promise.all() make the former upload of this buffer quickly
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could do something like this here to get the last one:

const lastBuffer = bufferData.pop()
await Promise.all(bufferData.map(getMediaId))
const mediaId = await getMediaId(lastBuffer, bufferData.length)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

let mediaId: string
try {
mediaId = await new Promise<string>((resolve, reject) => {
try {
request.post({
formData,
headers,
url: uploadMediaUrl + '?f=json',
}, (err, _, body) => {
if (err) {
reject(err)
} else {
let obj = body
if (typeof body !== 'object') {
obj = JSON.parse(body)
}
resolve(obj.MediaId || '')
}
})
} catch (e) {
reject(e)
}
})
} catch (e) {
log.error('PuppetPuppeteer', 'uploadMedia() uploadMedia exception: %s', e.message)
throw new Error('uploadMedia err: ' + e.message)
await Promise.all(funcList)
const lastOne = bufferData.length - 1
mediaId = await getMediaId(bufferData[lastOne], lastOne)
} catch {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to catch the error and log it here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

log.error('PuppetPuppeteer', 'uploadMedia(): upload fail')
throw new Error('PuppetPuppeteer.uploadMedia(): upload fail')
}
if (!mediaId) {
log.error('PuppetPuppeteer', 'uploadMedia(): upload fail')
Expand Down
8 changes: 4 additions & 4 deletions src/web-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,10 +275,10 @@ export interface WebRecomendInfo {
}

export const enum WebMediaType {
Image = 1,
Video = 2,
Audio = 3,
Attachment = 4,
Image = 'pic',
Video = 'video',
Attachment = 'doc',
// Audio = 3, useless now
}

export interface WebRoomRawMember {
Expand Down