11package com .baltsoft ;
22
3- import com .baltsoft .Model .ConversionResponse ;
4- import com .baltsoft .Model .ConversionResponseFile ;
53import okhttp3 .*;
64import javax .activation .MimetypesFileTypeMap ;
75import java .io .*;
86import java .math .BigDecimal ;
7+ import java .nio .file .Files ;
98import java .nio .file .Path ;
10- import java .util .Arrays ;
11- import java .util .List ;
129import java .util .concurrent .CompletableFuture ;
1310import java .util .concurrent .ExecutionException ;
1411
1512public class Param {
1613 private String name ;
17- private CompletableFuture <List <String >> values ;
18- // private InputStream streamValue;
19- private MediaType fileContentType ;
20- private String fileExtension ;
21- private Path file ;
14+ private CompletableFuture <String > value ;
2215 private Config config = Config .defaults ();
2316
24- public Param (String name , String [] values ) {
25- this .name = name .toLowerCase ();
26- this .values = CompletableFuture .completedFuture (values );
27- }
28-
2917 public Param (String name , String value ) {
30- this (name , new String []{value });
18+ this .name = name .toLowerCase ();
19+ this .value = CompletableFuture .completedFuture (value );
3120 }
3221
3322 public Param (String name , int value ) {
@@ -38,58 +27,34 @@ public Param(String name, BigDecimal value) {
3827 this (name , String .valueOf (value ));
3928 }
4029
41- // public Param(String name, InputStream value, String fileFormat) {
42- // }
43-
44- public Param (String name , Path value ) throws FileNotFoundException {
45- this (name , value , Config .defaults ());
30+ public Param (String name , byte [] value , String fileFormat ) {
31+ this (name , value , fileFormat , Config .defaults ());
4632 }
4733
48- public Param (String name , Path value , Config config ) throws FileNotFoundException {
49- this (name , new String []{});
50- file = value ;
51- String contentTypeString = MimetypesFileTypeMap .getDefaultFileTypeMap ().getContentType (file .toFile ());
52- fileContentType = MediaType .parse (contentTypeString );
53- fileExtension = getFileExtension (file );
54- values = upload (file , fileContentType , config );
34+ public Param (String name , byte [] value , String fileFormat , Config config ) {
35+ this (name , "" );
36+ String fileName = "getFile." + fileFormat ;
37+ String contentTypeString = MimetypesFileTypeMap .getDefaultFileTypeMap ().getContentType (fileName );
38+ this .value = upload (value , fileName , MediaType .parse (contentTypeString ), config );
5539 }
5640
57- public Param (String name , ConversionResult value ) throws ExecutionException , InterruptedException {
58- this (name , new String []{});
59-
60- value .getResponseFuture ().thenApplyAsync (r -> {
61- String [] urls = Arrays .stream (r .Files ).map (Param ::apply ).;
62- });
63-
64- ConversionResponse conversionResponse = value .get ();
65- String [] urls = new String [conversionResponse .Files .length ];
66- for (int i = 0 ; i < conversionResponse .Files .length ; i ++) {
67- urls [i ] = conversionResponse .Files [i ].Url ;
68- }
69- this .values = CompletableFuture .completedFuture (urls );
41+ public Param (String name , Path value ) throws IOException {
42+ this (name , value , Config .defaults ());
7043 }
7144
72- private static CompletableFuture <String []> upload (Path file , MediaType fileContentType , Config config ) {
73- return CompletableFuture .supplyAsync (() -> {
74- Request request = new Request .Builder ()
75- .url (Http .getUrlBuilder (config ).addPathSegment ("upload" )
76- .addQueryParameter ("filename" , file .getFileName ().toString ())
77- .build ())
78- .post (RequestBody .create (fileContentType , file .toFile ()))
79- .build ();
45+ public Param (String name , Path value , Config config ) throws IOException {
46+ this (name , "" );
47+ String contentTypeString = MimetypesFileTypeMap .getDefaultFileTypeMap ().getContentType (value .toFile ());
48+ this .value = upload (Files .readAllBytes (value ), value .getFileName ().toString (), MediaType .parse (contentTypeString ), config );
49+ }
8050
81- String bodyString = null ;
82- try {
83- bodyString = Http .getClient ().newCall (request ).execute ().body ().string ();
84- } catch (IOException e ) {
85- throw new RuntimeException (e );
86- }
87- return new String [] {bodyString };
88- });
51+ public Param (String name , ConversionResult value ) throws ExecutionException , InterruptedException {
52+ this (name , value , 0 );
8953 }
9054
91- private static String apply (ConversionResponseFile f ) {
92- return f .Url ;
55+ public Param (String name , ConversionResult value , int fileIndex ) throws ExecutionException , InterruptedException {
56+ this (name , "" );
57+ this .value = value .getFile (fileIndex ).thenApplyAsync (f -> f .getUrl ());
9358 }
9459
9560 public String getName () {
@@ -101,24 +66,23 @@ public Param setConfig(Config config) {
10166 return this ;
10267 }
10368
104- private String getFileExtension (Path file ) {
105- String name = file .getFileName ().toString ();
106- try {
107- return name .substring (name .lastIndexOf ("." ) + 2 );
108- } catch (Exception e ) {
109- return "" ;
110- }
69+ public String getValue () throws ExecutionException , InterruptedException {
70+ return this .value .get ();
11171 }
11272
113- public MediaType getFileContentType () {
114- return fileContentType ;
115- }
116-
117- public String getFileExtension () {
118- return fileExtension ;
119- }
120-
121- public String [] getValues () throws ExecutionException , InterruptedException {
122- return values .get ();
73+ private static CompletableFuture <String > upload (byte [] data , String fileName , MediaType fileContentType , Config config ) {
74+ return CompletableFuture .supplyAsync (() -> {
75+ Request request = new Request .Builder ()
76+ .url (Http .getUrlBuilder (config ).addPathSegment ("upload" )
77+ .addQueryParameter ("filename" , fileName .toString ())
78+ .build ())
79+ .post (RequestBody .create (fileContentType , data ))
80+ .build ();
81+ try {
82+ return Http .getClient ().newCall (request ).execute ().body ().string ();
83+ } catch (IOException e ) {
84+ throw new RuntimeException (e );
85+ }
86+ });
12387 }
124- }
88+ }
0 commit comments