diff --git a/package.json b/package.json index 3fe980e..d1d92ea 100644 --- a/package.json +++ b/package.json @@ -9,9 +9,11 @@ "author": "David A Roberts", "license": "MIT", "dependencies": { + "image-size": "^0.5.1", "matrix-js-sdk": "*", "node-localstorage": "^1.3.0", - "parent-require": "^1.0.0" + "parent-require": "^1.0.0", + "request": "^2.79.0" }, "peerDependencies": { "hubot": ">=2.0" diff --git a/src/matrix.coffee b/src/matrix.coffee index 19067bb..eb01024 100644 --- a/src/matrix.coffee +++ b/src/matrix.coffee @@ -6,6 +6,7 @@ catch sdk = require 'matrix-js-sdk' request = require 'request' +sizeOf = require 'image-size' unless localStorage? {LocalStorage} = require('node-localstorage') @@ -20,7 +21,7 @@ class Matrix extends Adapter for str in strings @robot.logger.info "Sending to #{envelope.room}: #{str}" if /^(f|ht)tps?:\/\//i.test(str) - @sendImage envelope, str + @sendURL envelope, str else @client.sendNotice(envelope.room, str).catch (err) => if err.name == 'UnknownDeviceError' @@ -47,13 +48,22 @@ class Matrix extends Adapter topic: str }, "" - sendImage: (envelope, url) -> - @client.uploadContent(stream: request url, name: url).done (murl) => - @client.sendMessage envelope.room, { - msgtype: "m.image", - body: url, - url: JSON.parse(murl).content_uri - } + sendURL: (envelope, url) -> + @robot.logger.info "Downloading #{url}" + request url: url, encoding: null, (error, response, body) => + if error + @robot.logger.info "Request error: #{JSON.stringify error}" + else if response.statusCode == 200 + try + dims = sizeOf body + @robot.logger.info "Image has dimensions #{JSON.stringify dims}, size #{body.length}" + dims.type = 'jpeg' if dims.type == 'jpg' + info = { mimetype: "image/#{dims.type}", h: dims.height, w: dims.width, size: body.length } + @client.uploadContent(body, name: url, type: info.mimetype, rawResponse: false, onlyContentUri: true).done (content_uri) => + @client.sendImageMessage(envelope.room, content_uri, info, url) + catch error + @robot.logger.info error.message + @send envelope, " #{url}" run: -> @robot.logger.info "Run #{@robot.name}"