From 47f35a942c008f7c2083039c82c5c997d3cd15eb Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Sun, 24 Jan 2016 21:23:09 +1100 Subject: [PATCH 1/3] Make sendFile() use original file name by default --- lib/Client/Client.js | 5 +++-- lib/Client/InternalClient.js | 4 ++-- src/Client/Client.js | 4 +++- src/Client/InternalClient.js | 4 +++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 46672b55b..877efe134 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -206,10 +206,11 @@ var Client = (function (_EventEmitter) { // def sendFile - Client.prototype.sendFile = function sendFile(where, attachment) { - var name = arguments.length <= 2 || arguments[2] === undefined ? "image.png" : arguments[2]; + Client.prototype.sendFile = function sendFile(where, attachment, name) { var callback = arguments.length <= 3 || arguments[3] === undefined ? function () /*err, m*/{} : arguments[3]; + name = name ? name : require('path').basename(attachment); + return this.internal.sendFile(where, attachment, name).then(dataCallback(callback), errorCallback(callback)); }; diff --git a/lib/Client/InternalClient.js b/lib/Client/InternalClient.js index 1d82f8066..d99b4b459 100644 --- a/lib/Client/InternalClient.js +++ b/lib/Client/InternalClient.js @@ -568,10 +568,10 @@ var InternalClient = (function () { // def sendFile - InternalClient.prototype.sendFile = function sendFile(where, _file) { + InternalClient.prototype.sendFile = function sendFile(where, _file, name) { var _this14 = this; - var name = arguments.length <= 2 || arguments[2] === undefined ? "image.png" : arguments[2]; + name = name ? name : require('path').basename(attachment); 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/Client.js b/src/Client/Client.js index cbc4945dc..961d50103 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -190,7 +190,9 @@ export default class Client extends EventEmitter { } // def sendFile - sendFile(where, attachment, name = "image.png", callback = (/*err, m*/) => { }) { + sendFile(where, attachment, name, callback = (/*err, m*/) => { }) { + name = name ? name : require('path').basename(attachment); + return this.internal.sendFile(where, attachment, name) .then(dataCallback(callback), errorCallback(callback)); } diff --git a/src/Client/InternalClient.js b/src/Client/InternalClient.js index 89f378fe3..4c104930a 100644 --- a/src/Client/InternalClient.js +++ b/src/Client/InternalClient.js @@ -460,7 +460,9 @@ export default class InternalClient { } // def sendFile - sendFile(where, _file, name = "image.png") { + sendFile(where, _file, name) { + name = name ? name : require('path').basename(attachment); + return this.resolver.resolveChannel(where) .then(channel => this.apiRequest("post", Endpoints.CHANNEL_MESSAGES(channel.id), true, null, { From 3a8f1ddbf9165eaacc17b147444726377787d89a Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 25 Jan 2016 10:35:44 +1100 Subject: [PATCH 2/3] Optional sendFile name callback and remove extra logic --- lib/Client/Client.js | 6 +++++- src/Client/Client.js | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/Client/Client.js b/lib/Client/Client.js index 877efe134..8c2d2584b 100644 --- a/lib/Client/Client.js +++ b/lib/Client/Client.js @@ -209,7 +209,11 @@ var Client = (function (_EventEmitter) { Client.prototype.sendFile = function sendFile(where, attachment, name) { var callback = arguments.length <= 3 || arguments[3] === undefined ? function () /*err, m*/{} : arguments[3]; - name = name ? name : require('path').basename(attachment); + if (typeof name === "function") { + // name is the callback + callback = name; + name = undefined; // Will get resolved into original filename in internal + } return this.internal.sendFile(where, attachment, name).then(dataCallback(callback), errorCallback(callback)); }; diff --git a/src/Client/Client.js b/src/Client/Client.js index 961d50103..40734a8df 100644 --- a/src/Client/Client.js +++ b/src/Client/Client.js @@ -191,8 +191,12 @@ export default class Client extends EventEmitter { // def sendFile sendFile(where, attachment, name, callback = (/*err, m*/) => { }) { - name = name ? name : require('path').basename(attachment); - + if (typeof name === "function") { + // name is the callback + callback = name; + name = undefined; // Will get resolved into original filename in internal + } + return this.internal.sendFile(where, attachment, name) .then(dataCallback(callback), errorCallback(callback)); } From 3b3f5d831a2b162fe42f3a29737b5028f1f91f6f Mon Sep 17 00:00:00 2001 From: Nicholas Tay Date: Mon, 25 Jan 2016 11:07:06 +1100 Subject: [PATCH 3/3] 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 =>