react-native-compressor 1.0.1 → 1.1.1
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 +57 -4
- package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.java +6 -14
- package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression/AutoVideoCompression.java +3 -2
- package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java +4 -0
- package/ios/Video/VideoCompressor.swift +8 -8
- package/lib/commonjs/Video/index.js +6 -0
- package/lib/commonjs/Video/index.js.map +1 -1
- package/lib/module/Video/index.js +5 -0
- package/lib/module/Video/index.js.map +1 -1
- package/lib/typescript/Video/index.d.ts +3 -0
- package/package.json +4 -1
- package/src/Video/index.tsx +9 -0
package/README.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
### Would you like to support me?
|
|
2
|
+
|
|
3
|
+
<a href="https://www.buymeacoffee.com/numan.dev" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: auto !important;width: auto !important;" ></a>
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
1
7
|
# react-native-compressor
|
|
2
8
|
|
|
3
9
|
<!-- Title -->
|
|
@@ -18,18 +24,20 @@
|
|
|
18
24
|
|
|
19
25
|
**react-native-compressor** package is a set of functions that allow you compress `Image`,`Audio` and `Video`
|
|
20
26
|
|
|
27
|
+
If you find this package useful hit the star 🌟
|
|
28
|
+
|
|
21
29
|
## Installation
|
|
22
30
|
|
|
23
|
-
|
|
31
|
+
#### For React Native 0.64 or lower
|
|
24
32
|
|
|
25
33
|
```sh
|
|
26
|
-
yarn add react-native-compressor
|
|
34
|
+
yarn add react-native-compressor@0.5.6
|
|
27
35
|
```
|
|
28
36
|
|
|
29
|
-
|
|
37
|
+
#### For React Native 0.65 or greater
|
|
30
38
|
|
|
31
39
|
```sh
|
|
32
|
-
|
|
40
|
+
yarn add react-native-compressor
|
|
33
41
|
```
|
|
34
42
|
|
|
35
43
|
### Automatic linking (for React Native >= 0.60 only)
|
|
@@ -150,6 +158,23 @@ const result = await Audio.compress(
|
|
|
150
158
|
);
|
|
151
159
|
```
|
|
152
160
|
|
|
161
|
+
### Background Upload
|
|
162
|
+
|
|
163
|
+
```js
|
|
164
|
+
import { backgroundUpload } from 'react-native-compressor';
|
|
165
|
+
|
|
166
|
+
const headers = {};
|
|
167
|
+
|
|
168
|
+
const uploadResult = await backgroundUpload(
|
|
169
|
+
url,
|
|
170
|
+
fileUrl,
|
|
171
|
+
{ httpMethod: 'PUT', headers },
|
|
172
|
+
(written, total) => {
|
|
173
|
+
console.log(written, total);
|
|
174
|
+
}
|
|
175
|
+
);
|
|
176
|
+
```
|
|
177
|
+
|
|
153
178
|
# API
|
|
154
179
|
|
|
155
180
|
## Image
|
|
@@ -204,8 +229,12 @@ const result = await Audio.compress(
|
|
|
204
229
|
The maximum size can be height in case of portrait video or can be width in case of landscape video.
|
|
205
230
|
|
|
206
231
|
- ###### `bitrate: string`
|
|
232
|
+
|
|
207
233
|
bitrate of video which reduce or increase video size. if compressionMethod will auto then this prop will not work
|
|
208
234
|
|
|
235
|
+
- ###### `minimumFileSizeForCompress: number` (default: 16)
|
|
236
|
+
16 means 16mb. default our package do not compress under 16mb video file. minimumFileSizeForCompress will allow us to change this 16mb offset. fixed [#26](https://github.com/Shobbak/react-native-compressor/issues/26)
|
|
237
|
+
|
|
209
238
|
## Audio
|
|
210
239
|
|
|
211
240
|
- ###### `compress(url: string, options?: audioCompresssionType): Promise<string>`
|
|
@@ -217,6 +246,30 @@ const result = await Audio.compress(
|
|
|
217
246
|
|
|
218
247
|
**Note: Audio compression will be add soon**
|
|
219
248
|
|
|
249
|
+
## Background Upload
|
|
250
|
+
|
|
251
|
+
- ###### `backgroundUpload: (url: string, fileUrl: string, options: FileSystemUploadOptions, onProgress?: ((writtem: number, total: number) => void) | undefined) => Promise<any>
|
|
252
|
+
|
|
253
|
+
- ###### ` FileSystemUploadOptions`
|
|
254
|
+
|
|
255
|
+
```js
|
|
256
|
+
type FileSystemUploadOptions = (
|
|
257
|
+
| {
|
|
258
|
+
uploadType?: FileSystemUploadType.BINARY_CONTENT,
|
|
259
|
+
}
|
|
260
|
+
| {
|
|
261
|
+
uploadType: FileSystemUploadType.MULTIPART,
|
|
262
|
+
fieldName?: string,
|
|
263
|
+
mimeType?: string,
|
|
264
|
+
parameters?: Record<string, string>,
|
|
265
|
+
}
|
|
266
|
+
) & {
|
|
267
|
+
headers?: Record<string, string>,
|
|
268
|
+
httpMethod?: FileSystemAcceptedUploadHttpMethod,
|
|
269
|
+
sessionType?: FileSystemSessionType,
|
|
270
|
+
};
|
|
271
|
+
```
|
|
272
|
+
|
|
220
273
|
## Contributing
|
|
221
274
|
|
|
222
275
|
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
@@ -66,7 +66,7 @@ public class ImageCompressor {
|
|
|
66
66
|
|
|
67
67
|
|
|
68
68
|
|
|
69
|
-
public static String encodeImage(ByteArrayOutputStream imageDataByteArrayOutputStream, Boolean isBase64,
|
|
69
|
+
public static String encodeImage(ByteArrayOutputStream imageDataByteArrayOutputStream, Boolean isBase64,String outputExtension, ReactApplicationContext reactContext) {
|
|
70
70
|
if(isBase64)
|
|
71
71
|
{
|
|
72
72
|
byte[] imageData=imageDataByteArrayOutputStream.toByteArray();
|
|
@@ -124,16 +124,16 @@ public class ImageCompressor {
|
|
|
124
124
|
final ByteArrayOutputStream imageDataByteArrayOutputStream = ImageCompressor.compress(resizedImage, options.output, options.quality);
|
|
125
125
|
Boolean isBase64=options.returnableOutputType==ImageCompressorOptions.ReturnableOutputType.base64;
|
|
126
126
|
|
|
127
|
-
String returnableResult = ImageCompressor.encodeImage(imageDataByteArrayOutputStream,isBase64,
|
|
127
|
+
String returnableResult = ImageCompressor.encodeImage(imageDataByteArrayOutputStream,isBase64,options.output.toString(),reactContext);
|
|
128
128
|
return returnableResult;
|
|
129
129
|
}
|
|
130
130
|
|
|
131
131
|
|
|
132
132
|
public static String autoCompressImage(String imagePath,ImageCompressorOptions compressorOptions, ReactApplicationContext reactContext) {
|
|
133
133
|
String outputExtension=compressorOptions.output.toString();
|
|
134
|
-
int quality= (int) (compressorOptions.quality*100);
|
|
135
134
|
float autoCompressMaxHeight = compressorOptions.maxHeight;
|
|
136
135
|
float autoCompressMaxWidth = compressorOptions.maxWidth;
|
|
136
|
+
Boolean isBase64=compressorOptions.returnableOutputType==ImageCompressorOptions.ReturnableOutputType.base64;
|
|
137
137
|
|
|
138
138
|
Uri uri= Uri.parse(imagePath);
|
|
139
139
|
imagePath = uri.getPath();
|
|
@@ -217,18 +217,10 @@ public class ImageCompressor {
|
|
|
217
217
|
} catch (IOException e) {
|
|
218
218
|
e.printStackTrace();
|
|
219
219
|
}
|
|
220
|
-
FileOutputStream out = null;
|
|
221
|
-
String filepath = generateCacheFilePath(outputExtension,reactContext);;
|
|
222
|
-
try {
|
|
223
|
-
out = new FileOutputStream(filepath);
|
|
224
|
-
|
|
225
|
-
//write the compressed bitmap at the destination specified by filename.
|
|
226
|
-
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, quality, out);
|
|
227
220
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
return getRNFileUrl(filepath);
|
|
221
|
+
final ByteArrayOutputStream imageDataByteArrayOutputStream = ImageCompressor.compress(scaledBitmap, compressorOptions.output, compressorOptions.quality);
|
|
222
|
+
String returnableResult = ImageCompressor.encodeImage(imageDataByteArrayOutputStream,isBase64,compressorOptions.output.toString(),reactContext);
|
|
223
|
+
return returnableResult;
|
|
232
224
|
}
|
|
233
225
|
|
|
234
226
|
public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
|
|
@@ -26,7 +26,8 @@ public class AutoVideoCompression {
|
|
|
26
26
|
static int currentVideoCompression=0;
|
|
27
27
|
|
|
28
28
|
public static void createCompressionSettings(String fileUrl,VideoCompressorHelper options,Promise promise, ReactApplicationContext reactContext) {
|
|
29
|
-
float maxSize = options.maxSize
|
|
29
|
+
float maxSize = options.maxSize;
|
|
30
|
+
float minimumFileSizeForCompress=options.minimumFileSizeForCompress;
|
|
30
31
|
try{
|
|
31
32
|
Uri uri= Uri.parse(fileUrl);
|
|
32
33
|
String srcPath = uri.getPath();
|
|
@@ -35,7 +36,7 @@ public class AutoVideoCompression {
|
|
|
35
36
|
File file=new File(srcPath);
|
|
36
37
|
float sizeInBytes = file.length();
|
|
37
38
|
float sizeInMb = sizeInBytes / (1024 * 1024);
|
|
38
|
-
if(sizeInMb>
|
|
39
|
+
if(sizeInMb>minimumFileSizeForCompress)
|
|
39
40
|
{
|
|
40
41
|
String destinationPath = generateCacheFilePath("mp4", reactContext);
|
|
41
42
|
int actualHeight =Integer.parseInt(metaRetriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
@@ -102,6 +102,7 @@ public class VideoCompressorHelper {
|
|
|
102
102
|
public float bitrate = 0;
|
|
103
103
|
public String uuid = "";
|
|
104
104
|
public float maxSize = 640.0f;
|
|
105
|
+
public float minimumFileSizeForCompress = 16.0f;
|
|
105
106
|
|
|
106
107
|
public static VideoCompressorHelper fromMap(ReadableMap map) {
|
|
107
108
|
final VideoCompressorHelper options = new VideoCompressorHelper();
|
|
@@ -120,6 +121,9 @@ public class VideoCompressorHelper {
|
|
|
120
121
|
case "uuid":
|
|
121
122
|
options.uuid = map.getString(key);
|
|
122
123
|
break;
|
|
124
|
+
case "minimumFileSizeForCompress":
|
|
125
|
+
options.minimumFileSizeForCompress =(float) map.getDouble(key);
|
|
126
|
+
break;
|
|
123
127
|
|
|
124
128
|
}
|
|
125
129
|
}
|
|
@@ -199,10 +199,13 @@ func makeValidUri(filePath: String) -> String {
|
|
|
199
199
|
|
|
200
200
|
|
|
201
201
|
func compressVideo(url: URL, options: [String: Any], onProgress: @escaping (Float) -> Void, onCompletion: @escaping (URL) -> Void, onFailure: @escaping (Error) -> Void){
|
|
202
|
-
|
|
202
|
+
var minimumFileSizeForCompress:Double=16.0;
|
|
203
203
|
let fileSize=self.getfileSize(forURL: url);
|
|
204
|
-
|
|
205
|
-
|
|
204
|
+
if((options["minimumFileSizeForCompress"]) != nil)
|
|
205
|
+
{
|
|
206
|
+
minimumFileSizeForCompress=options["minimumFileSizeForCompress"] as! Double;
|
|
207
|
+
}
|
|
208
|
+
if(fileSize>minimumFileSizeForCompress)
|
|
206
209
|
{
|
|
207
210
|
if(options["compressionMethod"] as! String=="auto")
|
|
208
211
|
{
|
|
@@ -242,8 +245,8 @@ func makeValidUri(filePath: String) -> String {
|
|
|
242
245
|
let compressFactor:Float = 0.8
|
|
243
246
|
let minCompressFactor:Float = 0.8
|
|
244
247
|
let maxBitrate:Int = 1669000
|
|
245
|
-
|
|
246
|
-
var remeasuredBitrate:Int = originalBitrate /
|
|
248
|
+
let minValue:Float=min(Float(originalHeight)/Float(height),Float(originalWidth)/Float(width))
|
|
249
|
+
var remeasuredBitrate:Int = Int(Float(originalBitrate) / minValue)
|
|
247
250
|
remeasuredBitrate = remeasuredBitrate*Int(compressFactor)
|
|
248
251
|
let minBitrate:Int = self.getVideoBitrateWithFactor(f: minCompressFactor) / (1280 * 720 / (width * height))
|
|
249
252
|
if (originalBitrate < minBitrate) {
|
|
@@ -379,14 +382,11 @@ func makeValidUri(filePath: String) -> String {
|
|
|
379
382
|
onCompletion(exporter.outputURL!)
|
|
380
383
|
break
|
|
381
384
|
default:
|
|
382
|
-
// let error = CompressionError(message: "Compression didn't complete")
|
|
383
|
-
// onFailure(error)
|
|
384
385
|
onCompletion(url)
|
|
385
386
|
break
|
|
386
387
|
}
|
|
387
388
|
break
|
|
388
389
|
case .failure(let error):
|
|
389
|
-
// onFailure(error)
|
|
390
390
|
onCompletion(url)
|
|
391
391
|
break
|
|
392
392
|
}
|
|
@@ -7,6 +7,8 @@ exports.default = exports.backgroundUpload = void 0;
|
|
|
7
7
|
|
|
8
8
|
var _reactNative = require("react-native");
|
|
9
9
|
|
|
10
|
+
require("react-native-get-random-values");
|
|
11
|
+
|
|
10
12
|
var _uuid = require("uuid");
|
|
11
13
|
|
|
12
14
|
const VideoCompressEventEmitter = new _reactNative.NativeEventEmitter(_reactNative.NativeModules.VideoCompressor);
|
|
@@ -76,6 +78,10 @@ const Video = {
|
|
|
76
78
|
modifiedOptions.maxSize = 640;
|
|
77
79
|
}
|
|
78
80
|
|
|
81
|
+
if (options !== null && options !== void 0 && options.minimumFileSizeForCompress) {
|
|
82
|
+
modifiedOptions.minimumFileSizeForCompress = options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress;
|
|
83
|
+
}
|
|
84
|
+
|
|
79
85
|
const result = await NativeVideoCompressor.compress(fileUrl, modifiedOptions);
|
|
80
86
|
return result;
|
|
81
87
|
} finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.tsx"],"names":["VideoCompressEventEmitter","NativeEventEmitter","NativeModules","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","Platform","OS","includes","replace","result","upload","method","httpMethod","headers","remove","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":";;;;;;;AAAA;;AAMA;;
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["VideoCompressEventEmitter","NativeEventEmitter","NativeModules","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","Platform","OS","includes","replace","result","upload","method","httpMethod","headers","remove","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":";;;;;;;AAAA;;AAMA;;AACA;;AA8DA,MAAMA,yBAAyB,GAAG,IAAIC,+BAAJ,CAChCC,2BAAcC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AAJ,EAAkB;AAChBA,QAAAA,YAAY,CAACe,MAAb;AACD;AACF;AACF,GAzDgC;AA0DjCrB,EAAAA,gBA1DiC;;AA2DjC8B,EAAAA,sBAAsB,CAACC,SAAD,EAAa;AACjC,QAAIA,SAAJ,EAAe;AACb,YAAMzB,YAAqC,GACzCX,yBAAyB,CAACY,WAA1B,CACE,uBADF,EAEGC,KAAD,IAAgB;AACduB,QAAAA,SAAS,CAACvB,KAAD,CAAT;;AACA,YAAIF,YAAJ,EAAkB;AAChBA,UAAAA,YAAY,CAACe,MAAb;AACD;AACF,OAPH,CADF;AAUD;;AACD,WAAOtB,qBAAqB,CAAC+B,sBAAtB,CAA6C,EAA7C,CAAP;AACD,GAzEgC;;AA0EjCE,EAAAA,wBAAwB,GAAG;AACzBrC,IAAAA,yBAAyB,CAACsC,kBAA1B,CAA6C,uBAA7C;AACA,WAAOlC,qBAAqB,CAACiC,wBAAtB,CAA+C,EAA/C,CAAP;AACD;;AA7EgC,CAAnC;eAgFeV,K","sourcesContent":["import {\n NativeModules,\n NativeEventEmitter,\n Platform,\n NativeEventSubscription,\n} from 'react-native';\nimport 'react-native-get-random-values';\nimport { v4 as uuidv4 } from 'uuid';\n\nexport declare enum FileSystemUploadType {\n BINARY_CONTENT = 0,\n MULTIPART = 1,\n}\n\nexport declare type FileSystemAcceptedUploadHttpMethod =\n | 'POST'\n | 'PUT'\n | 'PATCH';\nexport type compressionMethod = 'auto' | 'manual';\ntype videoCompresssionType = {\n bitrate?: number;\n maxSize?: number;\n compressionMethod?: compressionMethod;\n minimumFileSizeForCompress?: number;\n};\n\nexport declare enum FileSystemSessionType {\n BACKGROUND = 0,\n FOREGROUND = 1,\n}\n\nexport declare type HTTPResponse = {\n status: number;\n headers: Record<string, string>;\n body: string;\n};\n\nexport declare type FileSystemUploadOptions = (\n | {\n uploadType?: FileSystemUploadType.BINARY_CONTENT;\n }\n | {\n uploadType: FileSystemUploadType.MULTIPART;\n fieldName?: string;\n mimeType?: string;\n parameters?: Record<string, string>;\n }\n) & {\n headers?: Record<string, string>;\n httpMethod?: FileSystemAcceptedUploadHttpMethod;\n sessionType?: FileSystemSessionType;\n};\n\nexport type VideoCompressorType = {\n compress(\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ): Promise<string>;\n backgroundUpload(\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n ): Promise<any>;\n activateBackgroundTask(onExpired?: (data: any) => void): Promise<any>;\n deactivateBackgroundTask(): Promise<any>;\n};\n\nconst VideoCompressEventEmitter = new NativeEventEmitter(\n NativeModules.VideoCompressor\n);\n\nconst NativeVideoCompressor = NativeModules.VideoCompressor;\n\nexport const backgroundUpload = async (\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n): Promise<any> => {\n const uuid = uuidv4();\n let subscription: NativeEventSubscription;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'VideoCompressorProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.written, event.data.total);\n }\n }\n );\n }\n if (Platform.OS === 'android' && fileUrl.includes('file://')) {\n fileUrl = fileUrl.replace('file://', '');\n }\n const result = await NativeVideoCompressor.upload(fileUrl, {\n uuid,\n method: options.httpMethod,\n headers: options.headers,\n url,\n });\n return result;\n } finally {\n // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n};\n\nconst Video: VideoCompressorType = {\n compress: async (\n fileUrl: string,\n options?: {\n bitrate?: number;\n compressionMethod?: compressionMethod;\n maxSize?: number;\n minimumFileSizeForCompress?: number;\n },\n onProgress?: (progress: number) => void\n ) => {\n const uuid = uuidv4();\n let subscription: NativeEventSubscription;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'videoCompressProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.progress);\n }\n }\n );\n }\n const modifiedOptions: {\n uuid: string;\n bitrate?: number;\n compressionMethod?: compressionMethod;\n maxSize?: number;\n minimumFileSizeForCompress?: number;\n } = { uuid };\n if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;\n if (options?.compressionMethod) {\n modifiedOptions.compressionMethod = options?.compressionMethod;\n } else {\n modifiedOptions.compressionMethod = 'manual';\n }\n if (options?.maxSize) {\n modifiedOptions.maxSize = options?.maxSize;\n } else {\n modifiedOptions.maxSize = 640;\n }\n if (options?.minimumFileSizeForCompress) {\n modifiedOptions.minimumFileSizeForCompress =\n options?.minimumFileSizeForCompress;\n }\n const result = await NativeVideoCompressor.compress(\n fileUrl,\n modifiedOptions\n );\n return result;\n } finally {\n // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n },\n backgroundUpload,\n activateBackgroundTask(onExpired?) {\n if (onExpired) {\n const subscription: NativeEventSubscription =\n VideoCompressEventEmitter.addListener(\n 'backgroundTaskExpired',\n (event: any) => {\n onExpired(event);\n if (subscription) {\n subscription.remove();\n }\n }\n );\n }\n return NativeVideoCompressor.activateBackgroundTask({});\n },\n deactivateBackgroundTask() {\n VideoCompressEventEmitter.removeAllListeners('backgroundTaskExpired');\n return NativeVideoCompressor.deactivateBackgroundTask({});\n },\n} as VideoCompressorType;\n\nexport default Video;\n"]}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
|
|
2
|
+
import 'react-native-get-random-values';
|
|
2
3
|
import { v4 as uuidv4 } from 'uuid';
|
|
3
4
|
const VideoCompressEventEmitter = new NativeEventEmitter(NativeModules.VideoCompressor);
|
|
4
5
|
const NativeVideoCompressor = NativeModules.VideoCompressor;
|
|
@@ -64,6 +65,10 @@ const Video = {
|
|
|
64
65
|
modifiedOptions.maxSize = 640;
|
|
65
66
|
}
|
|
66
67
|
|
|
68
|
+
if (options !== null && options !== void 0 && options.minimumFileSizeForCompress) {
|
|
69
|
+
modifiedOptions.minimumFileSizeForCompress = options === null || options === void 0 ? void 0 : options.minimumFileSizeForCompress;
|
|
70
|
+
}
|
|
71
|
+
|
|
67
72
|
const result = await NativeVideoCompressor.compress(fileUrl, modifiedOptions);
|
|
68
73
|
return result;
|
|
69
74
|
} finally {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["index.tsx"],"names":["NativeModules","NativeEventEmitter","Platform","v4","uuidv4","VideoCompressEventEmitter","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","OS","includes","replace","result","upload","method","httpMethod","headers","remove","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":"AAAA,SACEA,aADF,EAEEC,kBAFF,EAGEC,QAHF,QAKO,cALP;AAMA,SAASC,EAAE,IAAIC,MAAf,QAA6B,MAA7B;
|
|
1
|
+
{"version":3,"sources":["index.tsx"],"names":["NativeModules","NativeEventEmitter","Platform","v4","uuidv4","VideoCompressEventEmitter","VideoCompressor","NativeVideoCompressor","backgroundUpload","url","fileUrl","options","onProgress","uuid","subscription","addListener","event","data","written","total","OS","includes","replace","result","upload","method","httpMethod","headers","remove","Video","compress","progress","modifiedOptions","bitrate","compressionMethod","maxSize","minimumFileSizeForCompress","activateBackgroundTask","onExpired","deactivateBackgroundTask","removeAllListeners"],"mappings":"AAAA,SACEA,aADF,EAEEC,kBAFF,EAGEC,QAHF,QAKO,cALP;AAMA,OAAO,gCAAP;AACA,SAASC,EAAE,IAAIC,MAAf,QAA6B,MAA7B;AA8DA,MAAMC,yBAAyB,GAAG,IAAIJ,kBAAJ,CAChCD,aAAa,CAACM,eADkB,CAAlC;AAIA,MAAMC,qBAAqB,GAAGP,aAAa,CAACM,eAA5C;AAEA,OAAO,MAAME,gBAAgB,GAAG,OAC9BC,GAD8B,EAE9BC,OAF8B,EAG9BC,OAH8B,EAI9BC,UAJ8B,KAKb;AACjB,QAAMC,IAAI,GAAGT,MAAM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AACb,YAAMxB,YAAqC,GACzCT,yBAAyB,CAACU,WAA1B,CACE,uBADF,EAEGC,KAAD,IAAgB;AACdsB,QAAAA,SAAS,CAACtB,KAAD,CAAT;;AACA,YAAIF,YAAJ,EAAkB;AAChBA,UAAAA,YAAY,CAACc,MAAb;AACD;AACF,OAPH,CADF;AAUD;;AACD,WAAOrB,qBAAqB,CAAC8B,sBAAtB,CAA6C,EAA7C,CAAP;AACD,GAzEgC;;AA0EjCE,EAAAA,wBAAwB,GAAG;AACzBlC,IAAAA,yBAAyB,CAACmC,kBAA1B,CAA6C,uBAA7C;AACA,WAAOjC,qBAAqB,CAACgC,wBAAtB,CAA+C,EAA/C,CAAP;AACD;;AA7EgC,CAAnC;AAgFA,eAAeV,KAAf","sourcesContent":["import {\n NativeModules,\n NativeEventEmitter,\n Platform,\n NativeEventSubscription,\n} from 'react-native';\nimport 'react-native-get-random-values';\nimport { v4 as uuidv4 } from 'uuid';\n\nexport declare enum FileSystemUploadType {\n BINARY_CONTENT = 0,\n MULTIPART = 1,\n}\n\nexport declare type FileSystemAcceptedUploadHttpMethod =\n | 'POST'\n | 'PUT'\n | 'PATCH';\nexport type compressionMethod = 'auto' | 'manual';\ntype videoCompresssionType = {\n bitrate?: number;\n maxSize?: number;\n compressionMethod?: compressionMethod;\n minimumFileSizeForCompress?: number;\n};\n\nexport declare enum FileSystemSessionType {\n BACKGROUND = 0,\n FOREGROUND = 1,\n}\n\nexport declare type HTTPResponse = {\n status: number;\n headers: Record<string, string>;\n body: string;\n};\n\nexport declare type FileSystemUploadOptions = (\n | {\n uploadType?: FileSystemUploadType.BINARY_CONTENT;\n }\n | {\n uploadType: FileSystemUploadType.MULTIPART;\n fieldName?: string;\n mimeType?: string;\n parameters?: Record<string, string>;\n }\n) & {\n headers?: Record<string, string>;\n httpMethod?: FileSystemAcceptedUploadHttpMethod;\n sessionType?: FileSystemSessionType;\n};\n\nexport type VideoCompressorType = {\n compress(\n fileUrl: string,\n options?: videoCompresssionType,\n onProgress?: (progress: number) => void\n ): Promise<string>;\n backgroundUpload(\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n ): Promise<any>;\n activateBackgroundTask(onExpired?: (data: any) => void): Promise<any>;\n deactivateBackgroundTask(): Promise<any>;\n};\n\nconst VideoCompressEventEmitter = new NativeEventEmitter(\n NativeModules.VideoCompressor\n);\n\nconst NativeVideoCompressor = NativeModules.VideoCompressor;\n\nexport const backgroundUpload = async (\n url: string,\n fileUrl: string,\n options: FileSystemUploadOptions,\n onProgress?: (writtem: number, total: number) => void\n): Promise<any> => {\n const uuid = uuidv4();\n let subscription: NativeEventSubscription;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'VideoCompressorProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.written, event.data.total);\n }\n }\n );\n }\n if (Platform.OS === 'android' && fileUrl.includes('file://')) {\n fileUrl = fileUrl.replace('file://', '');\n }\n const result = await NativeVideoCompressor.upload(fileUrl, {\n uuid,\n method: options.httpMethod,\n headers: options.headers,\n url,\n });\n return result;\n } finally {\n // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n};\n\nconst Video: VideoCompressorType = {\n compress: async (\n fileUrl: string,\n options?: {\n bitrate?: number;\n compressionMethod?: compressionMethod;\n maxSize?: number;\n minimumFileSizeForCompress?: number;\n },\n onProgress?: (progress: number) => void\n ) => {\n const uuid = uuidv4();\n let subscription: NativeEventSubscription;\n try {\n if (onProgress) {\n subscription = VideoCompressEventEmitter.addListener(\n 'videoCompressProgress',\n (event: any) => {\n if (event.uuid === uuid) {\n onProgress(event.data.progress);\n }\n }\n );\n }\n const modifiedOptions: {\n uuid: string;\n bitrate?: number;\n compressionMethod?: compressionMethod;\n maxSize?: number;\n minimumFileSizeForCompress?: number;\n } = { uuid };\n if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;\n if (options?.compressionMethod) {\n modifiedOptions.compressionMethod = options?.compressionMethod;\n } else {\n modifiedOptions.compressionMethod = 'manual';\n }\n if (options?.maxSize) {\n modifiedOptions.maxSize = options?.maxSize;\n } else {\n modifiedOptions.maxSize = 640;\n }\n if (options?.minimumFileSizeForCompress) {\n modifiedOptions.minimumFileSizeForCompress =\n options?.minimumFileSizeForCompress;\n }\n const result = await NativeVideoCompressor.compress(\n fileUrl,\n modifiedOptions\n );\n return result;\n } finally {\n // @ts-ignore\n if (subscription) {\n subscription.remove();\n }\n }\n },\n backgroundUpload,\n activateBackgroundTask(onExpired?) {\n if (onExpired) {\n const subscription: NativeEventSubscription =\n VideoCompressEventEmitter.addListener(\n 'backgroundTaskExpired',\n (event: any) => {\n onExpired(event);\n if (subscription) {\n subscription.remove();\n }\n }\n );\n }\n return NativeVideoCompressor.activateBackgroundTask({});\n },\n deactivateBackgroundTask() {\n VideoCompressEventEmitter.removeAllListeners('backgroundTaskExpired');\n return NativeVideoCompressor.deactivateBackgroundTask({});\n },\n} as VideoCompressorType;\n\nexport default Video;\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import 'react-native-get-random-values';
|
|
1
2
|
export declare enum FileSystemUploadType {
|
|
2
3
|
BINARY_CONTENT = 0,
|
|
3
4
|
MULTIPART = 1
|
|
@@ -6,7 +7,9 @@ export declare type FileSystemAcceptedUploadHttpMethod = 'POST' | 'PUT' | 'PATCH
|
|
|
6
7
|
export declare type compressionMethod = 'auto' | 'manual';
|
|
7
8
|
declare type videoCompresssionType = {
|
|
8
9
|
bitrate?: number;
|
|
10
|
+
maxSize?: number;
|
|
9
11
|
compressionMethod?: compressionMethod;
|
|
12
|
+
minimumFileSizeForCompress?: number;
|
|
10
13
|
};
|
|
11
14
|
export declare enum FileSystemSessionType {
|
|
12
15
|
BACKGROUND = 0,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-compressor",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "This library compress image, video and audio",
|
|
5
5
|
"main": "lib/commonjs/index",
|
|
6
6
|
"module": "lib/module/index",
|
|
@@ -162,5 +162,8 @@
|
|
|
162
162
|
}
|
|
163
163
|
]
|
|
164
164
|
]
|
|
165
|
+
},
|
|
166
|
+
"dependencies": {
|
|
167
|
+
"react-native-get-random-values": "^1.7.0"
|
|
165
168
|
}
|
|
166
169
|
}
|
package/src/Video/index.tsx
CHANGED
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
Platform,
|
|
5
5
|
NativeEventSubscription,
|
|
6
6
|
} from 'react-native';
|
|
7
|
+
import 'react-native-get-random-values';
|
|
7
8
|
import { v4 as uuidv4 } from 'uuid';
|
|
8
9
|
|
|
9
10
|
export declare enum FileSystemUploadType {
|
|
@@ -18,7 +19,9 @@ export declare type FileSystemAcceptedUploadHttpMethod =
|
|
|
18
19
|
export type compressionMethod = 'auto' | 'manual';
|
|
19
20
|
type videoCompresssionType = {
|
|
20
21
|
bitrate?: number;
|
|
22
|
+
maxSize?: number;
|
|
21
23
|
compressionMethod?: compressionMethod;
|
|
24
|
+
minimumFileSizeForCompress?: number;
|
|
22
25
|
};
|
|
23
26
|
|
|
24
27
|
export declare enum FileSystemSessionType {
|
|
@@ -114,6 +117,7 @@ const Video: VideoCompressorType = {
|
|
|
114
117
|
bitrate?: number;
|
|
115
118
|
compressionMethod?: compressionMethod;
|
|
116
119
|
maxSize?: number;
|
|
120
|
+
minimumFileSizeForCompress?: number;
|
|
117
121
|
},
|
|
118
122
|
onProgress?: (progress: number) => void
|
|
119
123
|
) => {
|
|
@@ -135,6 +139,7 @@ const Video: VideoCompressorType = {
|
|
|
135
139
|
bitrate?: number;
|
|
136
140
|
compressionMethod?: compressionMethod;
|
|
137
141
|
maxSize?: number;
|
|
142
|
+
minimumFileSizeForCompress?: number;
|
|
138
143
|
} = { uuid };
|
|
139
144
|
if (options?.bitrate) modifiedOptions.bitrate = options?.bitrate;
|
|
140
145
|
if (options?.compressionMethod) {
|
|
@@ -147,6 +152,10 @@ const Video: VideoCompressorType = {
|
|
|
147
152
|
} else {
|
|
148
153
|
modifiedOptions.maxSize = 640;
|
|
149
154
|
}
|
|
155
|
+
if (options?.minimumFileSizeForCompress) {
|
|
156
|
+
modifiedOptions.minimumFileSizeForCompress =
|
|
157
|
+
options?.minimumFileSizeForCompress;
|
|
158
|
+
}
|
|
150
159
|
const result = await NativeVideoCompressor.compress(
|
|
151
160
|
fileUrl,
|
|
152
161
|
modifiedOptions
|