From 24fe201b433c432499ddaa6f5fe62e30d0fd78d6 Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 6 Feb 2018 19:52:04 +0100 Subject: [PATCH] clean up sendImage code --- src/matrix.coffee | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/matrix.coffee b/src/matrix.coffee index d4b98dc..ae97e74 100644 --- a/src/matrix.coffee +++ b/src/matrix.coffee @@ -53,11 +53,12 @@ class Matrix extends Adapter @robot.logger.info 'found image: downloading...' - @getImage line, (buffer, info) => - @sendImage envelope, buffer, info, -> done() + @downloadImage line, (buffer) => + name = line.split('/')[-1..][0] + @sendImage envelope, buffer, name, -> done() - getImage: (imageURL, callback) -> + downloadImage: (imageURL, callback) -> # process the image a bit ne.get imageURL, follow_max: 5, (err, res, body) => gm(body) @@ -66,15 +67,17 @@ class Matrix extends Adapter .resize(360000,'@>') .toBuffer (err, buffer) => @robot.logger.info 'image downloaded and processed' + callback buffer - gm(buffer).identify "%m %w %h", (err, format) -> - [type, width, height] = format.split ' ' - callback buffer, - mimetype: "image/" + type.toLowerCase() - w: width - h: height - size: buffer.length - url: imageURL + + getImageInfo: (buffer, callback) -> + gm(buffer).identify "%m %w %h", (err, format) -> + [type, width, height] = format.split ' ' + callback + mimetype: "image/" + type.toLowerCase() + w: width + h: height + size: buffer.length sendText: (envelope, text, callback) -> @@ -85,7 +88,7 @@ class Matrix extends Adapter callback() if callback? - sendImage: (envelope, buffer, info, callback) -> + sendRawImage: (envelope, buffer, info, callback) -> try @client.uploadContent(buffer, name: info.url @@ -104,6 +107,14 @@ class Matrix extends Adapter callback() if callback? + sendImage: (envelope, buffer, name, callback) -> + @getImageInfo buffer, (info) => + @sendRawImage envelope, buffer, + url: if name? then name else 'image' + mimetype: info.mimetype + , callback + + send: (envelope, lines...) -> [..., last] = lines if typeof last is 'function'