From 3b3f5d831a2b162fe42f3a29737b5028f1f91f6f Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 25 Jan 2016 11:07:06 +1100 Subject: [PATCH] More string checking, and stream filename checking (fs streams) --- lib/Client/InternalClient.js | 11 ++++++++++- src/Client/InternalClient.js | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index d99b4b459..2bb8f8a34 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -571,7 +571,16 @@ var InternalClient = (function () { InternalClient.prototype.sendFile = function sendFile(where, _file, name) { var _this14 = this; - name = name ? name : require('path').basename(attachment); + if (!name) { + if (_file instanceof String || typeof _file === "string") { + name = require("path").basename(attachment); + } else if (_file.path) { + // fs.createReadStream()'s have .path that give the path. Not sure about other streams though. + name = require("path").basename(_file.path); + } else { + name = "image.png"; // Just have to go with default filenames. + } + } return this.resolver.resolveChannel(where).then(function (channel) { return _this14.apiRequest("post", _Constants.Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 4c104930a..b743af36e 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -461,7 +461,17 @@ export default class InternalClient { // def sendFile sendFile(where, _file, name) { - name = name ? name : require('path').basename(attachment); + + if (!name) { + if (_file instanceof String || typeof _file === "string") { + name = require("path").basename(attachment); + } else if (_file.path) { + // fs.createReadStream()'s have .path that give the path. Not sure about other streams though. + name = require("path").basename(_file.path); + } else { + name = "image.png"; // Just have to go with default filenames. + } + } return this.resolver.resolveChannel(where) .then(channel =>