From e47e75fb6811cd0d0453caf3ee6886729d44f53b Mon Sep 17 00:00:00 2001 From: SuperChang Date: Thu, 4 Jul 2019 17:56:11 +0800 Subject: [PATCH 1/6] Update web-schemas.ts Change WebMediaType 1 -> pic 2 -> video 4 -> doc and deprecate Audio. --- src/web-schemas.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/web-schemas.ts b/src/web-schemas.ts index 3ca48b3..21f6c95 100644 --- a/src/web-schemas.ts +++ b/src/web-schemas.ts @@ -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 { From 6bc7a25a65f8b478c8d50792c4c49b01364a5900 Mon Sep 17 00:00:00 2001 From: SuperChang Date: Thu, 4 Jul 2019 18:13:05 +0800 Subject: [PATCH 2/6] Update puppet-puppeteer.ts Fix the bug If the file.size > 1M, the uploadMedia() function will run error. Split the buffer, and then uploadFile one by one. --- src/puppet-puppeteer.ts | 118 +++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 43 deletions(-) diff --git a/src/puppet-puppeteer.ts b/src/puppet-puppeteer.ts index 23212e7..fca1b2e 100644 --- a/src/puppet-puppeteer.ts +++ b/src/puppet-puppeteer.ts @@ -1427,52 +1427,84 @@ 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, - }, - value: buffer, - }, - id, - lastModifiedDate: Date().toString(), - mediatype, - name: filename, - pass_ticket: passTicket || '', - size, - type: contentType, - uploadmediarequest: JSON.stringify(uploadMediaRequest), - webwx_data_ticket: webwxDataTicket, + /** + * 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); } - let mediaId: string - try { - mediaId = await new Promise((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) + + async function getMediaId(buffer: Buffer, index: number) : Promise { + const formData = { + filename: { + options: { + contentType, + filename, + size, + }, + value: buffer, + }, + chunks, + chunk: index, + 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((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 || '') } - 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) + }) + } 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 + Promise.all(funcList) + const lastOne = bufferData.length - 1; + let mediaId = await getMediaId(bufferData[lastOne], lastOne) if (!mediaId) { log.error('PuppetPuppeteer', 'uploadMedia(): upload fail') throw new Error('PuppetPuppeteer.uploadMedia(): upload fail') From 35571e5932b741904c44325cc9141ab27b769286 Mon Sep 17 00:00:00 2001 From: SuperChang Date: Thu, 4 Jul 2019 18:50:59 +0800 Subject: [PATCH 3/6] Update puppet-puppeteer.ts apply to ESLint --- src/puppet-puppeteer.ts | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/puppet-puppeteer.ts b/src/puppet-puppeteer.ts index fca1b2e..a92bc7d 100644 --- a/src/puppet-puppeteer.ts +++ b/src/puppet-puppeteer.ts @@ -1435,21 +1435,23 @@ export class PuppetPuppeteer extends Puppet { * chunk: the index of chunks */ const BASE_LENGTH = 512 * 1024 - const chunks = Math.ceil(buffer.length / BASE_LENGTH); + const chunks = Math.ceil(buffer.length / BASE_LENGTH) - const bufferData = []; - for(let i = 0; i < chunks; i++) { + const bufferData = [] + for (let i = 0; i < chunks; i++) { let tempBuffer - if(i === chunks - 1) { + if (i === chunks - 1) { tempBuffer = buffer.slice(i * BASE_LENGTH) } else { tempBuffer = buffer.slice(i * BASE_LENGTH, (i + 1) * BASE_LENGTH) } - bufferData.push(tempBuffer); + bufferData.push(tempBuffer) } - async function getMediaId(buffer: Buffer, index: number) : Promise { + async function getMediaId (buffer: Buffer, index: number) : Promise { const formData = { + chunk: index, + chunks, filename: { options: { contentType, @@ -1458,8 +1460,6 @@ export class PuppetPuppeteer extends Puppet { }, value: buffer, }, - chunks, - chunk: index, id, lastModifiedDate: Date().toString(), mediatype, @@ -1498,13 +1498,19 @@ export class PuppetPuppeteer extends Puppet { } } let funcList = [] - for(let i = 0; i < bufferData.length - 1; i++) { + 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 - Promise.all(funcList) - const lastOne = bufferData.length - 1; - let mediaId = await getMediaId(bufferData[lastOne], lastOne) + let mediaId: string + try { + await Promise.all(funcList) + const lastOne = bufferData.length - 1 + mediaId = await getMediaId(bufferData[lastOne], lastOne) + } catch { + log.error('PuppetPuppeteer', 'uploadMedia(): upload fail') + throw new Error('PuppetPuppeteer.uploadMedia(): upload fail') + } if (!mediaId) { log.error('PuppetPuppeteer', 'uploadMedia(): upload fail') throw new Error('PuppetPuppeteer.uploadMedia(): upload fail') From 37d31abd929053ef91ada416ce6e10280306754d Mon Sep 17 00:00:00 2001 From: SuperChang Date: Thu, 4 Jul 2019 20:12:14 +0800 Subject: [PATCH 4/6] Update puppet-puppeteer.ts Simplify the logic code. --- src/puppet-puppeteer.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/puppet-puppeteer.ts b/src/puppet-puppeteer.ts index a92bc7d..af7f9d9 100644 --- a/src/puppet-puppeteer.ts +++ b/src/puppet-puppeteer.ts @@ -1439,12 +1439,7 @@ export class PuppetPuppeteer extends Puppet { 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) - } + let tempBuffer = buffer.slice(i * BASE_LENGTH, (i + 1) * BASE_LENGTH) bufferData.push(tempBuffer) } From c83e595d9691d699c53bbc69f31fe8d970110be3 Mon Sep 17 00:00:00 2001 From: SuperChang Date: Thu, 4 Jul 2019 20:43:29 +0800 Subject: [PATCH 5/6] Update puppet-puppeteer.ts 1. graceful code 2. catch error log --- src/puppet-puppeteer.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/puppet-puppeteer.ts b/src/puppet-puppeteer.ts index af7f9d9..85f7d2a 100644 --- a/src/puppet-puppeteer.ts +++ b/src/puppet-puppeteer.ts @@ -1499,11 +1499,11 @@ export class PuppetPuppeteer extends Puppet { // use promise.all() make the former upload of this buffer quickly let mediaId: string try { - await Promise.all(funcList) - const lastOne = bufferData.length - 1 - mediaId = await getMediaId(bufferData[lastOne], lastOne) - } catch { - log.error('PuppetPuppeteer', 'uploadMedia(): upload fail') + const lastBuffer = bufferData.pop() + await Promise.all(bufferData.map(getMediaId)) + mediaId = await getMediaId(lastBuffer, bufferData.length) + } catch (e) { + log.error('PuppetPuppeteer', 'uploadMedia exception: %s', e.message) throw new Error('PuppetPuppeteer.uploadMedia(): upload fail') } if (!mediaId) { From 92104404289fa7278aeaf1a5a75cf089919ddda3 Mon Sep 17 00:00:00 2001 From: SuperChang Date: Thu, 4 Jul 2019 21:42:03 +0800 Subject: [PATCH 6/6] Update puppet-puppeteer.ts upload media by order --- src/puppet-puppeteer.ts | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/puppet-puppeteer.ts b/src/puppet-puppeteer.ts index 85f7d2a..845d40e 100644 --- a/src/puppet-puppeteer.ts +++ b/src/puppet-puppeteer.ts @@ -1492,19 +1492,9 @@ export class PuppetPuppeteer extends Puppet { 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 { - const lastBuffer = bufferData.pop() - await Promise.all(bufferData.map(getMediaId)) - mediaId = await getMediaId(lastBuffer, bufferData.length) - } catch (e) { - log.error('PuppetPuppeteer', 'uploadMedia exception: %s', e.message) - throw new Error('PuppetPuppeteer.uploadMedia(): upload fail') + let mediaId = '' + for (let i = 0; i < bufferData.length; i++) { + mediaId = await getMediaId(bufferData[i], i) } if (!mediaId) { log.error('PuppetPuppeteer', 'uploadMedia(): upload fail')