react-native-compressor 1.7.1 → 1.8.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.
- package/README.md +40 -15
- package/android/build.gradle +8 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.kt +527 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioMain.kt +31 -0
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.kt +157 -0
- package/android/src/main/java/com/reactnativecompressor/CompressorPackage.kt +34 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.kt +199 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressorOptions.kt +57 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageMain.kt +31 -0
- package/android/src/main/java/com/reactnativecompressor/Image/ImageSize.kt +3 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Downloader.kt +119 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/EventEmitterHandler.kt +66 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/MediaCache.kt +61 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.kt +177 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Uploader.kt +117 -0
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.kt +162 -0
- package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression.kt +63 -0
- package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.kt +125 -0
- package/android/src/main/java/com/reactnativecompressor/Video/VideoMain.kt +86 -0
- package/android/src/newarch/CompressorSpec.kt +5 -0
- package/android/src/oldarch/CompressorSpec.kt +32 -0
- package/ios/Audio/AudioMain.swift +115 -0
- package/ios/Compressor-Bridging-Header.h +1 -2
- package/ios/Compressor.h +0 -8
- package/ios/Compressor.mm +28 -389
- package/ios/CompressorManager.swift +96 -0
- package/ios/Image/ImageCompressor.swift +354 -0
- package/ios/Image/ImageCompressorOptions.swift +93 -0
- package/ios/Image/ImageMain.swift +27 -0
- package/ios/Utils/Downloader.swift +157 -0
- package/ios/Utils/EventEmitterHandler.swift +54 -0
- package/ios/Utils/MediaCache.swift +48 -0
- package/ios/Utils/Uploader.swift +99 -0
- package/ios/Utils/Utils.swift +110 -0
- package/ios/Video/{VideoCompressor.swift → VideoMain.swift} +168 -147
- package/lib/commonjs/Image/index.js +1 -1
- package/lib/commonjs/Image/index.js.map +1 -1
- package/lib/commonjs/Main.js +1 -8
- package/lib/commonjs/Main.js.map +1 -1
- package/lib/commonjs/Spec/NativeCompressor.js.map +1 -1
- package/lib/commonjs/Video/index.js +4 -33
- package/lib/commonjs/Video/index.js.map +1 -1
- package/lib/commonjs/expo-plugin/compressor.js.map +1 -1
- package/lib/commonjs/index.js +11 -6
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/utils/Downloader.js +38 -0
- package/lib/commonjs/utils/Downloader.js.map +1 -0
- package/lib/commonjs/utils/Uploader.js +40 -0
- package/lib/commonjs/utils/Uploader.js.map +1 -0
- package/lib/commonjs/utils/index.js +35 -0
- package/lib/commonjs/utils/index.js.map +1 -1
- package/lib/module/Image/index.js +1 -1
- package/lib/module/Image/index.js.map +1 -1
- package/lib/module/Main.js +1 -7
- package/lib/module/Main.js.map +1 -1
- package/lib/module/Spec/NativeCompressor.js.map +1 -1
- package/lib/module/Video/index.js +5 -33
- package/lib/module/Video/index.js.map +1 -1
- package/lib/module/expo-plugin/compressor.js.map +1 -1
- package/lib/module/index.js +6 -6
- package/lib/module/index.js.map +1 -1
- package/lib/module/utils/Downloader.js +31 -0
- package/lib/module/utils/Downloader.js.map +1 -0
- package/lib/module/utils/Uploader.js +33 -0
- package/lib/module/utils/Uploader.js.map +1 -0
- package/lib/module/utils/index.js +2 -0
- package/lib/module/utils/index.js.map +1 -1
- package/lib/typescript/Image/index.d.ts +4 -0
- package/lib/typescript/Image/index.d.ts.map +1 -1
- package/lib/typescript/Main.d.ts +1 -2
- package/lib/typescript/Main.d.ts.map +1 -1
- package/lib/typescript/Spec/NativeCompressor.d.ts +8 -2
- package/lib/typescript/Spec/NativeCompressor.d.ts.map +1 -1
- package/lib/typescript/Video/index.d.ts +4 -28
- package/lib/typescript/Video/index.d.ts.map +1 -1
- package/lib/typescript/index.d.ts +5 -4
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/utils/Downloader.d.ts +2 -0
- package/lib/typescript/utils/Downloader.d.ts.map +1 -0
- package/lib/typescript/utils/Uploader.d.ts +28 -0
- package/lib/typescript/utils/Uploader.d.ts.map +1 -0
- package/lib/typescript/utils/index.d.ts +2 -1
- package/lib/typescript/utils/index.d.ts.map +1 -1
- package/package.json +6 -4
- package/src/Image/index.tsx +5 -1
- package/src/Main.tsx +1 -15
- package/src/Spec/NativeCompressor.ts +14 -2
- package/src/Video/index.tsx +11 -84
- package/src/expo-plugin/compressor.ts +1 -1
- package/src/index.tsx +6 -3
- package/src/utils/Downloader.tsx +38 -0
- package/src/utils/Uploader.tsx +78 -0
- package/src/utils/index.tsx +2 -0
- package/android/src/main/java/com/reactnativecompressor/Audio/AudioCompressor.java +0 -605
- package/android/src/main/java/com/reactnativecompressor/CompressorModule.java +0 -220
- package/android/src/main/java/com/reactnativecompressor/CompressorPackage.java +0 -58
- package/android/src/main/java/com/reactnativecompressor/Image/ImageCompressor.java +0 -257
- package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageCompressorOptions.java +0 -69
- package/android/src/main/java/com/reactnativecompressor/Image/utils/ImageSize.java +0 -13
- package/android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploadHelper.java +0 -39
- package/android/src/main/java/com/reactnativecompressor/Utils/FileUplaoder/FileUploader.java +0 -129
- package/android/src/main/java/com/reactnativecompressor/Utils/MediaCache.java +0 -65
- package/android/src/main/java/com/reactnativecompressor/Utils/RealPathUtil.java +0 -210
- package/android/src/main/java/com/reactnativecompressor/Utils/Utils.java +0 -272
- package/android/src/main/java/com/reactnativecompressor/Video/AutoVideoCompression/AutoVideoCompression.java +0 -87
- package/android/src/main/java/com/reactnativecompressor/Video/VideoCompressorHelper.java +0 -182
- package/android/src/main/java/com/reactnativecompressor/Video/VideoModule.java +0 -112
- package/android/src/newarch/CompressorSpec.java +0 -9
- package/android/src/newarch/VideoCompressorSpec.java +0 -9
- package/android/src/oldarch/CompressorSpec.java +0 -34
- package/android/src/oldarch/VideoCompressorSpec.java +0 -26
- package/ios/Image/ImageCompressor.h +0 -22
- package/ios/Image/ImageCompressor.m +0 -540
- package/ios/Image/ImageCompressorOptions.h +0 -28
- package/ios/Image/ImageCompressorOptions.m +0 -97
- package/ios/Utils/Downloader.h +0 -16
- package/ios/Utils/Downloader.m +0 -150
- package/ios/Utils/MediaCache.h +0 -11
- package/ios/Utils/MediaCache.m +0 -51
- package/ios/Video/VideoCompressor.h +0 -5
- package/ios/Video/VideoCompressor.m +0 -1
- package/lib/commonjs/Spec/NativeVideoCompressor.js +0 -10
- package/lib/commonjs/Spec/NativeVideoCompressor.js.map +0 -1
- package/lib/module/Spec/NativeVideoCompressor.js +0 -3
- package/lib/module/Spec/NativeVideoCompressor.js.map +0 -1
- package/lib/typescript/Spec/NativeVideoCompressor.d.ts +0 -13
- package/lib/typescript/Spec/NativeVideoCompressor.d.ts.map +0 -1
- package/src/Spec/NativeVideoCompressor.ts +0 -14
package/ios/Compressor.mm
CHANGED
|
@@ -1,399 +1,33 @@
|
|
|
1
1
|
#import "Compressor.h"
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
#import "Image/ImageCompressor.h"
|
|
5
|
-
#import "Image/ImageCompressorOptions.h"
|
|
6
|
-
#import <React/RCTEventEmitter.h>
|
|
7
|
-
#import <AVFoundation/AVFoundation.h>
|
|
8
|
-
#import "MediaCache.h"
|
|
3
|
+
@interface RCT_EXTERN_MODULE(Compressor, RCTEventEmitter)
|
|
9
4
|
|
|
10
|
-
|
|
11
|
-
|
|
5
|
+
RCT_EXTERN_METHOD(image_compress: (NSString*) imagePath
|
|
6
|
+
withOptions: (NSDictionary*) optionMap
|
|
7
|
+
withResolver: (RCTPromiseResolveBlock) resolve
|
|
8
|
+
withRejecter: (RCTPromiseRejectBlock) reject)
|
|
12
9
|
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
RCT_EXTERN_METHOD(compress_audio: (NSString*) filePath
|
|
11
|
+
withOptions: (NSDictionary*) optionsDict
|
|
12
|
+
withResolver: (RCTPromiseResolveBlock) resolve
|
|
13
|
+
withRejecter: (RCTPromiseRejectBlock) reject)
|
|
15
14
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
metadatas = @[
|
|
20
|
-
@"albumName",
|
|
21
|
-
@"artist",
|
|
22
|
-
@"comment",
|
|
23
|
-
@"copyrights",
|
|
24
|
-
@"creationDate",
|
|
25
|
-
@"date",
|
|
26
|
-
@"encodedby",
|
|
27
|
-
@"genre",
|
|
28
|
-
@"language",
|
|
29
|
-
@"location",
|
|
30
|
-
@"lastModifiedDate",
|
|
31
|
-
@"performer",
|
|
32
|
-
@"publisher",
|
|
33
|
-
@"title"
|
|
34
|
-
];
|
|
35
|
-
}
|
|
36
|
-
return metadatas;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
- (instancetype)init {
|
|
40
|
-
self = [super init];
|
|
41
|
-
[ImageCompressor initCompressorInstance:self];
|
|
42
|
-
return self;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
RCT_EXPORT_MODULE()
|
|
46
|
-
|
|
47
|
-
+ (BOOL)requiresMainQueueSetup {
|
|
48
|
-
return NO;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
//Image
|
|
53
|
-
RCT_EXPORT_METHOD(
|
|
54
|
-
image_compress: (NSString*) imagePath
|
|
55
|
-
optionsDict: (NSDictionary*) optionsDict
|
|
56
|
-
resolver: (RCTPromiseResolveBlock) resolve
|
|
57
|
-
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
58
|
-
[self image_compress:imagePath optionMap:optionsDict resolve:resolve reject:reject];
|
|
59
|
-
}
|
|
15
|
+
RCT_EXTERN_METHOD(generateFilePath: (NSString*) _extension
|
|
16
|
+
withResolver: (RCTPromiseResolveBlock) resolve
|
|
17
|
+
withRejecter: (RCTPromiseRejectBlock) reject)
|
|
60
18
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
resolver: (RCTPromiseResolveBlock) resolve
|
|
66
|
-
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
67
|
-
[self compress_audio:filePath optionMap:optionsDict resolve:resolve reject:reject];
|
|
68
|
-
}
|
|
19
|
+
RCT_EXTERN_METHOD(getRealPath: (NSString*) path
|
|
20
|
+
withType: (NSString*) type
|
|
21
|
+
withResolver: (RCTPromiseResolveBlock) resolve
|
|
22
|
+
withRejecter: (RCTPromiseRejectBlock) reject)
|
|
69
23
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
resolver: (RCTPromiseResolveBlock) resolve
|
|
74
|
-
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
75
|
-
[self generateFilePath:extension resolve:resolve reject:reject];
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
RCT_EXPORT_METHOD(
|
|
79
|
-
getRealPath: (NSString*) path
|
|
80
|
-
type: (NSString*) type
|
|
81
|
-
resolver: (RCTPromiseResolveBlock) resolve
|
|
82
|
-
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
83
|
-
[self getRealPath:path type:type resolve:resolve reject:reject];
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
//general
|
|
87
|
-
RCT_EXPORT_METHOD(
|
|
88
|
-
getFileSize: (NSString*) filePath
|
|
89
|
-
resolver: (RCTPromiseResolveBlock) resolve
|
|
90
|
-
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
91
|
-
[self getFileSize:filePath resolve:resolve reject:reject];
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
RCT_EXPORT_METHOD(
|
|
96
|
-
getVideoMetaData: (NSString*) filePath
|
|
97
|
-
resolver: (RCTPromiseResolveBlock) resolve
|
|
98
|
-
rejecter: (RCTPromiseRejectBlock) reject) {
|
|
99
|
-
[self getVideoMetaData:filePath resolve:resolve reject:reject];
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
- (NSArray<NSString *>*)supportedEvents {
|
|
103
|
-
return @[@"downloadProgress"];
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
- (NSString *)getAudioQualityConstant:(NSString *)quality
|
|
107
|
-
{
|
|
108
|
-
NSMutableArray *audioQualityArray = [[NSMutableArray alloc]initWithObjects:@"low", @"medium", @"high", nil];
|
|
109
|
-
int index = [audioQualityArray indexOfObject:quality];
|
|
110
|
-
switch (index) {
|
|
111
|
-
case 0:
|
|
112
|
-
return AVAssetExportPresetLowQuality;
|
|
113
|
-
break;
|
|
114
|
-
case 1:
|
|
115
|
-
return AVAssetExportPresetMediumQuality;
|
|
116
|
-
break;
|
|
117
|
-
case 2:
|
|
118
|
-
return AVAssetExportPresetHighestQuality;
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
return AVAssetExportPresetMediumQuality;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
- (void)audo_compress_helper:(AVURLAsset *)avAsset qualityConstant:(NSString *)qualityConstant complete:(void (^)(NSString *mp3Path, BOOL finished))completeCallback {
|
|
125
|
-
NSString *path;
|
|
126
|
-
if ([avAsset.URL.scheme isEqualToString:AlAsset_Library_Scheme]) {
|
|
127
|
-
path = avAsset.URL.query;
|
|
128
|
-
if (path.length == 0) {
|
|
129
|
-
completeCallback(nil, NO);
|
|
130
|
-
return;
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
}else {
|
|
134
|
-
path = avAsset.URL.path;
|
|
135
|
-
if (!path || ![[NSFileManager defaultManager] fileExistsAtPath:path]) {
|
|
136
|
-
completeCallback(nil, NO);
|
|
137
|
-
return;
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
NSString *mp3Path = [ImageCompressor generateCacheFilePath:@"m4a"];;
|
|
142
|
-
|
|
143
|
-
if ([[NSFileManager defaultManager] fileExistsAtPath:mp3Path]) {
|
|
144
|
-
if (completeCallback)
|
|
145
|
-
completeCallback(mp3Path, YES);
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
NSURL *mp3Url;
|
|
150
|
-
NSArray *compatiblePresets = [AVAssetExportSession exportPresetsCompatibleWithAsset:avAsset];
|
|
151
|
-
|
|
152
|
-
if ([compatiblePresets containsObject:qualityConstant]) {
|
|
153
|
-
AVAssetExportSession *exportSession = [[AVAssetExportSession alloc]
|
|
154
|
-
initWithAsset:avAsset
|
|
155
|
-
presetName:AVAssetExportPresetAppleM4A];
|
|
156
|
-
|
|
157
|
-
mp3Url = [NSURL fileURLWithPath:mp3Path];
|
|
158
|
-
exportSession.outputURL = mp3Url;
|
|
159
|
-
exportSession.shouldOptimizeForNetworkUse = YES;
|
|
160
|
-
exportSession.outputFileType = AVFileTypeAppleM4A;
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
[exportSession exportAsynchronouslyWithCompletionHandler:^{
|
|
164
|
-
BOOL finished = NO;
|
|
165
|
-
switch ([exportSession status]) {
|
|
166
|
-
case AVAssetExportSessionStatusFailed:
|
|
167
|
-
NSLog(@"AVAssetExportSessionStatusFailed, error:%@.", exportSession.error);
|
|
168
|
-
break;
|
|
169
|
-
|
|
170
|
-
case AVAssetExportSessionStatusCancelled:
|
|
171
|
-
NSLog(@"AVAssetExportSessionStatusCancelled.");
|
|
172
|
-
break;
|
|
173
|
-
|
|
174
|
-
case AVAssetExportSessionStatusCompleted:
|
|
175
|
-
NSLog(@"AVAssetExportSessionStatusCompleted.");
|
|
176
|
-
finished = YES;
|
|
177
|
-
break;
|
|
178
|
-
|
|
179
|
-
case AVAssetExportSessionStatusUnknown:
|
|
180
|
-
NSLog(@"AVAssetExportSessionStatusUnknown");
|
|
181
|
-
break;
|
|
182
|
-
|
|
183
|
-
case AVAssetExportSessionStatusWaiting:
|
|
184
|
-
NSLog(@"AVAssetExportSessionStatusWaiting");
|
|
185
|
-
break;
|
|
186
|
-
|
|
187
|
-
case AVAssetExportSessionStatusExporting:
|
|
188
|
-
NSLog(@"AVAssetExportSessionStatusExporting");
|
|
189
|
-
break;
|
|
190
|
-
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (completeCallback)
|
|
194
|
-
completeCallback(mp3Path, finished);
|
|
195
|
-
}];
|
|
196
|
-
|
|
197
|
-
}
|
|
198
|
-
}
|
|
24
|
+
RCT_EXTERN_METHOD(getFileSize: (NSString*) filePath
|
|
25
|
+
withResolver: (RCTPromiseResolveBlock) resolve
|
|
26
|
+
withRejecter: (RCTPromiseRejectBlock) reject)
|
|
199
27
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
{
|
|
204
|
-
fileUrl=[fileUrl stringByReplacingOccurrencesOfString:@"file://"
|
|
205
|
-
withString:@""];
|
|
206
|
-
}
|
|
207
|
-
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
208
|
-
BOOL isDir;
|
|
209
|
-
if (![fileManager fileExistsAtPath:fileUrl isDirectory:&isDir] || isDir){
|
|
210
|
-
NSError *err = [NSError errorWithDomain:@"file not found" code:-15 userInfo:nil];
|
|
211
|
-
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
|
|
212
|
-
return;
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
NSDictionary *assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
|
|
216
|
-
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:fileUrl] options:assetOptions];
|
|
217
|
-
NSString *quality=[optionMap objectForKey:@"quality"];
|
|
218
|
-
NSString *qualityConstant=[self getAudioQualityConstant:quality];
|
|
219
|
-
[self audo_compress_helper:asset qualityConstant:qualityConstant complete:^(NSString *mp3Path, BOOL finished) {
|
|
220
|
-
if(finished)
|
|
221
|
-
{
|
|
222
|
-
resolve([NSString stringWithFormat: @"file:/%@", mp3Path]);
|
|
223
|
-
}
|
|
224
|
-
else
|
|
225
|
-
{
|
|
226
|
-
reject(@"Error", @"Something went wrong", nil);
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
}];
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
@catch (NSException *exception) {
|
|
233
|
-
reject(exception.name, exception.reason, nil);
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
- (void)generateFilePath:(NSString *)extension resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
|
|
238
|
-
@try {
|
|
239
|
-
NSString *outputUri =[ImageCompressor generateCacheFilePath:extension];
|
|
240
|
-
resolve(outputUri);
|
|
241
|
-
}
|
|
242
|
-
@catch (NSException *exception) {
|
|
243
|
-
reject(exception.name, exception.reason, nil);
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
- (void)getRealPath:(NSString *)path type:(NSString *)type resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
|
|
248
|
-
@try {
|
|
249
|
-
if([type isEqualToString:@"video"])
|
|
250
|
-
{
|
|
251
|
-
[ImageCompressor getAbsoluteVideoPath:path uuid:@"" completionHandler:^(NSString* absoluteImagePath){
|
|
252
|
-
resolve(absoluteImagePath);
|
|
253
|
-
}];
|
|
254
|
-
}
|
|
255
|
-
else
|
|
256
|
-
{
|
|
257
|
-
[ImageCompressor getAbsoluteImagePath:path uuid:@"test" completionHandler:^(NSString* absoluteImagePath){
|
|
258
|
-
resolve(absoluteImagePath);
|
|
259
|
-
}];
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
@catch (NSException *exception) {
|
|
263
|
-
reject(exception.name, exception.reason, nil);
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
- (void)getVideoMetaData:(NSString *)filePath resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
|
|
268
|
-
@try {
|
|
269
|
-
[ImageCompressor getAbsoluteVideoPath:filePath uuid:@"" completionHandler:^(NSString *absoluteImagePath) {
|
|
270
|
-
if([absoluteImagePath containsString:@"file://"])
|
|
271
|
-
{
|
|
272
|
-
absoluteImagePath=[absoluteImagePath stringByReplacingOccurrencesOfString:@"file://"
|
|
273
|
-
withString:@""];
|
|
274
|
-
}
|
|
275
|
-
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
276
|
-
|
|
277
|
-
BOOL isDir;
|
|
278
|
-
if (![fileManager fileExistsAtPath:absoluteImagePath isDirectory:&isDir] || isDir){
|
|
279
|
-
NSError *err = [NSError errorWithDomain:@"file not found" code:-15 userInfo:nil];
|
|
280
|
-
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
|
|
281
|
-
return;
|
|
282
|
-
}
|
|
283
|
-
NSDictionary *attrs = [fileManager attributesOfItemAtPath: absoluteImagePath error: NULL];
|
|
284
|
-
UInt32 fileSize = [attrs fileSize];
|
|
285
|
-
NSString *fileSizeString = [@(fileSize) stringValue];
|
|
286
|
-
|
|
287
|
-
NSMutableDictionary *result = [NSMutableDictionary new];
|
|
288
|
-
NSDictionary *assetOptions = @{AVURLAssetPreferPreciseDurationAndTimingKey: @YES};
|
|
289
|
-
AVURLAsset *asset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:absoluteImagePath] options:assetOptions];\
|
|
290
|
-
AVAssetTrack *avAsset = [[asset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0];
|
|
291
|
-
CGSize size = [avAsset naturalSize];
|
|
292
|
-
NSString *extension = [[absoluteImagePath lastPathComponent] pathExtension];
|
|
293
|
-
CMTime time = [asset duration];
|
|
294
|
-
int seconds = ceil(time.value/time.timescale);
|
|
295
|
-
[result setObject:[NSString stringWithFormat: @"%.2f", size.width] forKey:@"width"];
|
|
296
|
-
[result setObject:[NSString stringWithFormat: @"%.2f", size.height] forKey:@"height"];
|
|
297
|
-
[result setObject:extension forKey:@"extension"];
|
|
298
|
-
[result setObject:fileSizeString forKey:@"size"];
|
|
299
|
-
[result setObject:[@(seconds) stringValue] forKey:@"duration"];
|
|
300
|
-
NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata", nil];
|
|
301
|
-
[asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{
|
|
302
|
-
// string keys
|
|
303
|
-
for (NSString *key in [self metadatas]) {
|
|
304
|
-
NSArray *items = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata
|
|
305
|
-
withKey:key
|
|
306
|
-
keySpace:AVMetadataKeySpaceCommon];
|
|
307
|
-
for (AVMetadataItem *item in items) {
|
|
308
|
-
[result setObject:item.value forKey:key];
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
resolve(result);
|
|
312
|
-
}];
|
|
313
|
-
}];
|
|
314
|
-
}
|
|
315
|
-
@catch (NSException *exception) {
|
|
316
|
-
reject(exception.name, exception.reason, nil);
|
|
317
|
-
}
|
|
318
|
-
}
|
|
319
|
-
|
|
320
|
-
- (void)image_compress:(NSString *)imagePath optionMap:(NSDictionary *)optionMap resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
|
|
321
|
-
@try {
|
|
322
|
-
ImageCompressorOptions *options = [ImageCompressorOptions fromDictionary:optionMap];
|
|
323
|
-
[ImageCompressor getAbsoluteImagePath:imagePath uuid:options.uuid completionHandler:^(NSString* absoluteImagePath){
|
|
324
|
-
if(options.autoCompress)
|
|
325
|
-
{
|
|
326
|
-
NSString *result = [ImageCompressor autoCompressHandler:absoluteImagePath options:options];
|
|
327
|
-
resolve(result);
|
|
328
|
-
}
|
|
329
|
-
else
|
|
330
|
-
{
|
|
331
|
-
NSString *result = [ImageCompressor manualCompressHandler:absoluteImagePath options:options];
|
|
332
|
-
resolve(result);
|
|
333
|
-
}
|
|
334
|
-
[MediaCache removeCompletedImagePath:absoluteImagePath];
|
|
335
|
-
}];
|
|
336
|
-
|
|
337
|
-
}
|
|
338
|
-
@catch (NSException *exception) {
|
|
339
|
-
reject(exception.name, exception.reason, nil);
|
|
340
|
-
}
|
|
341
|
-
}
|
|
342
|
-
|
|
343
|
-
- (void)getFileSize:(NSString *)filePath resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject {
|
|
344
|
-
@try {
|
|
345
|
-
if([filePath hasPrefix:@"http://"]||[filePath hasPrefix:@"https://"])
|
|
346
|
-
{
|
|
347
|
-
[ImageCompressor getFileSizeFromURL:filePath completion:^(NSNumber *fileSize, NSError *error) {
|
|
348
|
-
if (error) {
|
|
349
|
-
NSLog(@"Error: %@", error.localizedDescription);
|
|
350
|
-
} else {
|
|
351
|
-
NSLog(@"File size: %@", fileSize);
|
|
352
|
-
resolve(fileSize);
|
|
353
|
-
}
|
|
354
|
-
}];
|
|
355
|
-
|
|
356
|
-
}
|
|
357
|
-
else
|
|
358
|
-
{
|
|
359
|
-
if([filePath containsString:@"file://"])
|
|
360
|
-
{
|
|
361
|
-
filePath=[filePath stringByReplacingOccurrencesOfString:@"file://"
|
|
362
|
-
withString:@""];
|
|
363
|
-
}
|
|
364
|
-
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
365
|
-
BOOL isDir;
|
|
366
|
-
if (![fileManager fileExistsAtPath:filePath isDirectory:&isDir] || isDir){
|
|
367
|
-
NSError *err = [NSError errorWithDomain:@"file not found" code:-15 userInfo:nil];
|
|
368
|
-
reject([NSString stringWithFormat: @"%lu", (long)err.code], err.localizedDescription, err);
|
|
369
|
-
return;
|
|
370
|
-
}
|
|
371
|
-
NSDictionary *attrs = [fileManager attributesOfItemAtPath: filePath error: NULL];
|
|
372
|
-
UInt32 fileSize = [attrs fileSize];
|
|
373
|
-
NSString *fileSizeString = [@(fileSize) stringValue];
|
|
374
|
-
resolve(fileSizeString);
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
@catch (NSException *exception) {
|
|
379
|
-
reject(exception.name, exception.reason, nil);
|
|
380
|
-
}
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
// Don't compile this code when we build for the old architecture.
|
|
384
|
-
#ifdef RCT_NEW_ARCH_ENABLED
|
|
385
|
-
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
386
|
-
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
387
|
-
{
|
|
388
|
-
return std::make_shared<facebook::react::NativeCompressorSpecJSI>(params);
|
|
389
|
-
}
|
|
390
|
-
#endif
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
@end
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
@interface RCT_EXTERN_MODULE(VideoCompressor, RCTEventEmitter)
|
|
28
|
+
RCT_EXTERN_METHOD(getVideoMetaData: (NSString*) filePath
|
|
29
|
+
withResolver: (RCTPromiseResolveBlock) resolve
|
|
30
|
+
withRejecter: (RCTPromiseRejectBlock) reject)
|
|
397
31
|
|
|
398
32
|
RCT_EXTERN_METHOD(compress:(NSString *)fileUrl
|
|
399
33
|
withOptions:(NSDictionary *)options
|
|
@@ -405,6 +39,11 @@ RCT_EXTERN_METHOD(upload:(NSString *)fileUrl
|
|
|
405
39
|
withResolver:(RCTPromiseResolveBlock)resolve
|
|
406
40
|
withRejecter:(RCTPromiseRejectBlock)reject)
|
|
407
41
|
|
|
42
|
+
RCT_EXTERN_METHOD(download:(NSString *)fileUrl
|
|
43
|
+
withOptions:(NSDictionary *)options
|
|
44
|
+
withResolver:(RCTPromiseResolveBlock)resolve
|
|
45
|
+
withRejecter:(RCTPromiseRejectBlock)reject)
|
|
46
|
+
|
|
408
47
|
RCT_EXTERN_METHOD(activateBackgroundTask: (NSDictionary *)options
|
|
409
48
|
withResolver:(RCTPromiseResolveBlock)resolve
|
|
410
49
|
withRejecter:(RCTPromiseRejectBlock)reject)
|
|
@@ -420,7 +59,7 @@ RCT_EXTERN_METHOD(cancelCompression:(NSString *)uuid)
|
|
|
420
59
|
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
421
60
|
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
422
61
|
{
|
|
423
|
-
return std::make_shared<facebook::react::
|
|
62
|
+
return std::make_shared<facebook::react::NativeCompressorSpecJSI>(params);
|
|
424
63
|
}
|
|
425
64
|
#endif
|
|
426
65
|
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import Foundation
|
|
2
|
+
import AVFoundation
|
|
3
|
+
|
|
4
|
+
let videoCompressor = VideoCompressor()
|
|
5
|
+
let uploader=Uploader()
|
|
6
|
+
@objc(Compressor)
|
|
7
|
+
class Compressor: RCTEventEmitter {
|
|
8
|
+
override static func moduleName() -> String {
|
|
9
|
+
return "Compressor"
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
override init() {
|
|
13
|
+
super.init()
|
|
14
|
+
EventEmitterHandler.initCompressorInstance(self)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
override func stopObserving() -> Void {
|
|
18
|
+
EventEmitterHandler.stopObserving()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
override func startObserving() -> Void {
|
|
22
|
+
EventEmitterHandler.startObserving()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
override static func requiresMainQueueSetup() -> Bool {
|
|
26
|
+
return false
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
override func supportedEvents() -> [String] {
|
|
30
|
+
return ["downloadProgress", "videoCompressProgress", "uploadProgress", "backgroundTaskExpired"]
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
@objc(image_compress:withOptions:withResolver:withRejecter:)
|
|
34
|
+
func image_compress(_ imagePath: String, optionMap: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
35
|
+
ImageMain.image_compress(imagePath, optionMap: optionMap, resolve: resolve, reject: reject)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@objc(compress_audio:withOptions:withResolver:withRejecter:)
|
|
39
|
+
func compress_audio(_ fileUrl: String, optionMap: NSDictionary, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
40
|
+
AudioMain.compress_audio(fileUrl, optionMap: optionMap, resolve: resolve, reject: reject)
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
@objc(generateFilePath:withResolver:withRejecter:)
|
|
44
|
+
func generateFilePath(_ _extension: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
45
|
+
Utils.generateFilePath(_extension, resolve: resolve, reject: reject)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
@objc(getRealPath:withType:withResolver:withRejecter:)
|
|
49
|
+
func getRealPath(_ path: String, type: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
50
|
+
Utils.getRealPath(path, type: type, resolve: resolve, reject: reject)
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
@objc(getFileSize:withResolver:withRejecter:)
|
|
54
|
+
func getFileSize(_ filePath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
55
|
+
Utils.getFileSize(filePath, resolve: resolve, reject: reject)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
@objc(getVideoMetaData:withResolver:withRejecter:)
|
|
59
|
+
func getVideoMetaData(_ filePath: String, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
|
|
60
|
+
videoCompressor.getVideoMetaData(filePath,resolve: resolve,reject: reject)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
@objc(activateBackgroundTask:withResolver:withRejecter:)
|
|
65
|
+
func activateBackgroundTask(options: [String: Any], resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {
|
|
66
|
+
videoCompressor.activateBackgroundTask(options: options,resolve: resolve,reject: reject)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
@objc(deactivateBackgroundTask:withResolver:withRejecter:)
|
|
70
|
+
func deactivateBackgroundTask(options: [String: Any], resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {
|
|
71
|
+
videoCompressor.deactivateBackgroundTask(options: options,resolve: resolve,reject: reject)
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
@objc(compress:withOptions:withResolver:withRejecter:)
|
|
75
|
+
func compress(fileUrl: String, options: [String: Any], resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {
|
|
76
|
+
videoCompressor.compress(fileUrl: fileUrl, options: options, resolve: resolve, reject: reject)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
@objc(cancelCompression:)
|
|
80
|
+
func cancelCompression(uuid: String) -> Void {
|
|
81
|
+
videoCompressor.cancelCompression(uuid: uuid)
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
@objc(upload:withOptions:withResolver:withRejecter:)
|
|
85
|
+
func upload(filePath: String, options: [String: Any], resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {
|
|
86
|
+
uploader.upload(filePath: filePath, options: options, resolve: resolve, reject: reject)
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
@objc(download:withOptions:withResolver:withRejecter:)
|
|
90
|
+
func download(filePath: String, options: [String: Any], resolve:@escaping RCTPromiseResolveBlock, reject:@escaping RCTPromiseRejectBlock) -> Void {
|
|
91
|
+
Downloader.downloadFileAndSaveToCache(filePath, uuid: options["uuid"] as! String,progressDivider: options["progressDivider"] as? Int ?? 0) { downloadedPath in
|
|
92
|
+
resolve(downloadedPath)
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
}
|