1010import com .facebook .react .bridge .ReactApplicationContext ;
1111import com .facebook .react .bridge .ReactContext ;
1212import com .facebook .react .bridge .ReadableMap ;
13+ import com .facebook .react .bridge .ReadableMapKeySetIterator ;
1314import com .facebook .react .bridge .WritableMap ;
1415import com .facebook .react .modules .core .DeviceEventManagerModule ;
15- import com .reactnativecompressor .Utils .FileUplaoder .FileUploadHelper ;
1616
1717import java .io .DataOutputStream ;
1818import java .io .File ;
1919import java .io .FileInputStream ;
20- import java .io .InputStream ;
21- import java .lang .reflect .Method ;
2220import java .net .HttpURLConnection ;
2321import java .net .URL ;
2422
@@ -27,6 +25,7 @@ public class FileUploader extends AsyncTask<String, Void, String> {
2725 private String fileUrl ;
2826 private final FileUploadHelper options ;
2927 private final ReactApplicationContext reactContext ;
28+ private final String TAG ="asyncTaskFileUploader" ;
3029 public FileUploader (String fileUrl , ReadableMap options ,ReactApplicationContext reactContext ,Promise promise ){
3130 this .fileUrl =fileUrl ;
3231 this .options = FileUploadHelper .fromMap (options );
@@ -49,12 +48,11 @@ private void sendProgressEvent(float progress) {
4948 _data .putDouble ("written" , progress *100 );
5049 _data .putDouble ("total" , 100 );
5150 _params .putMap ("data" , _data );
52- sendEvent (reactContext , "VideoCompressorProgress" , _params );
51+ sendEvent (this . reactContext , "VideoCompressorProgress" , _params );
5352 }
5453
5554 @ Override
5655 protected String doInBackground (String ... params ) {
57-
5856 try {
5957 String sourceFileUri = fileUrl ;
6058
@@ -77,19 +75,25 @@ protected String doInBackground(String... params) {
7775 FileInputStream fileInputStream = new FileInputStream (
7876 sourceFile );
7977 URL url = new URL (upLoadServerUri );
80-
8178 // Open a HTTP connection to the URL
8279 conn = (HttpURLConnection ) url .openConnection ();
83- conn .setDoInput (true ); // Allow Inputs
8480 conn .setDoOutput (true ); // Allow Outputs
81+ conn .setDoInput (true ); // Allow Inputs
82+ ReadableMapKeySetIterator headerIterator = this .options .headers .keySetIterator ();
83+ while (headerIterator .hasNextKey ()) {
84+ String key = headerIterator .nextKey ();
85+ String value = this .options .headers .getString (key );
86+ Log .d (TAG , key +" value: " +value );
87+ conn .setRequestProperty (key , value );
88+ }
89+
8590 conn .setUseCaches (false ); // Don't use a Cached Copy
8691 conn .setRequestMethod (this .options .method );
8792 conn .setDoOutput (true );
8893 conn .setRequestProperty ("Connection" , "Keep-Alive" );
8994 conn .setRequestProperty ("file" , sourceFileUri );
90-
91-
92- dos = new DataOutputStream (conn .getOutputStream ());
95+ conn .connect ();
96+ dos = new DataOutputStream (conn .getOutputStream ());
9397
9498 dos .writeBytes (twoHyphens + boundary + lineEnd );
9599
@@ -128,9 +132,14 @@ protected String doInBackground(String... params) {
128132 .getResponseMessage ();
129133
130134 if (conn .getResponseCode () == 200 ) {
131- Log .d ("uploadFile" , "doInBackground: upload successfully" );
135+ Log .d (TAG , "doInBackground: upload successfully" );
132136 sendProgressEvent (1 );
133- promise .resolve ("" );
137+ this .promise .resolve ("" );
138+ }
139+ else
140+ {
141+ Log .d (TAG , serverResponseMessage +" doInBackground: error" +conn .getResponseCode ());
142+ this .promise .reject ("" );
134143 }
135144
136145 fileInputStream .close ();
@@ -142,6 +151,10 @@ protected String doInBackground(String... params) {
142151 e .printStackTrace ();
143152
144153 }
154+ finally {
155+ if (conn != null )
156+ conn .disconnect ();
157+ }
145158 }
146159
147160
0 commit comments