@@ -19,84 +19,88 @@ import java.util.concurrent.Future
19
19
class FileBox (options : FileBoxOptions ) {
20
20
21
21
@JsonProperty
22
- private var mimeType: String? = null
22
+ var mimeType: String? = null
23
+
23
24
@JsonProperty
24
- private var base64 : String? = null
25
+ var base64: String? = null
26
+
25
27
@JsonProperty
26
- private var remoteUrl : String? = null
28
+ var remoteUrl: String? = null
29
+
27
30
@JsonProperty
28
- private var qrCode : String? = null
31
+ var qrCode: String? = null
32
+
29
33
@JsonProperty
30
- private var buffer : ByteArray? = null
31
- private var localPath : String? = null
34
+ var buffer: ByteArray? = null
35
+ var localPath: String? = null
36
+
32
37
@JsonProperty
33
- private var headers : OutgoingHttpHeaders ? = null
38
+ var headers: OutgoingHttpHeaders ? = null
39
+
34
40
@JsonProperty
35
- private var name : String? = null
41
+ var name: String? = null
42
+
36
43
@JsonProperty
37
- private var metadata: Metadata ? = null
44
+ var metadata: Metadata ? = null
45
+
38
46
@JsonProperty
39
- private lateinit var boxType: FileBoxType
47
+ var boxType: FileBoxType
40
48
41
- private val client: OkHttpClient = OkHttpClient ()
49
+ private val client: OkHttpClient = OkHttpClient ()
42
50
43
51
init {
44
- when (options){
45
- is FileBoxOptionsBuffer -> {
52
+ when (options) {
53
+ is FileBoxOptionsBuffer -> {
46
54
this .name = options.name
47
55
this .boxType = options.type
48
56
this .buffer = options.buffer
49
57
}
50
58
51
- is FileBoxOptionsFile -> {
59
+ is FileBoxOptionsFile -> {
52
60
this .name = options.name
53
61
this .boxType = options.type
54
62
this .localPath = options.path
55
63
}
56
64
57
- is FileBoxOptionsUrl -> {
65
+ is FileBoxOptionsUrl -> {
58
66
this .name = options.name
59
67
this .boxType = options.type
60
68
this .remoteUrl = options.url
61
69
this .headers = options.headers
62
70
}
63
71
64
- is FileBoxOptionsStream -> {
72
+ is FileBoxOptionsStream -> {
65
73
this .name = options.name
66
74
this .boxType = options.type
67
75
}
68
76
69
- is FileBoxOptionsQRCode -> {
77
+ is FileBoxOptionsQRCode -> {
70
78
this .name = options.name
71
79
this .boxType = options.type
72
80
this .qrCode = options.qrCode
73
81
}
74
82
75
- is FileBoxOptionsBase64 -> {
83
+ is FileBoxOptionsBase64 -> {
76
84
this .name = options.name
77
85
this .boxType = options.type
78
86
this .base64 = options.base64
79
87
}
80
-
81
-
82
-
83
88
}
84
-
85
89
}
86
90
87
- fun type ():FileBoxType {
91
+ fun type (): FileBoxType {
88
92
return this .boxType
89
93
}
90
94
91
- fun ready ():Future <Void >{
92
- if (this .boxType == FileBoxType .Url ){
95
+ fun ready (): Future <Void > {
96
+ if (this .boxType == FileBoxType .Url ) {
93
97
94
98
}
95
99
96
100
return CompletableFuture .completedFuture(null );
97
101
}
98
102
99
- fun syncRemoteName ():Future <Void >{
103
+ fun syncRemoteName (): Future <Void > {
100
104
101
105
val httpHeadHeader = httpHeadHeader(this .remoteUrl!! )
102
106
@@ -121,54 +125,53 @@ class FileBox(options: FileBoxOptions) {
121
125
122
126
}
123
127
124
- private fun httpHeadHeader (url : String ):Map <String , List <String >>{
128
+ private fun httpHeadHeader (url : String ): Map <String , List <String >> {
125
129
126
130
val request: Request = Request .Builder ()
127
- .url(url)
128
- .build()
131
+ .url(url)
132
+ .build()
129
133
130
- client.newCall(request).execute().use {
131
- response ->
134
+ client.newCall(request).execute().use { response ->
132
135
val headers = response.headers
133
136
return headers.toMultimap()
134
137
}
135
138
}
136
139
137
- fun toJsonString ():String {
140
+ fun toJsonString (): String {
138
141
buffer = toByte(this )
139
142
140
143
return JsonUtils .write(this )
141
144
142
145
}
143
146
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 -> {
147
150
148
151
val file = File (ClassLoader .getSystemResource(" dong.jpg" ).path)
149
152
150
153
return FileUtils .readFileToByteArray(file)
151
154
152
155
}
153
156
154
- FileBoxType .Url -> {
157
+ FileBoxType .Url -> {
155
158
return null ;
156
159
}
157
160
158
- else -> {
161
+ else -> {
159
162
TODO ()
160
163
}
161
164
162
165
}
163
166
}
164
167
165
- companion object {
168
+ companion object {
166
169
167
170
@JvmStatic
168
- fun fromFile (path : String ,name : String ):FileBox {
171
+ fun fromFile (path : String , name : String ): FileBox {
169
172
var localname = name
170
173
171
- if (StringUtils .isEmpty(name)){
174
+ if (StringUtils .isEmpty(name)) {
172
175
localname = FilenameUtils .getBaseName(path)
173
176
}
174
177
@@ -178,70 +181,70 @@ class FileBox(options: FileBoxOptions) {
178
181
}
179
182
180
183
@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;
219
222
}
220
223
221
224
@JvmStatic
222
- fun fromBase64 (base64 : String ,name : String ):FileBox {
225
+ fun fromBase64 (base64 : String , name : String ): FileBox {
223
226
val options = FileBoxOptionsBase64 (base64 = base64, name = name)
224
227
return FileBox (options)
225
228
}
226
229
227
230
@JvmStatic
228
- fun fromDataUrl (dataUrl : String ,name : String ):FileBox {
231
+ fun fromDataUrl (dataUrl : String , name : String ): FileBox {
229
232
val base64 = dataUrlToBase64(dataUrl);
230
- return fromBase64(base64,name)
233
+ return fromBase64(base64, name)
231
234
}
232
235
233
236
@JvmStatic
234
- fun fromQRCode (qrCode : String ):FileBox {
237
+ fun fromQRCode (qrCode : String ): FileBox {
235
238
val optionsQRCode = FileBoxOptionsQRCode (name = " qrcode.png" , qrCode = qrCode)
236
239
return FileBox (optionsQRCode)
237
240
}
238
241
239
242
@JvmStatic
240
- fun fromUrl (url : String ,name : String? ,headers : OutgoingHttpHeaders ? = null):FileBox {
243
+ fun fromUrl (url : String , name : String? , headers : OutgoingHttpHeaders ? = null): FileBox {
241
244
242
- var localName : String? = name
245
+ var localName: String? = name
243
246
244
- if (StringUtils .isEmpty(url)){
247
+ if (StringUtils .isEmpty(url)) {
245
248
val parsedUrl = URL (url)
246
249
localName = parsedUrl.path
247
250
}
@@ -253,22 +256,13 @@ class FileBox(options: FileBoxOptions) {
253
256
}
254
257
255
258
256
-
257
259
/* *
258
260
* ?????
259
261
*/
260
- fun dataUrlToBase64 (dataUrl : String ):String {
262
+ fun dataUrlToBase64 (dataUrl : String ): String {
261
263
val split = StringUtils .split(dataUrl, " ," )
262
264
return split[split.size - 1 ]
263
265
}
264
266
265
267
}
266
-
267
-
268
-
269
-
270
-
271
-
272
-
273
-
274
268
}
0 commit comments