react-native-compressor 1.8.9 → 1.8.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -11
- package/android/build.gradle +0 -1
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.kt +3 -1
- package/android/src/main/java/com/reactnativecompressor/Utils/Uploader.kt +163 -91
- package/android/src/main/java/com/reactnativecompressor/Utils/UploaderHelper.kt +121 -0
- package/ios/Utils/Uploader.swift +123 -4
- package/lib/commonjs/index.js +15 -1
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/Uploader.js +16 -1
- package/lib/commonjs/utils/Uploader.js.map +1 -1
- package/lib/module/index.js +5 -3
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/Uploader.js +13 -0
- package/lib/module/utils/Uploader.js.map +1 -1
- package/lib/typescript/index.d.ts +5 -3
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/Uploader.d.ts +12 -11
- package/lib/typescript/utils/Uploader.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/index.tsx +6 -0
- package/src/utils/Uploader.tsx +14 -14
package/README.md
CHANGED
|
@@ -287,6 +287,17 @@ const uploadResult = await backgroundUpload(
|
|
|
287
287
|
console.log(written, total);
|
|
288
288
|
}
|
|
289
289
|
);
|
|
290
|
+
|
|
291
|
+
//OR
|
|
292
|
+
|
|
293
|
+
const uploadResult = await backgroundUpload(
|
|
294
|
+
url,
|
|
295
|
+
fileUrl,
|
|
296
|
+
{ uploadType: UploadType.MULTIPART, httpMethod: 'POST', headers },
|
|
297
|
+
(written, total) => {
|
|
298
|
+
console.log(written, total);
|
|
299
|
+
}
|
|
300
|
+
);
|
|
290
301
|
```
|
|
291
302
|
|
|
292
303
|
### Download File
|
|
@@ -406,28 +417,50 @@ await clearCache(); // this will clear cache of thumbnails cache directory
|
|
|
406
417
|
|
|
407
418
|
## Background Upload
|
|
408
419
|
|
|
409
|
-
- ###### backgroundUpload: (url: string, fileUrl: string, options:
|
|
420
|
+
- ###### backgroundUpload: (url: string, fileUrl: string, options: UploaderOptions, onProgress?: ((writtem: number, total: number) => void) | undefined) => Promise< any >
|
|
410
421
|
|
|
411
|
-
- ###### `
|
|
422
|
+
- ###### ` UploaderOptions`
|
|
412
423
|
|
|
413
424
|
```js
|
|
414
|
-
|
|
425
|
+
export enum UploadType {
|
|
426
|
+
BINARY_CONTENT = 0,
|
|
427
|
+
MULTIPART = 1,
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
export enum UploaderHttpMethod {
|
|
431
|
+
POST = 'POST',
|
|
432
|
+
PUT = 'PUT',
|
|
433
|
+
PATCH = 'PATCH',
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export declare type HTTPResponse = {
|
|
437
|
+
status: number;
|
|
438
|
+
headers: Record<string, string>;
|
|
439
|
+
body: string;
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
export declare type HttpMethod = 'POST' | 'PUT' | 'PATCH';
|
|
443
|
+
|
|
444
|
+
export declare type UploaderOptions = (
|
|
415
445
|
| {
|
|
416
|
-
uploadType?:
|
|
446
|
+
uploadType?: UploadType.BINARY_CONTENT;
|
|
447
|
+
mimeType?: string;
|
|
417
448
|
}
|
|
418
449
|
| {
|
|
419
|
-
uploadType:
|
|
420
|
-
fieldName?: string
|
|
421
|
-
mimeType?: string
|
|
422
|
-
parameters?: Record<string, string
|
|
450
|
+
uploadType: UploadType.MULTIPART;
|
|
451
|
+
fieldName?: string;
|
|
452
|
+
mimeType?: string;
|
|
453
|
+
parameters?: Record<string, string>;
|
|
423
454
|
}
|
|
424
455
|
) & {
|
|
425
|
-
headers?: Record<string, string
|
|
426
|
-
httpMethod?:
|
|
427
|
-
sessionType?: FileSystemSessionType,
|
|
456
|
+
headers?: Record<string, string>;
|
|
457
|
+
httpMethod?: UploaderHttpMethod;
|
|
428
458
|
};
|
|
429
459
|
```
|
|
430
460
|
|
|
461
|
+
**Note:** some of the uploader code is borrowed from [Expo](https://github.com/expo/expo)
|
|
462
|
+
I tested file uploader on this backend [Nodejs-File-Uploader](https://github.com/numandev1/nodejs-file-uploader)
|
|
463
|
+
|
|
431
464
|
### Download
|
|
432
465
|
|
|
433
466
|
- ##### download: ( fileUrl: string, downloadProgress?: (progress: number) => void, progressDivider?: number ) => Promise< string >
|
package/android/build.gradle
CHANGED
|
@@ -115,7 +115,6 @@ dependencies {
|
|
|
115
115
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
|
|
116
116
|
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"
|
|
117
117
|
implementation "com.googlecode.mp4parser:isoparser:1.0.6"
|
|
118
|
-
implementation 'io.github.lizhangqu:coreprogress:1.0.2'
|
|
119
118
|
implementation 'com.github.banketree:AndroidLame-kotlin:v0.0.1'
|
|
120
119
|
implementation 'javazoom:jlayer:1.0.1'
|
|
121
120
|
}
|
|
@@ -15,12 +15,14 @@ import com.reactnativecompressor.Utils.Uploader
|
|
|
15
15
|
import com.reactnativecompressor.Utils.Utils
|
|
16
16
|
import com.reactnativecompressor.Utils.Utils.generateCacheFilePath
|
|
17
17
|
import com.reactnativecompressor.Utils.Utils.getRealPath
|
|
18
|
+
import com.reactnativecompressor.Utils.convertReadableMapToUploaderOptions
|
|
18
19
|
import com.reactnativecompressor.Video.VideoMain
|
|
19
20
|
|
|
20
21
|
class CompressorModule(private val reactContext: ReactApplicationContext) : CompressorSpec(reactContext) {
|
|
21
22
|
private val imageMain: ImageMain = ImageMain(reactContext)
|
|
22
23
|
private val videoMain: VideoMain = VideoMain(reactContext)
|
|
23
24
|
private val audioMain: AudioMain = AudioMain(reactContext)
|
|
25
|
+
private val uploader: Uploader = Uploader(reactContext)
|
|
24
26
|
private val videoThumbnail: CreateVideoThumbnailClass = CreateVideoThumbnailClass(reactContext)
|
|
25
27
|
|
|
26
28
|
override fun initialize() {
|
|
@@ -120,7 +122,7 @@ class CompressorModule(private val reactContext: ReactApplicationContext) : Comp
|
|
|
120
122
|
fileUrl: String,
|
|
121
123
|
options: ReadableMap,
|
|
122
124
|
promise: Promise) {
|
|
123
|
-
|
|
125
|
+
uploader.upload(fileUrl, options, reactContext, promise)
|
|
124
126
|
}
|
|
125
127
|
|
|
126
128
|
@ReactMethod
|
|
@@ -1,117 +1,189 @@
|
|
|
1
1
|
package com.reactnativecompressor.Utils
|
|
2
2
|
|
|
3
|
+
import android.annotation.SuppressLint
|
|
4
|
+
import android.content.ContentResolver
|
|
5
|
+
import android.net.Uri
|
|
3
6
|
import android.util.Log
|
|
7
|
+
import android.webkit.MimeTypeMap
|
|
4
8
|
import com.facebook.react.bridge.Arguments
|
|
5
9
|
import com.facebook.react.bridge.Promise
|
|
6
10
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
7
11
|
import com.facebook.react.bridge.ReadableMap
|
|
8
|
-
import io.github.lizhangqu.coreprogress.ProgressHelper
|
|
9
|
-
import io.github.lizhangqu.coreprogress.ProgressUIListener
|
|
10
12
|
import okhttp3.Call
|
|
11
13
|
import okhttp3.Callback
|
|
12
|
-
import okhttp3.
|
|
14
|
+
import okhttp3.Headers
|
|
13
15
|
import okhttp3.MediaType.Companion.toMediaTypeOrNull
|
|
16
|
+
import okhttp3.MultipartBody
|
|
14
17
|
import okhttp3.OkHttpClient
|
|
15
|
-
import okhttp3.Request
|
|
18
|
+
import okhttp3.Request
|
|
16
19
|
import okhttp3.RequestBody
|
|
20
|
+
import okhttp3.RequestBody.Companion.asRequestBody
|
|
17
21
|
import okhttp3.Response
|
|
18
22
|
import java.io.File
|
|
19
23
|
import java.io.IOException
|
|
20
|
-
import java.
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
24
|
+
import java.net.URLConnection
|
|
25
|
+
import java.util.concurrent.TimeUnit
|
|
26
|
+
import java.util.regex.Pattern
|
|
27
|
+
|
|
28
|
+
class Uploader(private val reactContext: ReactApplicationContext) {
|
|
29
|
+
val TAG = "asyncTaskUploader"
|
|
30
|
+
var client: OkHttpClient? = null
|
|
31
|
+
val MIN_EVENT_DT_MS: Long = 100
|
|
32
|
+
|
|
33
|
+
fun upload(fileUriString: String, _options: ReadableMap, reactContext: ReactApplicationContext, promise: Promise) {
|
|
34
|
+
val options:UploaderOptions=convertReadableMapToUploaderOptions(_options)
|
|
35
|
+
val url = options.url
|
|
36
|
+
val uuid = options.uuid
|
|
37
|
+
val progressListener: CountingRequestListener = object : CountingRequestListener {
|
|
38
|
+
private var mLastUpdate: Long = -1
|
|
39
|
+
override fun onProgress(bytesWritten: Long, contentLength: Long) {
|
|
40
|
+
val currentTime = System.currentTimeMillis()
|
|
41
|
+
|
|
42
|
+
// Throttle events. Sending too many events will block the JS event loop.
|
|
43
|
+
// Make sure to send the last event when we're at 100%.
|
|
44
|
+
if (currentTime > mLastUpdate + MIN_EVENT_DT_MS || bytesWritten == contentLength) {
|
|
45
|
+
mLastUpdate = currentTime
|
|
46
|
+
EventEmitterHandler.sendUploadProgressEvent(bytesWritten,contentLength,uuid)
|
|
43
47
|
}
|
|
44
48
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
override fun onUIProgressFinish() {
|
|
66
|
-
super.onUIProgressFinish()
|
|
67
|
-
Log.d(TAG, "onUIProgressFinish:")
|
|
68
|
-
}
|
|
69
|
-
})
|
|
70
|
-
builder.put(requestBody)
|
|
71
|
-
val call = okHttpClient.newCall(builder.build())
|
|
72
|
-
call.enqueue(object : Callback {
|
|
73
|
-
override fun onFailure(call: Call, e: IOException) {
|
|
74
|
-
Log.d(TAG, "=============onFailure===============")
|
|
75
|
-
promise.reject("")
|
|
76
|
-
e.printStackTrace()
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
@Throws(IOException::class)
|
|
80
|
-
override fun onResponse(call: Call, response: Response) {
|
|
81
|
-
Log.d(TAG, "=============onResponse===============")
|
|
82
|
-
Log.d(TAG, "request headers:" + response.request.headers)
|
|
83
|
-
Log.d(TAG, "response code:" + response.code)
|
|
84
|
-
Log.d(TAG, "response headers:" + response.headers)
|
|
85
|
-
Log.d(TAG, "response body:" + response.body!!.string())
|
|
86
|
-
val param = Arguments.createMap()
|
|
87
|
-
param.putInt("status", response.code)
|
|
88
|
-
promise.resolve(param)
|
|
89
|
-
}
|
|
49
|
+
}
|
|
50
|
+
val request = createUploadRequest(
|
|
51
|
+
url, fileUriString, options
|
|
52
|
+
) { requestBody -> CountingRequestBody(requestBody, progressListener) }
|
|
53
|
+
|
|
54
|
+
okHttpClient?.let {
|
|
55
|
+
it.newCall(request).enqueue(object : Callback {
|
|
56
|
+
override fun onFailure(call: Call, e: IOException) {
|
|
57
|
+
Log.e(TAG, e.message.toString())
|
|
58
|
+
promise.reject(TAG, e.message, e)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
override fun onResponse(call: Call, response: Response) {
|
|
62
|
+
val param = Arguments.createMap()
|
|
63
|
+
param.putInt("status", response.code)
|
|
64
|
+
param.putString("body", response.body?.string())
|
|
65
|
+
param.putMap("headers", translateHeaders(response.headers))
|
|
66
|
+
response.close()
|
|
67
|
+
promise.resolve(param)
|
|
68
|
+
}
|
|
90
69
|
})
|
|
70
|
+
} ?: run {
|
|
71
|
+
promise.reject(UploaderOkHttpNullException())
|
|
72
|
+
}
|
|
73
|
+
|
|
91
74
|
}
|
|
92
|
-
}
|
|
93
75
|
|
|
76
|
+
@get:Synchronized
|
|
77
|
+
private val okHttpClient: OkHttpClient?
|
|
78
|
+
get() {
|
|
79
|
+
if (client == null) {
|
|
80
|
+
val builder = OkHttpClient.Builder()
|
|
81
|
+
.connectTimeout(60, TimeUnit.SECONDS)
|
|
82
|
+
.readTimeout(60, TimeUnit.SECONDS)
|
|
83
|
+
.writeTimeout(60, TimeUnit.SECONDS)
|
|
84
|
+
client = builder.build()
|
|
85
|
+
}
|
|
86
|
+
return client
|
|
87
|
+
}
|
|
94
88
|
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
89
|
+
private fun slashifyFilePath(path: String?): String? {
|
|
90
|
+
return if (path == null) {
|
|
91
|
+
null
|
|
92
|
+
} else if (path.startsWith("file:///")) {
|
|
93
|
+
path
|
|
94
|
+
} else {
|
|
95
|
+
// Ensure leading schema with a triple slash
|
|
96
|
+
Pattern.compile("^file:/*").matcher(path).replaceAll("file:///")
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
@Throws(IOException::class)
|
|
101
|
+
private fun createUploadRequest(url: String, fileUriString: String, options: UploaderOptions, decorator: RequestBodyDecorator): Request {
|
|
102
|
+
val fileUri = Uri.parse(slashifyFilePath(fileUriString))
|
|
103
|
+
fileUri.checkIfFileExists()
|
|
104
|
+
|
|
105
|
+
val requestBuilder = Request.Builder().url(url)
|
|
106
|
+
options.headers?.let {
|
|
107
|
+
it.forEach { (key, value) -> requestBuilder.addHeader(key, value) }
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
val body = createRequestBody(options, decorator, fileUri.toFile())
|
|
111
|
+
return options.httpMethod.let { requestBuilder.method(it.value, body).build() }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
@SuppressLint("NewApi")
|
|
115
|
+
private fun createRequestBody(options: UploaderOptions, decorator: RequestBodyDecorator, file: File): RequestBody {
|
|
116
|
+
return when (options.uploadType) {
|
|
117
|
+
UploadType.BINARY_CONTENT -> {
|
|
118
|
+
val mimeType: String? = if (options.mimeType?.isNotEmpty() == true) {
|
|
119
|
+
options.mimeType
|
|
120
|
+
} else {
|
|
121
|
+
getContentType(reactContext, file) ?: "application/octet-stream"
|
|
122
|
+
}
|
|
123
|
+
val contentType = mimeType?.toMediaTypeOrNull()
|
|
124
|
+
decorator.decorate(file.asRequestBody(contentType))
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
UploadType.MULTIPART -> {
|
|
128
|
+
val bodyBuilder = MultipartBody.Builder().setType(MultipartBody.FORM)
|
|
129
|
+
options.parameters?.let {
|
|
130
|
+
(it as Map<String, Any>)
|
|
131
|
+
.forEach { (key, value) -> bodyBuilder.addFormDataPart(key, value.toString()) }
|
|
112
132
|
}
|
|
133
|
+
val mimeType: String = options.mimeType ?: URLConnection.guessContentTypeFromName(file.name)
|
|
134
|
+
|
|
135
|
+
val fieldName = options.fieldName ?: file.name
|
|
136
|
+
bodyBuilder.addFormDataPart(fieldName, file.name, decorator.decorate(file.asRequestBody(mimeType.toMediaTypeOrNull())))
|
|
137
|
+
bodyBuilder.build()
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
fun getContentType(context: ReactApplicationContext, file: File): String? {
|
|
143
|
+
val contentResolver: ContentResolver = context.contentResolver
|
|
144
|
+
val fileUri = Uri.fromFile(file)
|
|
145
|
+
|
|
146
|
+
// Try to get the MIME type from the ContentResolver
|
|
147
|
+
val mimeType = contentResolver.getType(fileUri)
|
|
148
|
+
|
|
149
|
+
// If the ContentResolver couldn't determine the MIME type, try to infer it from the file extension
|
|
150
|
+
if (mimeType == null) {
|
|
151
|
+
val fileExtension = MimeTypeMap.getFileExtensionFromUrl(fileUri.toString())
|
|
152
|
+
if (fileExtension != null) {
|
|
153
|
+
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension.toLowerCase())
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
return mimeType
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@Throws(IOException::class)
|
|
161
|
+
private fun Uri.checkIfFileExists() {
|
|
162
|
+
val file = this.toFile()
|
|
163
|
+
if (!file.exists()) {
|
|
164
|
+
throw IOException("Directory for '${file.path}' doesn't exist.")
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// extension functions of Uri class
|
|
169
|
+
private fun Uri.toFile() = if (this.path != null) {
|
|
170
|
+
File(this.path!!)
|
|
171
|
+
} else {
|
|
172
|
+
throw IOException("Invalid Uri: $this")
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
private fun translateHeaders(headers: Headers): ReadableMap {
|
|
176
|
+
val responseHeaders = Arguments.createMap()
|
|
177
|
+
for (i in 0 until headers.size) {
|
|
178
|
+
val headerName = headers.name(i)
|
|
179
|
+
// multiple values for the same header
|
|
180
|
+
if (responseHeaders.hasKey(headerName)) {
|
|
181
|
+
val existingValue = responseHeaders.getString(headerName)
|
|
182
|
+
responseHeaders.putString(headerName, "$existingValue, ${headers.value(i)}")
|
|
183
|
+
} else {
|
|
184
|
+
responseHeaders.putString(headerName, headers.value(i))
|
|
113
185
|
}
|
|
114
|
-
return options
|
|
115
186
|
}
|
|
187
|
+
return responseHeaders
|
|
116
188
|
}
|
|
117
189
|
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
package com.reactnativecompressor.Utils
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReadableMap
|
|
4
|
+
import okhttp3.RequestBody
|
|
5
|
+
import okio.Buffer
|
|
6
|
+
import okio.BufferedSink
|
|
7
|
+
import okio.ForwardingSink
|
|
8
|
+
import okio.IOException
|
|
9
|
+
import okio.Sink
|
|
10
|
+
import okio.buffer
|
|
11
|
+
|
|
12
|
+
interface Record
|
|
13
|
+
annotation class Field(val key: String = "")
|
|
14
|
+
interface Enumerable
|
|
15
|
+
|
|
16
|
+
@FunctionalInterface
|
|
17
|
+
fun interface RequestBodyDecorator {
|
|
18
|
+
fun decorate(requestBody: RequestBody): RequestBody
|
|
19
|
+
}
|
|
20
|
+
enum class EncodingType(val value: String) : Enumerable {
|
|
21
|
+
UTF8("utf8"),
|
|
22
|
+
BASE64("base64")
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
enum class UploadType(val value: Int) : Enumerable {
|
|
26
|
+
BINARY_CONTENT(0),
|
|
27
|
+
MULTIPART(1)
|
|
28
|
+
}
|
|
29
|
+
data class UploaderOptions(
|
|
30
|
+
val headers: Map<String, String>?,
|
|
31
|
+
val httpMethod: HttpMethod = HttpMethod.POST,
|
|
32
|
+
val uploadType: UploadType,
|
|
33
|
+
val fieldName: String?,
|
|
34
|
+
val mimeType: String?,
|
|
35
|
+
val parameters: Map<String, String>?,
|
|
36
|
+
val uuid:String,
|
|
37
|
+
val url:String
|
|
38
|
+
) : Record
|
|
39
|
+
|
|
40
|
+
enum class HttpMethod(val value: String) : Enumerable {
|
|
41
|
+
POST("POST"),
|
|
42
|
+
PUT("PUT"),
|
|
43
|
+
PATCH("PATCH")
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
internal class UploaderOkHttpNullException :
|
|
47
|
+
CodedException("okHttpClient is null")
|
|
48
|
+
internal class CookieHandlerNotFoundException :
|
|
49
|
+
CodedException("Failed to find CookieHandler")
|
|
50
|
+
interface CodedThrowable {
|
|
51
|
+
val code: String?
|
|
52
|
+
val message: String?
|
|
53
|
+
}
|
|
54
|
+
abstract class CodedException : Exception, CodedThrowable {
|
|
55
|
+
constructor(message: String?) : super(message)
|
|
56
|
+
constructor(cause: Throwable?) : super(cause)
|
|
57
|
+
constructor(message: String?, cause: Throwable?) : super(message, cause)
|
|
58
|
+
|
|
59
|
+
override val code: String
|
|
60
|
+
get() = "ERR_UNSPECIFIED_ANDROID_EXCEPTION"
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@FunctionalInterface
|
|
64
|
+
interface CountingRequestListener {
|
|
65
|
+
fun onProgress(bytesWritten: Long, contentLength: Long)
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@FunctionalInterface
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
private class CountingSink(
|
|
72
|
+
sink: Sink,
|
|
73
|
+
private val requestBody: RequestBody,
|
|
74
|
+
private val progressListener: CountingRequestListener
|
|
75
|
+
) : ForwardingSink(sink) {
|
|
76
|
+
private var bytesWritten = 0L
|
|
77
|
+
|
|
78
|
+
override fun write(source: Buffer, byteCount: Long) {
|
|
79
|
+
super.write(source, byteCount)
|
|
80
|
+
|
|
81
|
+
bytesWritten += byteCount
|
|
82
|
+
progressListener.onProgress(bytesWritten, requestBody.contentLength())
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
class CountingRequestBody(
|
|
87
|
+
private val requestBody: RequestBody,
|
|
88
|
+
private val progressListener: CountingRequestListener
|
|
89
|
+
) : RequestBody() {
|
|
90
|
+
override fun contentType() = requestBody.contentType()
|
|
91
|
+
|
|
92
|
+
@Throws(IOException::class)
|
|
93
|
+
override fun contentLength() = requestBody.contentLength()
|
|
94
|
+
|
|
95
|
+
override fun writeTo(sink: BufferedSink) {
|
|
96
|
+
val countingSink = CountingSink(sink, this, progressListener)
|
|
97
|
+
|
|
98
|
+
val bufferedSink = countingSink.buffer()
|
|
99
|
+
requestBody.writeTo(bufferedSink)
|
|
100
|
+
bufferedSink.flush()
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
fun convertReadableMapToUploaderOptions(options: ReadableMap): UploaderOptions {
|
|
106
|
+
val headers = options.getMap("headers")?.toHashMap() as? Map<String, String>
|
|
107
|
+
val httpMethod = HttpMethod.valueOf(options.getString("httpMethod") ?: "POST")
|
|
108
|
+
val uploadTypeInt = options.getInt("uploadType")
|
|
109
|
+
val uploadType = when (uploadTypeInt) {
|
|
110
|
+
UploadType.BINARY_CONTENT.value -> UploadType.BINARY_CONTENT
|
|
111
|
+
UploadType.MULTIPART.value -> UploadType.MULTIPART
|
|
112
|
+
else -> UploadType.BINARY_CONTENT // Provide a default value or handle the case as needed
|
|
113
|
+
}
|
|
114
|
+
val fieldName = options.getString("fieldName")?: "file"
|
|
115
|
+
val mimeType = options.getString("mimeType")?: ""
|
|
116
|
+
val parameters = options.getMap("parameters")?.toHashMap() as? Map<String, String>
|
|
117
|
+
val uuid = options.getString("uuid") ?: ""
|
|
118
|
+
val url = options.getString("url") ?: ""
|
|
119
|
+
|
|
120
|
+
return UploaderOptions(headers, httpMethod, uploadType, fieldName, mimeType, parameters, uuid, url)
|
|
121
|
+
}
|
package/ios/Utils/Uploader.swift
CHANGED
|
@@ -6,6 +6,13 @@
|
|
|
6
6
|
//
|
|
7
7
|
|
|
8
8
|
import Foundation
|
|
9
|
+
import MobileCoreServices
|
|
10
|
+
|
|
11
|
+
enum UploaderUploadType: Int {
|
|
12
|
+
case UploaderInvalidType = -1
|
|
13
|
+
case UploaderBinaryContent = 0
|
|
14
|
+
case UploaderMultipart = 1
|
|
15
|
+
}
|
|
9
16
|
|
|
10
17
|
struct UploadError: Error {
|
|
11
18
|
private let message: String
|
|
@@ -44,11 +51,19 @@ class Uploader : NSObject, URLSessionTaskDelegate{
|
|
|
44
51
|
return
|
|
45
52
|
}
|
|
46
53
|
|
|
47
|
-
guard let
|
|
54
|
+
guard let localFile = URL(string: fileUrl) else{
|
|
48
55
|
let uploadError = UploadError(message: "invalid file url")
|
|
49
56
|
reject("Failed", "Upload Failed", uploadError)
|
|
50
57
|
return
|
|
51
58
|
}
|
|
59
|
+
|
|
60
|
+
let fieldName = options["fieldName"] as? String ?? "file"
|
|
61
|
+
let mimeType = options["mimeType"] as? String ?? ""
|
|
62
|
+
|
|
63
|
+
let parameters = options["parameters"] as? [String: String]
|
|
64
|
+
|
|
65
|
+
let uploadType = options["uploadType"] as? Int ?? 0
|
|
66
|
+
|
|
52
67
|
|
|
53
68
|
let headers = options["headers"] as? [String: String] ?? [:]
|
|
54
69
|
|
|
@@ -61,11 +76,30 @@ class Uploader : NSObject, URLSessionTaskDelegate{
|
|
|
61
76
|
|
|
62
77
|
uploadResolvers[uuid] = resolve
|
|
63
78
|
uploadRejectors[uuid] = reject
|
|
64
|
-
|
|
79
|
+
|
|
80
|
+
let type:UploaderUploadType=self.getUploadType(from: uploadType)
|
|
81
|
+
|
|
65
82
|
let config = URLSessionConfiguration.background(withIdentifier: uuid)
|
|
66
83
|
let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)
|
|
67
|
-
|
|
68
|
-
|
|
84
|
+
var task:URLSessionUploadTask!;
|
|
85
|
+
if type == .UploaderBinaryContent {
|
|
86
|
+
task = session.uploadTask(with: request, fromFile: localFile)
|
|
87
|
+
} else if type == .UploaderMultipart {
|
|
88
|
+
let boundaryString = UUID().uuidString
|
|
89
|
+
let data = try? createMultipartBody(boundary: boundaryString, sourceUrl:localFile,parameters:parameters,fieldName:fieldName,mimeType:mimeType)
|
|
90
|
+
|
|
91
|
+
request.setValue("multipart/form-data; boundary=\(boundaryString)", forHTTPHeaderField: "Content-Type")
|
|
92
|
+
request.httpBody = data
|
|
93
|
+
|
|
94
|
+
task=session.uploadTask(withStreamedRequest: request)
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
let errorMessage = String(format: "Invalid upload type: '%@'.", options["uploadType"] as? String ?? "")
|
|
98
|
+
reject("ERR_FILESYSTEM_INVALID_UPLOAD_TYPE", errorMessage, nil)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
task.resume()
|
|
102
|
+
|
|
69
103
|
}
|
|
70
104
|
|
|
71
105
|
func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
|
|
@@ -96,4 +130,89 @@ class Uploader : NSObject, URLSessionTaskDelegate{
|
|
|
96
130
|
guard let uuid = session.configuration.identifier else {return}
|
|
97
131
|
EventEmitterHandler.emituploadProgress(uuid,totalBytesSent: totalBytesSent,totalBytesExpectedToSend: totalBytesExpectedToSend)
|
|
98
132
|
}
|
|
133
|
+
|
|
134
|
+
func headersForMultipartParams(_ params: [String: String]?, boundary: String) -> String {
|
|
135
|
+
guard let params else {
|
|
136
|
+
return ""
|
|
137
|
+
}
|
|
138
|
+
return params.map { (key: String, value: String) in
|
|
139
|
+
"""
|
|
140
|
+
--\(boundary)
|
|
141
|
+
Content-Disposition: form-data; name="\(key)"
|
|
142
|
+
|
|
143
|
+
\(value)
|
|
144
|
+
"""
|
|
145
|
+
}
|
|
146
|
+
.joined()
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
func createMultipartBody(boundary: String, sourceUrl: URL, parameters: [String: String]? = nil, fieldName: String? = nil, mimeType: String? = nil) throws -> Data {
|
|
150
|
+
var body = Data()
|
|
151
|
+
|
|
152
|
+
// Add boundary
|
|
153
|
+
let boundaryPrefix = "--\(boundary)\r\n"
|
|
154
|
+
body.append(boundaryPrefix.data(using: .utf8)!)
|
|
155
|
+
|
|
156
|
+
// Add content disposition for the file
|
|
157
|
+
var contentDisposition = "Content-Disposition: form-data; name=\"file\"; filename=\"\(sourceUrl.lastPathComponent)\"\r\n"
|
|
158
|
+
if let fieldName = fieldName {
|
|
159
|
+
contentDisposition = "Content-Disposition: form-data; name=\"\(fieldName)\"; filename=\"\(sourceUrl.lastPathComponent)\"\r\n"
|
|
160
|
+
}
|
|
161
|
+
body.append(contentDisposition.data(using: .utf8)!)
|
|
162
|
+
|
|
163
|
+
// Add optional MIME type
|
|
164
|
+
if let mimeType = mimeType {
|
|
165
|
+
let contentType = "Content-Type: \(mimeType)\r\n"
|
|
166
|
+
body.append(contentType.data(using: .utf8)!)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
// Add blank line
|
|
170
|
+
body.append("\r\n".data(using: .utf8)!)
|
|
171
|
+
|
|
172
|
+
// Add file data
|
|
173
|
+
let fileData = try Data(contentsOf: sourceUrl)
|
|
174
|
+
body.append(fileData)
|
|
175
|
+
|
|
176
|
+
// Add parameters, if any
|
|
177
|
+
if let parameters = parameters {
|
|
178
|
+
for (key, value) in parameters {
|
|
179
|
+
body.append("\r\n".data(using: .utf8)!)
|
|
180
|
+
body.append(boundaryPrefix.data(using: .utf8)!)
|
|
181
|
+
let parameterContentDisposition = "Content-Disposition: form-data; name=\"\(key)\"\r\n\r\n"
|
|
182
|
+
body.append(parameterContentDisposition.data(using: .utf8)!)
|
|
183
|
+
body.append(value.data(using: .utf8)!)
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Add closing boundary
|
|
188
|
+
let closingBoundary = "\r\n--\(boundary)--\r\n"
|
|
189
|
+
body.append(closingBoundary.data(using: .utf8)!)
|
|
190
|
+
|
|
191
|
+
return body
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
func findMimeType(forAttachment attachment: URL) -> String {
|
|
195
|
+
if let identifier = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, attachment.pathExtension as CFString, nil)?.takeRetainedValue() {
|
|
196
|
+
if let type = UTTypeCopyPreferredTagWithClass(identifier, kUTTagClassMIMEType)?.takeRetainedValue() {
|
|
197
|
+
return type as String
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
return "application/octet-stream"
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
func getUploadType(from type: Int?) -> UploaderUploadType {
|
|
204
|
+
guard let typeValue = type else {
|
|
205
|
+
return .UploaderInvalidType
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
switch typeValue {
|
|
209
|
+
case UploaderUploadType.UploaderBinaryContent.rawValue,
|
|
210
|
+
UploaderUploadType.UploaderMultipart.rawValue:
|
|
211
|
+
return UploaderUploadType(rawValue: typeValue) ?? .UploaderInvalidType
|
|
212
|
+
default:
|
|
213
|
+
return .UploaderInvalidType
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
|
|
99
218
|
}
|
package/lib/commonjs/index.js
CHANGED
|
@@ -15,6 +15,18 @@ Object.defineProperty(exports, "Image", {
|
|
|
15
15
|
return _Image.default;
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
|
+
Object.defineProperty(exports, "UploadType", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _utils.UploadType;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "UploaderHttpMethod", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () {
|
|
27
|
+
return _utils.UploaderHttpMethod;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
18
30
|
Object.defineProperty(exports, "Video", {
|
|
19
31
|
enumerable: true,
|
|
20
32
|
get: function () {
|
|
@@ -100,7 +112,9 @@ var _default = {
|
|
|
100
112
|
backgroundUpload: _utils.backgroundUpload,
|
|
101
113
|
createVideoThumbnail: _utils.createVideoThumbnail,
|
|
102
114
|
clearCache: _utils.clearCache,
|
|
103
|
-
download: _utils.download
|
|
115
|
+
download: _utils.download,
|
|
116
|
+
UploadType: _utils.UploadType,
|
|
117
|
+
UploaderHttpMethod: _utils.UploaderHttpMethod
|
|
104
118
|
};
|
|
105
119
|
exports.default = _default;
|
|
106
120
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_Video","_interopRequireDefault","require","_Audio","_Image","_utils","obj","__esModule","default","_default","Video","Audio","Image","getDetails","uuidv4","generateFilePath","getRealPath","getVideoMetaData","getFileSize","backgroundUpload","createVideoThumbnail","clearCache","download","exports"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":"
|
|
1
|
+
{"version":3,"names":["_Video","_interopRequireDefault","require","_Audio","_Image","_utils","obj","__esModule","default","_default","Video","Audio","Image","getDetails","uuidv4","generateFilePath","getRealPath","getVideoMetaData","getFileSize","backgroundUpload","createVideoThumbnail","clearCache","download","UploadType","UploaderHttpMethod","exports"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAAA,MAAA,GAAAC,sBAAA,CAAAC,OAAA;AAEA,IAAAC,MAAA,GAAAF,sBAAA,CAAAC,OAAA;AACA,IAAAE,MAAA,GAAAH,sBAAA,CAAAC,OAAA;AACA,IAAAG,MAAA,GAAAH,OAAA;AAaiB,SAAAD,uBAAAK,GAAA,WAAAA,GAAA,IAAAA,GAAA,CAAAC,UAAA,GAAAD,GAAA,KAAAE,OAAA,EAAAF,GAAA;AAAA,IAAAG,QAAA,GAqBF;EACbC,KAAK,EAALA,cAAK;EACLC,KAAK,EAALA,cAAK;EACLC,KAAK,EAALA,cAAK;EACLC,UAAU,EAAVA,iBAAU;EACVC,MAAM,EAANA,aAAM;EACNC,gBAAgB,EAAhBA,uBAAgB;EAChBC,WAAW,EAAXA,kBAAW;EACXC,gBAAgB,EAAhBA,uBAAgB;EAChBC,WAAW,EAAXA,kBAAW;EACXC,gBAAgB,EAAhBA,uBAAgB;EAChBC,oBAAoB,EAApBA,2BAAoB;EACpBC,UAAU,EAAVA,iBAAU;EACVC,QAAQ,EAARA,eAAQ;EACRC,UAAU,EAAVA,iBAAU;EACVC,kBAAkB,EAAlBA;AACF,CAAC;AAAAC,OAAA,CAAAjB,OAAA,GAAAC,QAAA"}
|
|
@@ -3,11 +3,24 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.backgroundUpload = void 0;
|
|
6
|
+
exports.backgroundUpload = exports.UploaderHttpMethod = exports.UploadType = void 0;
|
|
7
7
|
var _reactNative = require("react-native");
|
|
8
8
|
var _Main = require("../Main");
|
|
9
9
|
var _ = require(".");
|
|
10
10
|
const CompressEventEmitter = new _reactNative.NativeEventEmitter(_Main.Compressor);
|
|
11
|
+
let UploadType = /*#__PURE__*/function (UploadType) {
|
|
12
|
+
UploadType[UploadType["BINARY_CONTENT"] = 0] = "BINARY_CONTENT";
|
|
13
|
+
UploadType[UploadType["MULTIPART"] = 1] = "MULTIPART";
|
|
14
|
+
return UploadType;
|
|
15
|
+
}({});
|
|
16
|
+
exports.UploadType = UploadType;
|
|
17
|
+
let UploaderHttpMethod = /*#__PURE__*/function (UploaderHttpMethod) {
|
|
18
|
+
UploaderHttpMethod["POST"] = "POST";
|
|
19
|
+
UploaderHttpMethod["PUT"] = "PUT";
|
|
20
|
+
UploaderHttpMethod["PATCH"] = "PATCH";
|
|
21
|
+
return UploaderHttpMethod;
|
|
22
|
+
}({});
|
|
23
|
+
exports.UploaderHttpMethod = UploaderHttpMethod;
|
|
11
24
|
const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
12
25
|
const uuid = (0, _.uuidv4)();
|
|
13
26
|
let subscription;
|
|
@@ -26,6 +39,8 @@ const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
|
26
39
|
uuid,
|
|
27
40
|
method: options.httpMethod,
|
|
28
41
|
headers: options.headers,
|
|
42
|
+
uploadType: options.uploadType,
|
|
43
|
+
...options,
|
|
29
44
|
url
|
|
30
45
|
});
|
|
31
46
|
return result;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_reactNative","require","_Main","_","CompressEventEmitter","NativeEventEmitter","Compressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","uuidv4","subscription","addListener","event","data","written","total","Platform","OS","includes","replace","result","upload","method","httpMethod","headers","
|
|
1
|
+
{"version":3,"names":["_reactNative","require","_Main","_","CompressEventEmitter","NativeEventEmitter","Compressor","UploadType","exports","UploaderHttpMethod","backgroundUpload","url","fileUrl","options","onProgress","uuid","uuidv4","subscription","addListener","event","data","written","total","Platform","OS","includes","replace","result","upload","method","httpMethod","headers","uploadType","remove"],"sourceRoot":"../../../src","sources":["utils/Uploader.tsx"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAEA,IAAAC,KAAA,GAAAD,OAAA;AAEA,IAAAE,CAAA,GAAAF,OAAA;AADA,MAAMG,oBAAoB,GAAG,IAAIC,+BAAkB,CAACC,gBAAU,CAAC;AAAC,IAEpDC,UAAU,0BAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAAAC,OAAA,CAAAD,UAAA,GAAAA,UAAA;AAAA,IAKVE,kBAAkB,0BAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAA,OAAlBA,kBAAkB;AAAA;AAAAD,OAAA,CAAAC,kBAAA,GAAAA,kBAAA;AA8BvB,MAAMC,gBAAgB,GAAG,MAAAA,CAC9BC,GAAW,EACXC,OAAe,EACfC,OAAwB,EACxBC,UAAqD,KACpC;EACjB,MAAMC,IAAI,GAAG,IAAAC,QAAM,EAAC,CAAC;EACrB,IAAIC,YAAqC;EACzC,IAAI;IACF,IAAIH,UAAU,EAAE;MACdG,YAAY,GAAGb,oBAAoB,CAACc,WAAW,CAC7C,gBAAgB,EACfC,KAAU,IAAK;QACd,IAAIA,KAAK,CAACJ,IAAI,KAAKA,IAAI,EAAE;UACvBD,UAAU,CAACK,KAAK,CAACC,IAAI,CAACC,OAAO,EAAEF,KAAK,CAACC,IAAI,CAACE,KAAK,CAAC;QAClD;MACF,CACF,CAAC;IACH;IACA,IAAIC,qBAAQ,CAACC,EAAE,KAAK,SAAS,IAAIZ,OAAO,CAACa,QAAQ,CAAC,SAAS,CAAC,EAAE;MAC5Db,OAAO,GAAGA,OAAO,CAACc,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;IAC1C;IACA,MAAMC,MAAM,GAAG,MAAMrB,gBAAU,CAACsB,MAAM,CAAChB,OAAO,EAAE;MAC9CG,IAAI;MACJc,MAAM,EAAEhB,OAAO,CAACiB,UAAU;MAC1BC,OAAO,EAAElB,OAAO,CAACkB,OAAO;MACxBC,UAAU,EAAEnB,OAAO,CAACmB,UAAU;MAC9B,GAAGnB,OAAO;MACVF;IACF,CAAC,CAAC;IACF,OAAOgB,MAAM;EACf,CAAC,SAAS;IACR;IACA,IAAIV,YAAY,EAAE;MAChBA,YAAY,CAACgB,MAAM,CAAC,CAAC;IACvB;EACF;AACF,CAAC;AAACzB,OAAA,CAAAE,gBAAA,GAAAA,gBAAA"}
|
package/lib/module/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import Video from './Video';
|
|
2
2
|
import Audio from './Audio';
|
|
3
3
|
import Image from './Image';
|
|
4
|
-
import { getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, getFileSize, backgroundUpload, createVideoThumbnail, download, clearCache } from './utils';
|
|
4
|
+
import { getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, getFileSize, backgroundUpload, createVideoThumbnail, download, clearCache, UploadType, UploaderHttpMethod } from './utils';
|
|
5
5
|
export { Video, Audio, Image, backgroundUpload, download,
|
|
6
6
|
//type
|
|
7
|
-
getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, createVideoThumbnail, clearCache, getFileSize };
|
|
7
|
+
getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, createVideoThumbnail, clearCache, getFileSize, UploadType, UploaderHttpMethod };
|
|
8
8
|
export default {
|
|
9
9
|
Video,
|
|
10
10
|
Audio,
|
|
@@ -18,6 +18,8 @@ export default {
|
|
|
18
18
|
backgroundUpload,
|
|
19
19
|
createVideoThumbnail,
|
|
20
20
|
clearCache,
|
|
21
|
-
download
|
|
21
|
+
download,
|
|
22
|
+
UploadType,
|
|
23
|
+
UploaderHttpMethod
|
|
22
24
|
};
|
|
23
25
|
//# sourceMappingURL=index.js.map
|
package/lib/module/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["Video","Audio","Image","getDetails","uuidv4","generateFilePath","getRealPath","getVideoMetaData","getFileSize","backgroundUpload","createVideoThumbnail","download","clearCache"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":"AAAA,OAAOA,KAAK,MAAM,SAAS;AAE3B,OAAOC,KAAK,MAAM,SAAS;AAC3B,OAAOC,KAAK,MAAM,SAAS;AAC3B,SACEC,UAAU,EACVC,MAAM,EACNC,gBAAgB,EAChBC,WAAW,EACXC,gBAAgB,EAChBC,WAAW,EACXC,gBAAgB,EAChBC,oBAAoB,EACpBC,QAAQ,EACRC,UAAU,
|
|
1
|
+
{"version":3,"names":["Video","Audio","Image","getDetails","uuidv4","generateFilePath","getRealPath","getVideoMetaData","getFileSize","backgroundUpload","createVideoThumbnail","download","clearCache","UploadType","UploaderHttpMethod"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":"AAAA,OAAOA,KAAK,MAAM,SAAS;AAE3B,OAAOC,KAAK,MAAM,SAAS;AAC3B,OAAOC,KAAK,MAAM,SAAS;AAC3B,SACEC,UAAU,EACVC,MAAM,EACNC,gBAAgB,EAChBC,WAAW,EACXC,gBAAgB,EAChBC,WAAW,EACXC,gBAAgB,EAChBC,oBAAoB,EACpBC,QAAQ,EACRC,UAAU,EACVC,UAAU,EACVC,kBAAkB,QACb,SAAS;AAEhB,SACEd,KAAK,EACLC,KAAK,EACLC,KAAK,EACLO,gBAAgB,EAChBE,QAAQ;AACR;AACAR,UAAU,EACVC,MAAM,EACNC,gBAAgB,EAChBC,WAAW,EACXC,gBAAgB,EAChBG,oBAAoB,EACpBE,UAAU,EACVJ,WAAW,EACXK,UAAU,EACVC,kBAAkB;AAGpB,eAAe;EACbd,KAAK;EACLC,KAAK;EACLC,KAAK;EACLC,UAAU;EACVC,MAAM;EACNC,gBAAgB;EAChBC,WAAW;EACXC,gBAAgB;EAChBC,WAAW;EACXC,gBAAgB;EAChBC,oBAAoB;EACpBE,UAAU;EACVD,QAAQ;EACRE,UAAU;EACVC;AACF,CAAC"}
|
|
@@ -2,6 +2,17 @@ import { NativeEventEmitter, Platform } from 'react-native';
|
|
|
2
2
|
import { Compressor } from '../Main';
|
|
3
3
|
const CompressEventEmitter = new NativeEventEmitter(Compressor);
|
|
4
4
|
import { uuidv4 } from '.';
|
|
5
|
+
export let UploadType = /*#__PURE__*/function (UploadType) {
|
|
6
|
+
UploadType[UploadType["BINARY_CONTENT"] = 0] = "BINARY_CONTENT";
|
|
7
|
+
UploadType[UploadType["MULTIPART"] = 1] = "MULTIPART";
|
|
8
|
+
return UploadType;
|
|
9
|
+
}({});
|
|
10
|
+
export let UploaderHttpMethod = /*#__PURE__*/function (UploaderHttpMethod) {
|
|
11
|
+
UploaderHttpMethod["POST"] = "POST";
|
|
12
|
+
UploaderHttpMethod["PUT"] = "PUT";
|
|
13
|
+
UploaderHttpMethod["PATCH"] = "PATCH";
|
|
14
|
+
return UploaderHttpMethod;
|
|
15
|
+
}({});
|
|
5
16
|
export const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
6
17
|
const uuid = uuidv4();
|
|
7
18
|
let subscription;
|
|
@@ -20,6 +31,8 @@ export const backgroundUpload = async (url, fileUrl, options, onProgress) => {
|
|
|
20
31
|
uuid,
|
|
21
32
|
method: options.httpMethod,
|
|
22
33
|
headers: options.headers,
|
|
34
|
+
uploadType: options.uploadType,
|
|
35
|
+
...options,
|
|
23
36
|
url
|
|
24
37
|
});
|
|
25
38
|
return result;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["NativeEventEmitter","Platform","Compressor","CompressEventEmitter","uuidv4","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","OS","includes","replace","result","upload","method","httpMethod","headers","remove"],"sourceRoot":"../../../src","sources":["utils/Uploader.tsx"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,QAAQ,QAAQ,cAAc;AAE3D,SAASC,UAAU,QAAQ,SAAS;AACpC,MAAMC,oBAAoB,GAAG,IAAIH,kBAAkB,CAACE,UAAU,CAAC;AAC/D,SAASE,MAAM,QAAQ,GAAG;
|
|
1
|
+
{"version":3,"names":["NativeEventEmitter","Platform","Compressor","CompressEventEmitter","uuidv4","UploadType","UploaderHttpMethod","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","OS","includes","replace","result","upload","method","httpMethod","headers","uploadType","remove"],"sourceRoot":"../../../src","sources":["utils/Uploader.tsx"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,QAAQ,QAAQ,cAAc;AAE3D,SAASC,UAAU,QAAQ,SAAS;AACpC,MAAMC,oBAAoB,GAAG,IAAIH,kBAAkB,CAACE,UAAU,CAAC;AAC/D,SAASE,MAAM,QAAQ,GAAG;AAC1B,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAVA,UAAU,CAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAKtB,WAAYC,kBAAkB,0BAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAlBA,kBAAkB;EAAA,OAAlBA,kBAAkB;AAAA;AA8B9B,OAAO,MAAMC,gBAAgB,GAAG,MAAAA,CAC9BC,GAAW,EACXC,OAAe,EACfC,OAAwB,EACxBC,UAAqD,KACpC;EACjB,MAAMC,IAAI,GAAGR,MAAM,CAAC,CAAC;EACrB,IAAIS,YAAqC;EACzC,IAAI;IACF,IAAIF,UAAU,EAAE;MACdE,YAAY,GAAGV,oBAAoB,CAACW,WAAW,CAC7C,gBAAgB,EACfC,KAAU,IAAK;QACd,IAAIA,KAAK,CAACH,IAAI,KAAKA,IAAI,EAAE;UACvBD,UAAU,CAACI,KAAK,CAACC,IAAI,CAACC,OAAO,EAAEF,KAAK,CAACC,IAAI,CAACE,KAAK,CAAC;QAClD;MACF,CACF,CAAC;IACH;IACA,IAAIjB,QAAQ,CAACkB,EAAE,KAAK,SAAS,IAAIV,OAAO,CAACW,QAAQ,CAAC,SAAS,CAAC,EAAE;MAC5DX,OAAO,GAAGA,OAAO,CAACY,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC;IAC1C;IACA,MAAMC,MAAM,GAAG,MAAMpB,UAAU,CAACqB,MAAM,CAACd,OAAO,EAAE;MAC9CG,IAAI;MACJY,MAAM,EAAEd,OAAO,CAACe,UAAU;MAC1BC,OAAO,EAAEhB,OAAO,CAACgB,OAAO;MACxBC,UAAU,EAAEjB,OAAO,CAACiB,UAAU;MAC9B,GAAGjB,OAAO;MACVF;IACF,CAAC,CAAC;IACF,OAAOc,MAAM;EACf,CAAC,SAAS;IACR;IACA,IAAIT,YAAY,EAAE;MAChBA,YAAY,CAACe,MAAM,CAAC,CAAC;IACvB;EACF;AACF,CAAC"}
|
|
@@ -2,8 +2,8 @@ import Video from './Video';
|
|
|
2
2
|
import type { VideoCompressorType } from './Video';
|
|
3
3
|
import Audio from './Audio';
|
|
4
4
|
import Image from './Image';
|
|
5
|
-
import { getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, getFileSize, backgroundUpload, createVideoThumbnail, download, clearCache } from './utils';
|
|
6
|
-
export { Video, Audio, Image, backgroundUpload, download, getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, createVideoThumbnail, clearCache, getFileSize, };
|
|
5
|
+
import { getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, getFileSize, backgroundUpload, createVideoThumbnail, download, clearCache, UploadType, UploaderHttpMethod } from './utils';
|
|
6
|
+
export { Video, Audio, Image, backgroundUpload, download, getDetails, uuidv4, generateFilePath, getRealPath, getVideoMetaData, createVideoThumbnail, clearCache, getFileSize, UploadType, UploaderHttpMethod, };
|
|
7
7
|
export type { VideoCompressorType };
|
|
8
8
|
declare const _default: {
|
|
9
9
|
Video: VideoCompressorType;
|
|
@@ -17,7 +17,7 @@ declare const _default: {
|
|
|
17
17
|
getRealPath: (path: string, type: "video" | "image") => Promise<string>;
|
|
18
18
|
getVideoMetaData: (filePath: string) => Promise<string>;
|
|
19
19
|
getFileSize: (filePath: string) => Promise<string>;
|
|
20
|
-
backgroundUpload: (url: string, fileUrl: string, options: import("./utils").
|
|
20
|
+
backgroundUpload: (url: string, fileUrl: string, options: import("./utils").UploaderOptions, onProgress?: ((writtem: number, total: number) => void) | undefined) => Promise<any>;
|
|
21
21
|
createVideoThumbnail: (fileUrl: string, options?: {
|
|
22
22
|
headers?: {
|
|
23
23
|
[key: string]: string;
|
|
@@ -31,6 +31,8 @@ declare const _default: {
|
|
|
31
31
|
}>;
|
|
32
32
|
clearCache: (cacheDir?: string | undefined) => Promise<string>;
|
|
33
33
|
download: (fileUrl: string, downloadProgress?: ((progress: number) => void) | undefined, progressDivider?: number | undefined) => Promise<any>;
|
|
34
|
+
UploadType: typeof UploadType;
|
|
35
|
+
UploaderHttpMethod: typeof UploaderHttpMethod;
|
|
34
36
|
};
|
|
35
37
|
export default _default;
|
|
36
38
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,EACL,UAAU,EACV,MAAM,EACN,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,oBAAoB,EACpB,QAAQ,EACR,UAAU,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,KAAK,MAAM,SAAS,CAAC;AAC5B,OAAO,EACL,UAAU,EACV,MAAM,EACN,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,oBAAoB,EACpB,QAAQ,EACR,UAAU,EACV,UAAU,EACV,kBAAkB,EACnB,MAAM,SAAS,CAAC;AAEjB,OAAO,EACL,KAAK,EACL,KAAK,EACL,KAAK,EACL,gBAAgB,EAChB,QAAQ,EAER,UAAU,EACV,MAAM,EACN,gBAAgB,EAChB,WAAW,EACX,gBAAgB,EAChB,oBAAoB,EACpB,UAAU,EACV,WAAW,EACX,UAAU,EACV,kBAAkB,GACnB,CAAC;AACF,YAAY,EAAE,mBAAmB,EAAE,CAAC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACpC,wBAgBE"}
|
|
@@ -1,28 +1,29 @@
|
|
|
1
|
-
export declare enum
|
|
1
|
+
export declare enum UploadType {
|
|
2
2
|
BINARY_CONTENT = 0,
|
|
3
3
|
MULTIPART = 1
|
|
4
4
|
}
|
|
5
|
-
export declare enum
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
export declare enum UploaderHttpMethod {
|
|
6
|
+
POST = "POST",
|
|
7
|
+
PUT = "PUT",
|
|
8
|
+
PATCH = "PATCH"
|
|
8
9
|
}
|
|
9
10
|
export declare type HTTPResponse = {
|
|
10
11
|
status: number;
|
|
11
12
|
headers: Record<string, string>;
|
|
12
13
|
body: string;
|
|
13
14
|
};
|
|
14
|
-
export declare type
|
|
15
|
-
export declare type
|
|
16
|
-
uploadType?:
|
|
15
|
+
export declare type HttpMethod = 'POST' | 'PUT' | 'PATCH';
|
|
16
|
+
export declare type UploaderOptions = ({
|
|
17
|
+
uploadType?: UploadType.BINARY_CONTENT;
|
|
18
|
+
mimeType?: string;
|
|
17
19
|
} | {
|
|
18
|
-
uploadType:
|
|
20
|
+
uploadType: UploadType.MULTIPART;
|
|
19
21
|
fieldName?: string;
|
|
20
22
|
mimeType?: string;
|
|
21
23
|
parameters?: Record<string, string>;
|
|
22
24
|
}) & {
|
|
23
25
|
headers?: Record<string, string>;
|
|
24
|
-
httpMethod?:
|
|
25
|
-
sessionType?: FileSystemSessionType;
|
|
26
|
+
httpMethod?: UploaderHttpMethod | HttpMethod;
|
|
26
27
|
};
|
|
27
|
-
export declare const backgroundUpload: (url: string, fileUrl: string, options:
|
|
28
|
+
export declare const backgroundUpload: (url: string, fileUrl: string, options: UploaderOptions, onProgress?: ((writtem: number, total: number) => void) | undefined) => Promise<any>;
|
|
28
29
|
//# sourceMappingURL=Uploader.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Uploader.d.ts","sourceRoot":"","sources":["../../../src/utils/Uploader.tsx"],"names":[],"mappings":"AAKA,
|
|
1
|
+
{"version":3,"file":"Uploader.d.ts","sourceRoot":"","sources":["../../../src/utils/Uploader.tsx"],"names":[],"mappings":"AAKA,oBAAY,UAAU;IACpB,cAAc,IAAI;IAClB,SAAS,IAAI;CACd;AAED,oBAAY,kBAAkB;IAC5B,IAAI,SAAS;IACb,GAAG,QAAQ;IACX,KAAK,UAAU;CAChB;AAED,MAAM,CAAC,OAAO,MAAM,YAAY,GAAG;IACjC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,IAAI,EAAE,MAAM,CAAC;CACd,CAAC;AAEF,MAAM,CAAC,OAAO,MAAM,UAAU,GAAG,MAAM,GAAG,KAAK,GAAG,OAAO,CAAC;AAE1D,MAAM,CAAC,OAAO,MAAM,eAAe,GAAG,CAClC;IACE,UAAU,CAAC,EAAE,UAAU,CAAC,cAAc,CAAC;IACvC,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB,GACD;IACE,UAAU,EAAE,UAAU,CAAC,SAAS,CAAC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACrC,CACJ,GAAG;IACF,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,UAAU,CAAC,EAAE,kBAAkB,GAAG,UAAU,CAAC;CAC9C,CAAC;AAEF,eAAO,MAAM,gBAAgB,QACtB,MAAM,WACF,MAAM,WACN,eAAe,0BACD,MAAM,SAAS,MAAM,KAAK,IAAI,kBACpD,QAAQ,GAAG,CAgCb,CAAC"}
|
package/package.json
CHANGED
package/src/index.tsx
CHANGED
|
@@ -13,6 +13,8 @@ import {
|
|
|
13
13
|
createVideoThumbnail,
|
|
14
14
|
download,
|
|
15
15
|
clearCache,
|
|
16
|
+
UploadType,
|
|
17
|
+
UploaderHttpMethod,
|
|
16
18
|
} from './utils';
|
|
17
19
|
|
|
18
20
|
export {
|
|
@@ -30,6 +32,8 @@ export {
|
|
|
30
32
|
createVideoThumbnail,
|
|
31
33
|
clearCache,
|
|
32
34
|
getFileSize,
|
|
35
|
+
UploadType,
|
|
36
|
+
UploaderHttpMethod,
|
|
33
37
|
};
|
|
34
38
|
export type { VideoCompressorType };
|
|
35
39
|
export default {
|
|
@@ -46,4 +50,6 @@ export default {
|
|
|
46
50
|
createVideoThumbnail,
|
|
47
51
|
clearCache,
|
|
48
52
|
download,
|
|
53
|
+
UploadType,
|
|
54
|
+
UploaderHttpMethod,
|
|
49
55
|
};
|
package/src/utils/Uploader.tsx
CHANGED
|
@@ -3,14 +3,15 @@ import type { NativeEventSubscription } from 'react-native';
|
|
|
3
3
|
import { Compressor } from '../Main';
|
|
4
4
|
const CompressEventEmitter = new NativeEventEmitter(Compressor);
|
|
5
5
|
import { uuidv4 } from '.';
|
|
6
|
-
export
|
|
6
|
+
export enum UploadType {
|
|
7
7
|
BINARY_CONTENT = 0,
|
|
8
8
|
MULTIPART = 1,
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
export enum UploaderHttpMethod {
|
|
12
|
+
POST = 'POST',
|
|
13
|
+
PUT = 'PUT',
|
|
14
|
+
PATCH = 'PATCH',
|
|
14
15
|
}
|
|
15
16
|
|
|
16
17
|
export declare type HTTPResponse = {
|
|
@@ -19,31 +20,28 @@ export declare type HTTPResponse = {
|
|
|
19
20
|
body: string;
|
|
20
21
|
};
|
|
21
22
|
|
|
22
|
-
export declare type
|
|
23
|
-
| 'POST'
|
|
24
|
-
| 'PUT'
|
|
25
|
-
| 'PATCH';
|
|
23
|
+
export declare type HttpMethod = 'POST' | 'PUT' | 'PATCH';
|
|
26
24
|
|
|
27
|
-
export declare type
|
|
25
|
+
export declare type UploaderOptions = (
|
|
28
26
|
| {
|
|
29
|
-
uploadType?:
|
|
27
|
+
uploadType?: UploadType.BINARY_CONTENT;
|
|
28
|
+
mimeType?: string;
|
|
30
29
|
}
|
|
31
30
|
| {
|
|
32
|
-
uploadType:
|
|
31
|
+
uploadType: UploadType.MULTIPART;
|
|
33
32
|
fieldName?: string;
|
|
34
33
|
mimeType?: string;
|
|
35
34
|
parameters?: Record<string, string>;
|
|
36
35
|
}
|
|
37
36
|
) & {
|
|
38
37
|
headers?: Record<string, string>;
|
|
39
|
-
httpMethod?:
|
|
40
|
-
sessionType?: FileSystemSessionType;
|
|
38
|
+
httpMethod?: UploaderHttpMethod | HttpMethod;
|
|
41
39
|
};
|
|
42
40
|
|
|
43
41
|
export const backgroundUpload = async (
|
|
44
42
|
url: string,
|
|
45
43
|
fileUrl: string,
|
|
46
|
-
options:
|
|
44
|
+
options: UploaderOptions,
|
|
47
45
|
onProgress?: (writtem: number, total: number) => void
|
|
48
46
|
): Promise<any> => {
|
|
49
47
|
const uuid = uuidv4();
|
|
@@ -66,6 +64,8 @@ export const backgroundUpload = async (
|
|
|
66
64
|
uuid,
|
|
67
65
|
method: options.httpMethod,
|
|
68
66
|
headers: options.headers,
|
|
67
|
+
uploadType: options.uploadType,
|
|
68
|
+
...options,
|
|
69
69
|
url,
|
|
70
70
|
});
|
|
71
71
|
return result;
|