provide image info, fixes #6

This commit is contained in:
David A Roberts 2017-02-14 11:28:54 +00:00
parent 64e4f890e5
commit 95ddd678db
2 changed files with 21 additions and 9 deletions

View File

@ -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"

View File

@ -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}"