@@ -6,6 +6,8 @@ import java.util.Base64
6
6
7
7
import javax .activation .MimeType
8
8
import org .apache .commons .io .IOUtils
9
+ import wechaty .puppet .ResourceBox .ResourceBoxType
10
+ import wechaty .puppet .ResourceBox .ResourceBoxType .Type
9
11
import wechaty .puppet .schemas .Puppet .objectMapper
10
12
11
13
/**
@@ -55,7 +57,7 @@ object ResourceBox {
55
57
val File : Type = Value (5 )
56
58
val Stream : Type = Value (6 )
57
59
}
58
- class UrlResourceBox private (val url : String ) extends AbstractResourceBox {
60
+ private class UrlResourceBox (val url : String ) extends AbstractResourceBox {
59
61
override def toStream : InputStream = {
60
62
val connection = new URL (url).openConnection.asInstanceOf [HttpURLConnection ]
61
63
connection.setConnectTimeout(5000 )
@@ -75,8 +77,10 @@ object ResourceBox {
75
77
}
76
78
77
79
override def name : String = url.substring(url.lastIndexOf(" /" )+ 1 )
80
+
81
+ override def resourceType : Type = ResourceBoxType .Url
78
82
}
79
- class Base64ResourceBox private (override val name : String ,base64: String ) extends AbstractResourceBox {
83
+ private class Base64ResourceBox (override val name : String ,base64: String ) extends AbstractResourceBox {
80
84
override def toStream : InputStream = {
81
85
// decode base64 as byte array input stream
82
86
new ByteArrayInputStream (Base64 .getDecoder.decode(base64))
@@ -92,17 +96,22 @@ object ResourceBox {
92
96
objectNode.toString
93
97
}
94
98
99
+ override def resourceType : Type = ResourceBoxType .Base64
95
100
}
96
- class StreamResourceBox private (override val name : String ,stream: InputStream ) extends AbstractResourceBox {
101
+ private class StreamResourceBox (override val name : String ,stream: InputStream ) extends AbstractResourceBox {
97
102
override def toStream : InputStream = stream
98
103
override protected def using [T <: Closeable , R ](resource : T )(block : T => R ): R = {
99
104
block(resource) // don't close the stream.must be closed by creator
100
105
}
106
+
107
+ override def resourceType : Type = ResourceBoxType .Stream
101
108
}
102
109
private class FileResourceBox (file: File ) extends AbstractResourceBox {
103
110
override def toStream : InputStream = new FileInputStream (file)
104
111
105
112
override def name : String = file.getName
113
+
114
+ override def resourceType : Type = ResourceBoxType .File
106
115
}
107
116
private trait AbstractResourceBox extends ResourceBox {
108
117
override def toBase64 : String = {
@@ -130,6 +139,7 @@ object ResourceBox {
130
139
}
131
140
}
132
141
trait ResourceBox {
142
+ def resourceType : ResourceBoxType .Type
133
143
def name : String
134
144
def toStream : InputStream
135
145
def toBase64 : String
0 commit comments