-
Notifications
You must be signed in to change notification settings - Fork 73
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e47e75f
Update web-schemas.ts
su-chang 6bc7a25
Update puppet-puppeteer.ts
su-chang 35571e5
Update puppet-puppeteer.ts
su-chang 37d31ab
Update puppet-puppeteer.ts
su-chang c83e595
Update puppet-puppeteer.ts
su-chang 9210440
Update puppet-puppeteer.ts
su-chang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we need to catch the error and log it here. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done