clean up sendImage code

This commit is contained in:
Michele Guerini Rocco 2018-02-06 19:52:04 +01:00
parent a034cc1a0c
commit 24fe201b43
Signed by: rnhmjoj
GPG Key ID: 91BE884FBA4B591A

View File

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