aliyun-oss-react-native

RNAliyunOSS+DOWNLOAD.m 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. //
  2. // RNAliyunOSS+DOWNLOAD.m
  3. // aliyun-oss-rn-sdk
  4. //
  5. // Created by 罗章 on 2018/5/8.
  6. //
  7. #import "RNAliyunOSS+DOWNLOAD.h"
  8. #import <React/RCTLog.h>
  9. #import <React/RCTConvert.h>
  10. @implementation RNAliyunOSS (DOWNLOAD)
  11. /**
  12. Asynchronous downloading
  13. */
  14. RCT_REMAP_METHOD(asyncDownload, asyncDownloadWithBucketName:(NSString *)bucketName objectKey:(NSString *)objectKey filepath:(NSString *)filepath options:(NSDictionary*)options resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
  15. OSSGetObjectRequest * get = [OSSGetObjectRequest new];
  16. //required fields
  17. get.bucketName = bucketName;
  18. get.objectKey = objectKey;
  19. //图片处理情况
  20. NSString *xOssProcess = [RCTConvert NSString:options[@"x-oss-process"]];
  21. get.xOssProcess = xOssProcess;
  22. //optional fields
  23. get.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
  24. NSLog(@"%lld, %lld, %lld", bytesWritten, totalBytesWritten, totalBytesExpectedToWrite);
  25. // Only send events if anyone is listening
  26. if (self.hasListeners) {
  27. [self sendEventWithName:@"downloadProgress" body:@{@"bytesWritten":[NSString stringWithFormat:@"%lld",bytesWritten],
  28. @"currentSize": [NSString stringWithFormat:@"%lld",totalBytesWritten],
  29. @"totalSize": [NSString stringWithFormat:@"%lld",totalBytesExpectedToWrite]}];
  30. }
  31. };
  32. if (![[filepath oss_trim] isEqualToString:@""]) {
  33. get.downloadToFileURL = [NSURL fileURLWithPath:[filepath stringByAppendingPathComponent:objectKey]];
  34. } else {
  35. NSString *docDir = [self getDocumentDirectory];
  36. get.downloadToFileURL = [NSURL fileURLWithPath:[docDir stringByAppendingPathComponent:objectKey]];
  37. }
  38. OSSTask * getTask = [self.client getObject:get];
  39. [getTask continueWithBlock:^id(OSSTask *task) {
  40. if (!task.error) {
  41. NSLog(@"download object success!");
  42. OSSGetObjectResult *result = task.result;
  43. NSLog(@"download dota length: %lu", [result.downloadedData length]);
  44. resolve([get.downloadToFileURL absoluteString]);
  45. } else {
  46. NSLog(@"download object failed, error: %@" ,task.error);
  47. reject(@"Error", @"Download failed", task.error);
  48. }
  49. return nil;
  50. }];
  51. }
  52. @end