@@ -19,84 +19,88 @@ import java.util.concurrent.Future
1919class FileBox (options : FileBoxOptions ) {
2020
2121 @JsonProperty
22- private var mimeType: String? = null
22+ var mimeType: String? = null
23+
2324 @JsonProperty
24- private var base64 : String? = null
25+ var base64: String? = null
26+
2527 @JsonProperty
26- private var remoteUrl : String? = null
28+ var remoteUrl: String? = null
29+
2730 @JsonProperty
28- private var qrCode : String? = null
31+ var qrCode: String? = null
32+
2933 @JsonProperty
30- private var buffer : ByteArray? = null
31- private var localPath : String? = null
34+ var buffer: ByteArray? = null
35+ var localPath: String? = null
36+
3237 @JsonProperty
33- private var headers : OutgoingHttpHeaders ? = null
38+ var headers: OutgoingHttpHeaders ? = null
39+
3440 @JsonProperty
35- private var name : String? = null
41+ var name: String? = null
42+
3643 @JsonProperty
37- private var metadata: Metadata ? = null
44+ var metadata: Metadata ? = null
45+
3846 @JsonProperty
39- private lateinit var boxType: FileBoxType
47+ var boxType: FileBoxType
4048
41- private val client: OkHttpClient = OkHttpClient ()
49+ private val client: OkHttpClient = OkHttpClient ()
4250
4351 init {
44- when (options){
45- is FileBoxOptionsBuffer -> {
52+ when (options) {
53+ is FileBoxOptionsBuffer -> {
4654 this .name = options.name
4755 this .boxType = options.type
4856 this .buffer = options.buffer
4957 }
5058
51- is FileBoxOptionsFile -> {
59+ is FileBoxOptionsFile -> {
5260 this .name = options.name
5361 this .boxType = options.type
5462 this .localPath = options.path
5563 }
5664
57- is FileBoxOptionsUrl -> {
65+ is FileBoxOptionsUrl -> {
5866 this .name = options.name
5967 this .boxType = options.type
6068 this .remoteUrl = options.url
6169 this .headers = options.headers
6270 }
6371
64- is FileBoxOptionsStream -> {
72+ is FileBoxOptionsStream -> {
6573 this .name = options.name
6674 this .boxType = options.type
6775 }
6876
69- is FileBoxOptionsQRCode -> {
77+ is FileBoxOptionsQRCode -> {
7078 this .name = options.name
7179 this .boxType = options.type
7280 this .qrCode = options.qrCode
7381 }
7482
75- is FileBoxOptionsBase64 -> {
83+ is FileBoxOptionsBase64 -> {
7684 this .name = options.name
7785 this .boxType = options.type
7886 this .base64 = options.base64
7987 }
80-
81-
82-
8388 }
84-
8589 }
8690
87- fun type ():FileBoxType {
91+ fun type (): FileBoxType {
8892 return this .boxType
8993 }
9094
91- fun ready ():Future <Void >{
92- if (this .boxType == FileBoxType .Url ){
95+ fun ready (): Future <Void > {
96+ if (this .boxType == FileBoxType .Url ) {
9397
9498 }
9599
96100 return CompletableFuture .completedFuture(null );
97101 }
98102
99- fun syncRemoteName ():Future <Void >{
103+ fun syncRemoteName (): Future <Void > {
100104
101105 val httpHeadHeader = httpHeadHeader(this .remoteUrl!! )
102106
@@ -121,54 +125,53 @@ class FileBox(options: FileBoxOptions) {
121125
122126 }
123127
124- private fun httpHeadHeader (url : String ):Map <String , List <String >>{
128+ private fun httpHeadHeader (url : String ): Map <String , List <String >> {
125129
126130 val request: Request = Request .Builder ()
127- .url(url)
128- .build()
131+ .url(url)
132+ .build()
129133
130- client.newCall(request).execute().use {
131- response ->
134+ client.newCall(request).execute().use { response ->
132135 val headers = response.headers
133136 return headers.toMultimap()
134137 }
135138 }
136139
137- fun toJsonString ():String {
140+ fun toJsonString (): String {
138141 buffer = toByte(this )
139142
140143 return JsonUtils .write(this )
141144
142145 }
143146
144- fun toByte (fileBox : FileBox ):ByteArray? {
145- when (fileBox.type()){
146- FileBoxType .File -> {
147+ fun toByte (fileBox : FileBox ): ByteArray? {
148+ when (fileBox.type()) {
149+ FileBoxType .File -> {
147150
148151 val file = File (ClassLoader .getSystemResource(" dong.jpg" ).path)
149152
150153 return FileUtils .readFileToByteArray(file)
151154
152155 }
153156
154- FileBoxType .Url -> {
157+ FileBoxType .Url -> {
155158 return null ;
156159 }
157160
158- else -> {
161+ else -> {
159162 TODO ()
160163 }
161164
162165 }
163166 }
164167
165- companion object {
168+ companion object {
166169
167170 @JvmStatic
168- fun fromFile (path : String ,name : String ):FileBox {
171+ fun fromFile (path : String , name : String ): FileBox {
169172 var localname = name
170173
171- if (StringUtils .isEmpty(name)){
174+ if (StringUtils .isEmpty(name)) {
172175 localname = FilenameUtils .getBaseName(path)
173176 }
174177
@@ -178,70 +181,70 @@ class FileBox(options: FileBoxOptions) {
178181 }
179182
180183 @JvmStatic
181- fun fromJson (obj : String ):FileBox {
182-
183- return JsonUtils .readValue<FileBox >(obj)
184-
185- // val jsonObject = JsonObject (obj)
186- //
187- // var fileBox:FileBox
188- //
189- // val type = jsonObject.getInteger ("boxType")
190- //
191- // when(type){
192- //
193- // FileBoxType.Base64.code ->{
194- // fileBox = fromBase64(
195- // jsonObject.getString ("base64"),
196- // jsonObject.getString ("name")
197- // )
198- // }
199- //
200- // FileBoxType.Url.code ->{
201- // fileBox = fromUrl(
202- // jsonObject.getString ("name"),
203- // jsonObject.getString ("remoteUrl")
204- // )
205- // }
206- //
207- // FileBoxType.QRcode.code ->{
208- // fileBox = fromQRCode(
209- // jsonObject.getString ("qrCode")
210- // )
211- // }
212- // else ->{
213- // throw Exception("unknown filebox json object{type} $jsonObject ")
214- // }
215- // }
216- //
217- // fileBox.metadata = jsonObject .get("metadata")
218- // return fileBox;
184+ fun fromJson (obj : String ): FileBox {
185+
186+ // return JsonUtils.readValue<FileBox>(obj)
187+
188+ val jsonNode = JsonUtils .mapper.readTree (obj)
189+
190+ var fileBox: FileBox
191+
192+ val type = jsonNode.findValue (" boxType" ).asInt( )
193+
194+ when (type) {
195+
196+ FileBoxType .Base64 .code -> {
197+ fileBox = fromBase64(
198+ jsonNode.findValue (" base64" ).asText( ),
199+ jsonNode.findValue (" name" ).asText( )
200+ )
201+ }
202+
203+ FileBoxType .Url .code -> {
204+ fileBox = fromUrl(
205+ jsonNode.findValue (" name" ).asText( ),
206+ jsonNode.findValue (" remoteUrl" ).asText( )
207+ )
208+ }
209+
210+ FileBoxType .QRcode .code -> {
211+ fileBox = fromQRCode(
212+ jsonNode.findValue (" qrCode" ).asText( )
213+ )
214+ }
215+ else -> {
216+ throw Exception (" unknown filebox json object{type} $jsonNode " )
217+ }
218+ }
219+
220+ // fileBox.metadata = jsonNode .get("metadata")
221+ return fileBox;
219222 }
220223
221224 @JvmStatic
222- fun fromBase64 (base64 : String ,name : String ):FileBox {
225+ fun fromBase64 (base64 : String , name : String ): FileBox {
223226 val options = FileBoxOptionsBase64 (base64 = base64, name = name)
224227 return FileBox (options)
225228 }
226229
227230 @JvmStatic
228- fun fromDataUrl (dataUrl : String ,name : String ):FileBox {
231+ fun fromDataUrl (dataUrl : String , name : String ): FileBox {
229232 val base64 = dataUrlToBase64(dataUrl);
230- return fromBase64(base64,name)
233+ return fromBase64(base64, name)
231234 }
232235
233236 @JvmStatic
234- fun fromQRCode (qrCode : String ):FileBox {
237+ fun fromQRCode (qrCode : String ): FileBox {
235238 val optionsQRCode = FileBoxOptionsQRCode (name = " qrcode.png" , qrCode = qrCode)
236239 return FileBox (optionsQRCode)
237240 }
238241
239242 @JvmStatic
240- fun fromUrl (url : String ,name : String? ,headers : OutgoingHttpHeaders ? = null):FileBox {
243+ fun fromUrl (url : String , name : String? , headers : OutgoingHttpHeaders ? = null): FileBox {
241244
242- var localName : String? = name
245+ var localName: String? = name
243246
244- if (StringUtils .isEmpty(url)){
247+ if (StringUtils .isEmpty(url)) {
245248 val parsedUrl = URL (url)
246249 localName = parsedUrl.path
247250 }
@@ -253,22 +256,13 @@ class FileBox(options: FileBoxOptions) {
253256 }
254257
255258
256-
257259 /* *
258260 * ?????
259261 */
260- fun dataUrlToBase64 (dataUrl : String ):String {
262+ fun dataUrlToBase64 (dataUrl : String ): String {
261263 val split = StringUtils .split(dataUrl, " ," )
262264 return split[split.size - 1 ]
263265 }
264266
265267 }
266-
267-
268-
269-
270-
271-
272-
273-
274268}
0 commit comments