Skip to content

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

Description

@xuwenliu

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方法
  };
});

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions