react-native-compressor 1.10.5 → 1.11.0
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.
|
@@ -9,7 +9,6 @@ import android.os.Build
|
|
|
9
9
|
import android.text.TextUtils
|
|
10
10
|
import android.webkit.URLUtil
|
|
11
11
|
import com.facebook.react.bridge.Arguments
|
|
12
|
-
import com.facebook.react.bridge.GuardedResultAsyncTask
|
|
13
12
|
import com.facebook.react.bridge.Promise
|
|
14
13
|
import com.facebook.react.bridge.ReactApplicationContext
|
|
15
14
|
import com.facebook.react.bridge.ReactContext
|
|
@@ -23,69 +22,76 @@ import java.io.UnsupportedEncodingException
|
|
|
23
22
|
import java.lang.ref.WeakReference
|
|
24
23
|
import java.net.URLDecoder
|
|
25
24
|
import java.util.UUID
|
|
26
|
-
|
|
25
|
+
import kotlinx.coroutines.CoroutineScope
|
|
26
|
+
import kotlinx.coroutines.Dispatchers
|
|
27
|
+
import kotlinx.coroutines.launch
|
|
28
|
+
import kotlinx.coroutines.withContext
|
|
27
29
|
|
|
28
30
|
class CreateVideoThumbnailClass(private val reactContext: ReactApplicationContext) {
|
|
29
31
|
@ReactMethod
|
|
30
|
-
fun create(fileUrl:String,options: ReadableMap, promise: Promise) {
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private class ProcessDataTask(reactContext: ReactContext,private val filePath:String, private val promise: Promise, private val options: ReadableMap) : GuardedResultAsyncTask<ReadableMap?>(reactContext.exceptionHandler) {
|
|
35
|
-
private val weakContext: WeakReference<Context>
|
|
36
|
-
|
|
37
|
-
init {
|
|
38
|
-
weakContext = WeakReference(reactContext.applicationContext)
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
override fun doInBackgroundGuarded(): ReadableMap? {
|
|
42
|
-
val format = "jpeg"
|
|
43
|
-
val cacheName = if (options.hasKey("cacheName")) options.getString("cacheName") else ""
|
|
44
|
-
val thumbnailDir = weakContext.get()!!.applicationContext.cacheDir.absolutePath + "/thumbnails"
|
|
45
|
-
val cacheDir = createDirIfNotExists(thumbnailDir)
|
|
46
|
-
if (!TextUtils.isEmpty(cacheName)) {
|
|
47
|
-
val file = File(thumbnailDir, "$cacheName.$format")
|
|
48
|
-
if (file.exists()) {
|
|
49
|
-
val map = Arguments.createMap()
|
|
50
|
-
map.putString("path", "file://" + file.absolutePath)
|
|
51
|
-
val image = BitmapFactory.decodeFile(file.absolutePath)
|
|
52
|
-
map.putDouble("size", image.byteCount.toDouble())
|
|
53
|
-
map.putString("mime", "image/$format")
|
|
54
|
-
map.putDouble("width", image.width.toDouble())
|
|
55
|
-
map.putDouble("height", image.height.toDouble())
|
|
56
|
-
return map
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
val headers: Map<String, String> = if (options.hasKey("headers")) options.getMap("headers")!!.toHashMap() as Map<String, String> else HashMap<String, String>()
|
|
60
|
-
val fileName = if (TextUtils.isEmpty(cacheName)) "thumb-" + UUID.randomUUID().toString() else "$cacheName.$format"
|
|
61
|
-
var fOut: OutputStream? = null
|
|
32
|
+
fun create(fileUrl: String, options: ReadableMap, promise: Promise) {
|
|
33
|
+
CoroutineScope(Dispatchers.Main).launch {
|
|
62
34
|
try {
|
|
63
|
-
val
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
35
|
+
val result = processDataInBackground(reactContext, fileUrl, options)
|
|
36
|
+
promise.resolve(result)
|
|
37
|
+
} catch (e: Exception) {
|
|
38
|
+
promise.reject("CreateVideoThumbnail_ERROR", e)
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
68
42
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
43
|
+
private suspend fun processDataInBackground(reactContext: ReactContext, filePath: String, options: ReadableMap): ReadableMap? = withContext(Dispatchers.IO) {
|
|
44
|
+
val weakContext = WeakReference(reactContext.applicationContext)
|
|
45
|
+
val format = "jpeg"
|
|
46
|
+
val cacheName = if (options.hasKey("cacheName")) options.getString("cacheName") else ""
|
|
47
|
+
val thumbnailDir = weakContext.get()!!.applicationContext.cacheDir.absolutePath + "/thumbnails"
|
|
48
|
+
val cacheDir = createDirIfNotExists(thumbnailDir)
|
|
73
49
|
|
|
50
|
+
if (!TextUtils.isEmpty(cacheName)) {
|
|
51
|
+
val file = File(thumbnailDir, "$cacheName.$format")
|
|
52
|
+
if (file.exists()) {
|
|
74
53
|
val map = Arguments.createMap()
|
|
75
54
|
map.putString("path", "file://" + file.absolutePath)
|
|
55
|
+
val image = BitmapFactory.decodeFile(file.absolutePath)
|
|
76
56
|
map.putDouble("size", image.byteCount.toDouble())
|
|
77
57
|
map.putString("mime", "image/$format")
|
|
78
58
|
map.putDouble("width", image.width.toDouble())
|
|
79
59
|
map.putDouble("height", image.height.toDouble())
|
|
80
|
-
return map
|
|
81
|
-
} catch (e: Exception) {
|
|
82
|
-
promise.reject("CreateVideoThumbnail_ERROR", e)
|
|
60
|
+
return@withContext map
|
|
83
61
|
}
|
|
84
|
-
return null
|
|
85
62
|
}
|
|
86
63
|
|
|
87
|
-
|
|
88
|
-
|
|
64
|
+
val headers: Map<String, String> = if (options.hasKey("headers")) options.getMap("headers")!!.toHashMap() as Map<String, String> else HashMap<String, String>()
|
|
65
|
+
val fileName = if (TextUtils.isEmpty(cacheName)) "thumb-" + UUID.randomUUID().toString() else "$cacheName.$format"
|
|
66
|
+
var fOut: OutputStream? = null
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
val file = File(cacheDir, fileName)
|
|
70
|
+
val context = weakContext.get()
|
|
71
|
+
val image = getBitmapAtTime(context, filePath, 0, headers)
|
|
72
|
+
file.createNewFile()
|
|
73
|
+
fOut = FileOutputStream(file)
|
|
74
|
+
|
|
75
|
+
// 100 means no compression, the lower you go, the stronger the compression
|
|
76
|
+
image.compress(Bitmap.CompressFormat.JPEG, 90, fOut)
|
|
77
|
+
fOut.flush()
|
|
78
|
+
fOut.close()
|
|
79
|
+
|
|
80
|
+
val map = Arguments.createMap()
|
|
81
|
+
map.putString("path", "file://" + file.absolutePath)
|
|
82
|
+
map.putDouble("size", image.byteCount.toDouble())
|
|
83
|
+
map.putString("mime", "image/$format")
|
|
84
|
+
map.putDouble("width", image.width.toDouble())
|
|
85
|
+
map.putDouble("height", image.height.toDouble())
|
|
86
|
+
return@withContext map
|
|
87
|
+
} catch (e: Exception) {
|
|
88
|
+
throw e
|
|
89
|
+
} finally {
|
|
90
|
+
try {
|
|
91
|
+
fOut?.close()
|
|
92
|
+
} catch (e: IOException) {
|
|
93
|
+
// Ignore
|
|
94
|
+
}
|
|
89
95
|
}
|
|
90
96
|
}
|
|
91
97
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-compressor",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.11.0",
|
|
4
4
|
"description": "Compress Image, Video, and Audio same like Whatsapp & Auto/Manual Compression | Background Upload | Download File | Create Video Thumbnail",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|