node-webot / co-wechat-api

Wechat API. Support Async Functions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

uploadImageMedia(filepath) 方法如果filepath传递的是buffer类型,调用该快捷方法则少了filename参数

xuwenliu opened this issue · comments

uploadImageMedia(filepath)
uploadVoiceMedia(filepath)
uploadVideoMedia(filepath)
uploadThumbMedia(filepath)

以上4个快捷方法少了filename参数

看了源码:

exports.uploadMedia = async function (filepath, type, filename, mime) {
  const { accessToken } = await this.ensureAccessToken();
  var form = formstream();
  if (Buffer.isBuffer(filepath)) {
    form.buffer('media', filepath, filename, mime);     // 需要filename这个参数
  } else if (typeof filepath === 'string') {
    var stat = await statAsync(filepath);
    form.file('media', filepath, filename || path.basename(filepath), stat.size);
  }
  var url = this.prefix + 'media/upload?access_token=' + accessToken + '&type=' + type;
  var opts = {
    method: 'POST',
    timeout: 60000, // 60秒超时
    headers: form.headers(),
    data: form
  };
  opts.headers.Accept = 'application/json';
  return this.request(url, opts);
};

['image', 'voice', 'video', 'thumb'].forEach(function (type) {
  var method = 'upload' + type[0].toUpperCase() + type.substring(1);
  var newMethod = method + 'Media';
  exports[method] = util.deprecate(async function (filepath) {
    return this.uploadMedia(filepath, type);
  }, `${method}: Use ${newMethod} instead`);

  exports[newMethod] = async function (filepath) {     // 未接受filename参数
    return this.uploadMedia(filepath, type);     // 调用uploadMedia方法
  };
});

上传了文件了才会返回media_id呀,你这个逻辑搞错了。

上传了文件了才会返回media_id呀,你这个逻辑搞错了。

不是media_id,可查看我写的注释
我的意思是:
你这里导出了4个快捷方法。那调用这4个快捷方法时,如果filepath是传递的buffer,那么就缺失了filename这个参数。

if (Buffer.isBuffer(filepath)) {
    form.buffer('media', filepath, filename, mime);     // 需要filename这个参数
  } 
['image', 'voice', 'video', 'thumb'].forEach(function (type) {
  var method = 'upload' + type[0].toUpperCase() + type.substring(1);
  var newMethod = method + 'Media';
  exports[method] = util.deprecate(async function (filepath) {
    return this.uploadMedia(filepath, type);
  }, `${method}: Use ${newMethod} instead`);

  exports[newMethod] = async function (filepath) {     // 未接受filename参数
    return this.uploadMedia(filepath, type);     // 调用uploadMedia方法
  };
});

``