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

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

Open
xuwenliu opened this issue Jan 13, 2021 · 2 comments

Comments

@xuwenliu
Copy link

xuwenliu commented Jan 13, 2021

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方法
  };
});
@pigLoveRabbit520
Copy link

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

@xuwenliu
Copy link
Author

上传了文件了才会返回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方法
  };
});

``

@xuwenliu xuwenliu changed the title uploadImageMedia(filepath) 方法少了media参数 uploadImageMedia(filepath) 方法如果filepath传递的是buffer类型,调用该快捷方法则少了filename参数 Jan 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants