如何获取含PDF Buffer的数组按MB限制拆分的次数?
获取PDF数组的拆分次数方法
场景说明
现有一个存储PDF信息的Buffer数组,元素结构示例如下:
[{ correlative: "G-22-1-06", content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 72 6d 7 6d 6 509 64 ... 277154 more bytes>, name_Patient: "NamePatient" }, { correlative: "G-22-1-07", content: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 d3 eb e9 e1 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 43 72 65 61 74 6f 72 20 28 43 68 72 6f 72 6d 7 6d 6 509 64 ... 275969 more bytes>, name_Patient: "NamePatient" }]
当数组总大小超过设定的MB限制时会被拆分(比如20个PDF按每2个一组拆分),需要获取该数组的拆分次数。
现有代码片段(中文翻译)
// 计算数组中所有文件的总大小(单位:MB) const totalSize: number = this.getSizeFromAttachments(attachments); // 根据设定的总限制计算每组的文件数量 const chunkSplit = Math.floor(isNaN(totalSize) ? 1 : totalSize / this.LIMIT_ATTACHMENTS) + 1; // 如果总大小超过限制,拆分数组 const attachmentsChunk: any[][] = _.chunk(attachments, chunkSplit); // 遍历拆分后的数组 attachmentsChunk?.map(async (attachment: EmailAttachment[], index) => { // 执行后续操作 });
获取拆分次数的方法
拆分次数就是拆分后数组的长度,直接通过attachmentsChunk.length获取即可。
比如示例中20个PDF按每2个一组拆分,拆分后的数组长度是10,对应拆分次数为10。若原数组总大小未超过限制,attachmentsChunk长度为1,代表无需拆分,或视为1次完整组的处理。
内容的提问来源于stack exchange,提问作者Izlia
相关产品推荐
相关产品推荐

