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", "author": "David A Roberts",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"image-size": "^0.5.1",
"matrix-js-sdk": "*", "matrix-js-sdk": "*",
"node-localstorage": "^1.3.0", "node-localstorage": "^1.3.0",
"parent-require": "^1.0.0" "parent-require": "^1.0.0",
"request": "^2.79.0"
}, },
"peerDependencies": { "peerDependencies": {
"hubot": ">=2.0" "hubot": ">=2.0"

View File

@ -6,6 +6,7 @@ catch
sdk = require 'matrix-js-sdk' sdk = require 'matrix-js-sdk'
request = require 'request' request = require 'request'
sizeOf = require 'image-size'
unless localStorage? unless localStorage?
{LocalStorage} = require('node-localstorage') {LocalStorage} = require('node-localstorage')
@ -20,7 +21,7 @@ class Matrix extends Adapter
for str in strings for str in strings
@robot.logger.info "Sending to #{envelope.room}: #{str}" @robot.logger.info "Sending to #{envelope.room}: #{str}"
if /^(f|ht)tps?:\/\//i.test(str) if /^(f|ht)tps?:\/\//i.test(str)
@sendImage envelope, str @sendURL envelope, str
else else
@client.sendNotice(envelope.room, str).catch (err) => @client.sendNotice(envelope.room, str).catch (err) =>
if err.name == 'UnknownDeviceError' if err.name == 'UnknownDeviceError'
@ -47,13 +48,22 @@ class Matrix extends Adapter
topic: str topic: str
}, "" }, ""
sendImage: (envelope, url) -> sendURL: (envelope, url) ->
@client.uploadContent(stream: request url, name: url).done (murl) => @robot.logger.info "Downloading #{url}"
@client.sendMessage envelope.room, { request url: url, encoding: null, (error, response, body) =>
msgtype: "m.image", if error
body: url, @robot.logger.info "Request error: #{JSON.stringify error}"
url: JSON.parse(murl).content_uri 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: -> run: ->
@robot.logger.info "Run #{@robot.name}" @robot.logger.info "Run #{@robot.name}"