123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188 |
-
-
-
- #import "RNAliyunOSS.h"
- #import <React/RCTLog.h>
- #import <React/RCTConvert.h>
- @import Photos;
- @import MobileCoreServices;
-
-
- @implementation RNAliyunOSS
-
-
- -(void)startObserving {
- _hasListeners = YES;
-
- }
-
-
-
- -(void)stopObserving {
- _hasListeners = NO;
-
- }
-
-
-
- -(NSArray<NSString *> *)supportedEvents
- {
- return @[@"uploadProgress", @"downloadProgress"];
- }
-
-
-
- -(NSString *)getDocumentDirectory {
- NSString * path = NSHomeDirectory();
- NSLog(@"NSHomeDirectory:%@",path);
- NSString * userName = NSUserName();
- NSString * rootPath = NSHomeDirectoryForUser(userName);
- NSLog(@"NSHomeDirectoryForUser:%@",rootPath);
- NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString * documentsDirectory = [paths objectAtIndex:0];
- return documentsDirectory;
- }
-
-
-
- -(NSString*)getTemporaryDirectory {
- NSString *TMP_DIRECTORY = @"react-native/";
- NSString *filepath = [NSTemporaryDirectory() stringByAppendingString:TMP_DIRECTORY];
-
- BOOL isDir;
- BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:filepath isDirectory:&isDir];
- if (!exists) {
- [[NSFileManager defaultManager] createDirectoryAtPath: filepath
- withIntermediateDirectories:YES attributes:nil error:nil];
- }
-
- return filepath;
- }
-
-
-
- -(void)initConfiguration:(NSDictionary *)configuration {
- _clientConfiguration = [OSSClientConfiguration new];
- _clientConfiguration.maxRetryCount = [RCTConvert int:configuration[@"maxRetryCount"]];
- _clientConfiguration.timeoutIntervalForRequest = [RCTConvert double:configuration[@"timeoutIntervalForRequest"]];
- _clientConfiguration.timeoutIntervalForResource = [RCTConvert double:configuration[@"timeoutIntervalForResource"]];
- }
-
-
-
- -(void)beginUploadingWithFilepath:(NSString *)filepath resultBlock:(void (^) (NSData *))callback {
-
-
- if ([filepath hasPrefix:@"assets-library://"]) {
- PHAsset *asset = [PHAsset fetchAssetsWithALAssetURLs:@[filepath] options:nil].firstObject;
- [self convertToNSDataFromAsset:asset withHandler:callback];
-
- } else if ([filepath hasPrefix:@"localIdentifier://"]) {
- NSString *localIdentifier = [filepath stringByReplacingOccurrencesOfString:@"localIdentifier://" withString:@""];
- PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil].firstObject;
- [self convertToNSDataFromAsset:asset withHandler:callback];
-
- } else {
- filepath = [filepath stringByReplacingOccurrencesOfString:@"file://" withString:@""];
- NSData *data = [NSData dataWithContentsOfFile:filepath];
- callback(data);
- }
- }
-
-
- -(void)convertToNSDataFromAsset:(PHAsset *)asset withHandler:(void (^) (NSData *))handler
- {
- PHImageManager *imageManager = [PHImageManager defaultManager];
-
- switch (asset.mediaType) {
-
- case PHAssetMediaTypeImage: {
- PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
- options.networkAccessAllowed = YES;
- [imageManager requestImageDataForAsset:asset options:options resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
- if ([dataUTI isEqualToString:(__bridge NSString *)kUTTypeJPEG]) {
- handler(imageData);
- } else {
-
- CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
- NSDictionary *imageInfo = (__bridge NSDictionary*)CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
- NSDictionary *metadata = [imageInfo copy];
-
- NSMutableData *imageDataJPEG = [NSMutableData data];
-
- CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)imageDataJPEG, kUTTypeJPEG, 1, NULL);
- CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metadata);
- CGImageDestinationFinalize(destination);
-
- handler([NSData dataWithData:imageDataJPEG]);
- }
- }];
- break;
- }
-
- case PHAssetMediaTypeVideo:{
- PHVideoRequestOptions *options = [[PHVideoRequestOptions alloc] init];
- options.networkAccessAllowed = YES;
- [imageManager requestExportSessionForVideo:asset options:options exportPreset:AVAssetExportPresetHighestQuality resultHandler:^(AVAssetExportSession * _Nullable exportSession, NSDictionary * _Nullable info) {
-
-
- NSString *filePath = [[self getTemporaryDirectory] stringByAppendingString:[[NSUUID UUID] UUIDString]];
- filePath = [filePath stringByAppendingString:@".mp4"];
-
- exportSession.shouldOptimizeForNetworkUse = YES;
- exportSession.outputFileType = AVFileTypeMPEG4;
- exportSession.outputURL = [NSURL fileURLWithPath:filePath];
-
- [exportSession exportAsynchronouslyWithCompletionHandler:^{
- handler([NSData dataWithContentsOfFile:filePath]);
- }];
- }];
- break;
- }
- default:
- break;
- }
- }
-
-
- RCT_EXPORT_MODULE()
-
- @end
|