Skip to content

Commit 18b0bc8

Browse files
committed
#21 add ResourceBoxType for ResourceBox interface
1 parent 33c4b24 commit 18b0bc8

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/GrpcSupport.scala

+5-5
Original file line numberDiff line numberDiff line change
@@ -188,12 +188,12 @@ trait GrpcSupport {
188188
response
189189
}
190190
private val ACCESS_KEY_ID = "AKIA3PQY2OQG5FEXWMH6"
191-
private val BUCKET= "macpro-message-file",
192-
private val EXPIRE_TIME= 3600 * 24 * 3,
193-
private val PATH= "image-message",
194-
private val SECRET_ACCESS_KEY= "jw7Deo+W8l4FTOL2BXd/VubTJjt1mhm55sRhnsEn",
191+
private val BUCKET= "macpro-message-file"
192+
private val EXPIRE_TIME= 3600 * 24 * 3
193+
private val PATH= "image-message"
194+
private val SECRET_ACCESS_KEY= "jw7Deo+W8l4FTOL2BXd/VubTJjt1mhm55sRhnsEn"
195195
// private val s3 = new AmazonS3Client(new BasicAWSCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY));
196-
private val s3=new AmazonS3ClientBuilder()
196+
private val s3=AmazonS3ClientBuilder.standard()
197197
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY_ID, SECRET_ACCESS_KEY)))
198198
.enablePayloadSigning()
199199
.withRegion(Regions.CN_NORTHWEST_1).build(); // 此处根据自己的 s3 地区位置改变

wechaty-puppet-padplus/src/main/scala/wechaty/padplus/support/MessageRawSupport.scala

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import wechaty.padplus.schemas.GrpcSchemas.GrpcMessagePayload
77
import wechaty.padplus.schemas.ModelMessage.{GrpcResponseMessageData, PadplusMessagePayload}
88
import wechaty.padplus.schemas.PadplusEnums.PadplusMessageType
99
import wechaty.puppet.ResourceBox
10-
import wechaty.puppet.ResourceBox.UrlResourceBox
1110
import wechaty.puppet.events.EventEmitter
1211
import wechaty.puppet.schemas.Event.EventMessagePayload
1312
import wechaty.puppet.schemas.Image.ImageType.Type
@@ -39,10 +38,10 @@ trait MessageRawSupport {
3938
override def messageSendContact(conversationId: String, contactId: String): String = ???
4039

4140
override def messageSendFile(conversationId: String, file: ResourceBox): String = {
42-
val url = file match{
43-
case urlResource:UrlResourceBox => urlResource.url
44-
case _ => uploadFile(file.name,file.toStream)
45-
}
41+
// val url = file.resourceType match{
42+
// case ResourceBoxType.Url => file.url
43+
// case _ => uploadFile(file.name,file.toStream)
44+
// }
4645

4746
/*
4847
data = {

wechaty-puppet/src/main/scala/wechaty/puppet/ResourceBox.scala

+13-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import java.util.Base64
66

77
import javax.activation.MimeType
88
import org.apache.commons.io.IOUtils
9+
import wechaty.puppet.ResourceBox.ResourceBoxType
10+
import wechaty.puppet.ResourceBox.ResourceBoxType.Type
911
import wechaty.puppet.schemas.Puppet.objectMapper
1012

1113
/**
@@ -55,7 +57,7 @@ object ResourceBox {
5557
val File :Type = Value(5)
5658
val Stream :Type = Value(6)
5759
}
58-
class UrlResourceBox private(val url:String) extends AbstractResourceBox{
60+
private class UrlResourceBox (val url:String) extends AbstractResourceBox{
5961
override def toStream: InputStream = {
6062
val connection = new URL(url).openConnection.asInstanceOf[HttpURLConnection]
6163
connection.setConnectTimeout(5000)
@@ -75,8 +77,10 @@ object ResourceBox {
7577
}
7678

7779
override def name: String = url.substring(url.lastIndexOf("/")+1)
80+
81+
override def resourceType: Type = ResourceBoxType.Url
7882
}
79-
class Base64ResourceBox private(override val name:String,base64:String) extends AbstractResourceBox{
83+
private class Base64ResourceBox (override val name:String,base64:String) extends AbstractResourceBox{
8084
override def toStream: InputStream = {
8185
//decode base64 as byte array input stream
8286
new ByteArrayInputStream(Base64.getDecoder.decode(base64))
@@ -92,17 +96,22 @@ object ResourceBox {
9296
objectNode.toString
9397
}
9498

99+
override def resourceType: Type = ResourceBoxType.Base64
95100
}
96-
class StreamResourceBox private(override val name:String,stream:InputStream) extends AbstractResourceBox{
101+
private class StreamResourceBox (override val name:String,stream:InputStream) extends AbstractResourceBox{
97102
override def toStream: InputStream = stream
98103
override protected def using[T <: Closeable, R](resource: T)(block: T => R): R = {
99104
block(resource) //don't close the stream.must be closed by creator
100105
}
106+
107+
override def resourceType: Type = ResourceBoxType.Stream
101108
}
102109
private class FileResourceBox(file:File) extends AbstractResourceBox{
103110
override def toStream: InputStream = new FileInputStream(file)
104111

105112
override def name: String = file.getName
113+
114+
override def resourceType: Type = ResourceBoxType.File
106115
}
107116
private trait AbstractResourceBox extends ResourceBox {
108117
override def toBase64: String = {
@@ -130,6 +139,7 @@ object ResourceBox {
130139
}
131140
}
132141
trait ResourceBox {
142+
def resourceType:ResourceBoxType.Type
133143
def name:String
134144
def toStream:InputStream
135145
def toBase64:String

0 commit comments

Comments
 (0)