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

Update api_material.js #136

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
21 changes: 14 additions & 7 deletions lib/api_material.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ const { postJSON } = require('./util');
* Shortcut: * - `exports.uploadImageMaterial(filepath);`
* - `exports.uploadVoiceMaterial(filepath);`
* - `exports.uploadThumbMaterial(filepath);` * @param {String} filepath 文件路径
* @param {String} type 媒体类型,可用值有image、voice、video、thumb
* @param {String|Buffer} filepath 文件路径/文件Buffer数据
* @param {String} type 媒体类型,可用值有image、voice、thumb
* @param {String} filename 文件名
* @param {String} mime 文件类型,filepath为Buffer数据时才需要传
*/
exports.uploadMaterial = async function (filepath, type) {
exports.uploadMaterial = async function (filepath, type, filename, mime) {
const { accessToken } = await this.ensureAccessToken();
var stat = await statAsync(filepath);
var form = formstream();
form.file('media', filepath, path.basename(filepath), stat.size);
if (Buffer.isBuffer(filepath)) {
form.buffer('media', filepath, filename, mime);
} else if (typeof filepath === 'string') {
var stat = await statAsync(filepath);
form.file('media', filepath, filename || path.basename(filepath), stat.size);
}
var url = this.prefix + 'material/add_material?access_token=' + accessToken + '&type=' + type;
var opts = {
dataType: 'json',
Expand All @@ -42,11 +49,11 @@ exports.uploadMaterial = async function (filepath, type) {
};
return this.request(url, opts);
};

['image', 'voice', 'thumb'].forEach(function (type) {
var method = 'upload' + type[0].toUpperCase() + type.substring(1) + 'Material';
exports[method] = async function (filepath) {
return this.uploadMaterial(filepath, type);
exports[method] = async function (filepath, filename, mime) {
return this.uploadMaterial(filepath, type, filename, mime);
};
});

Expand Down