Skip to content

Commit

Permalink
add cdn information to Image instance
Browse files Browse the repository at this point in the history
  • Loading branch information
jcai committed Jun 24, 2020
1 parent f4d8480 commit 2e39828
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
57 changes: 56 additions & 1 deletion wechaty/src/main/scala/wechaty/user/Image.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,71 @@
package wechaty.user

import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.annotation.{JacksonXmlProperty, JacksonXmlRootElement}
import com.typesafe.scalalogging.LazyLogging
import wechaty.Wechaty.PuppetResolver
import wechaty.puppet.ResourceBox
import wechaty.puppet.{ResourceBox, schemas}
import wechaty.puppet.schemas.Image.ImageType
import wechaty.user.Image.{CdnImage, Msg}

/**
*
* @author <a href="mailto:jcai@ganshane.com">Jun Tsai</a>
* @since 2020-06-15
*/
object Image{
@JacksonXmlRootElement(localName = "msg")
class Msg{
@JacksonXmlProperty
var img:CdnImage = _
}
class CdnImage{
@JacksonXmlProperty(isAttribute = true)
var aeskey:String = _
@JacksonXmlProperty(isAttribute = true)
var encryver :String = _
@JacksonXmlProperty(isAttribute = true)
var cdnthumblength :Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnthumbheight:Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnthumbwidth:Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnmidheight:Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnmidwidth:Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnhdheight:Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnhdwidth:Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnmidimgurl:String = _
@JacksonXmlProperty(isAttribute = true)
var length:Int= _
@JacksonXmlProperty(isAttribute = true)
var cdnbigimgurl:String = _
@JacksonXmlProperty(isAttribute = true)
var hdlength:String = _
@JacksonXmlProperty(isAttribute = true)
var md5:String = _
@JacksonXmlProperty(isAttribute = true)
var cdnthumbaeskey:String = _
@JacksonXmlProperty(isAttribute = true)
var cdnthumburl:String = _
}
}
class Image(imageId:String)(implicit resolver:PuppetResolver) extends LazyLogging{
lazy val payload: schemas.Message.MessagePayload = {
resolver
.puppet
.messagePayload(imageId)
}
def getCdnImage: CdnImage ={
val mapper = new XmlMapper()
mapper.readValue(payload.text,classOf[Msg]).img

}

def thumbnail : ResourceBox = {
resolver.puppet.messageImage(this.imageId, ImageType.Thumbnail)
}
Expand Down
21 changes: 21 additions & 0 deletions wechaty/src/test/scala/wechaty/user/ImageTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package wechaty.user

import org.junit.jupiter.api.{Assertions, Test}
import wechaty.TestBase

/**
*
* @author <a href="mailto:jcai@ganshane.com">Jun Tsai</a>
* @since 2020-06-24
*/
class ImageTest extends TestBase{
val test="<?xml version=\"1.0\"?>\n<msg>\n\t<img aeskey=\"1e1f1fa09fd3c197d544834fe0428f01\" encryver=\"1\" cdnthumbaeskey=\"1e1f1fa09fd3c197d544834fe0428f01\" cdnthumburl=\"304e0201000447304502010002047fc1d03902032f50e502049131227502045ef2c8e9042030303031393766623832303364373463373630353831643333353138376335300204010828010201000400\" cdnthumblength=\"5674\" cdnthumbheight=\"150\" cdnthumbwidth=\"150\" cdnmidheight=\"0\" cdnmidwidth=\"0\" cdnhdheight=\"0\" cdnhdwidth=\"0\" cdnmidimgurl=\"304e0201000447304502010002047fc1d03902032f50e502049131227502045ef2c8e9042030303031393766623832303364373463373630353831643333353138376335300204010828010201000400\" length=\"14193\" cdnbigimgurl=\"304e0201000447304502010002047fc1d03902032f50e502049131227502045ef2c8e9042030303031393766623832303364373463373630353831643333353138376335300204010828010201000400\" hdlength=\"14195\" md5=\"3df5c8f7130c71cf16ac79b6f49a6684\" />\n</msg>\n"
@Test
def test_cdn: Unit ={
mockRoomMessage(test)
val image = new Image("messageId")
val cdnImage = image.getCdnImage
Assertions.assertEquals("1e1f1fa09fd3c197d544834fe0428f01",cdnImage.aeskey)
Assertions.assertEquals("1e1f1fa09fd3c197d544834fe0428f01",cdnImage.cdnthumbaeskey)
}
}

0 comments on commit 2e39828

Please sign in to comment.