123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- //
- // OSSCompat.m
- // oss_ios_sdk_new
- //
- // Created by zhouzhuo on 9/10/15.
- // Copyright (c) 2015 aliyun.com. All rights reserved.
- //
- #import "OSSDefine.h"
- #import "OSSCompat.h"
- #import "OSSBolts.h"
- #import "OSSModel.h"
- @implementation OSSClient (Compat)
- - (OSSTaskHandler *)uploadData:(NSData *)data
- withContentType:(NSString *)contentType
- withObjectMeta:(NSDictionary *)meta
- toBucketName:(NSString *)bucketName
- toObjectKey:(NSString *)objectKey
- onCompleted:(void(^)(BOOL, NSError *))onCompleted
- onProgress:(void(^)(float progress))onProgress {
- OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
- [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withSuccessBlock:^id(OSSTask *task) {
- OSSPutObjectRequest * put = [OSSPutObjectRequest new];
- put.bucketName = bucketName;
- put.objectKey = objectKey;
- put.objectMeta = meta;
- put.uploadingData = data;
- put.contentType = contentType;
- put.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
- if (totalBytesExpectedToSend) {
- onProgress((float)totalBytesSent / totalBytesExpectedToSend);
- }
- };
- [bcts.token registerCancellationObserverWithBlock:^{
- [put cancel];
- }];
- OSSTask * putTask = [self putObject:put];
- [putTask waitUntilFinished];
- onProgress(1.0f);
- return putTask;
- }] continueWithBlock:^id(OSSTask *task) {
- if (task.error) {
- onCompleted(NO, task.error);
- } else {
- onCompleted(YES, nil);
- }
- return nil;
- }];
- return bcts;
- }
- - (OSSTaskHandler *)downloadToDataFromBucket:(NSString *)bucketName
- objectKey:(NSString *)objectKey
- onCompleted:(void (^)(NSData *, NSError *))onCompleted
- onProgress:(void (^)(float))onProgress {
- OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
- [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withBlock:^id(OSSTask *task) {
- OSSGetObjectRequest * get = [OSSGetObjectRequest new];
- get.bucketName = bucketName;
- get.objectKey = objectKey;
- get.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
- if (totalBytesExpectedToWrite) {
- onProgress((float)totalBytesWritten / totalBytesExpectedToWrite);
- }
- };
- [bcts.token registerCancellationObserverWithBlock:^{
- [get cancel];
- }];
- OSSTask * getTask = [self getObject:get];
- [getTask waitUntilFinished];
- onProgress(1.0f);
- return getTask;
- }] continueWithBlock:^id(OSSTask *task) {
- if (task.error) {
- onCompleted(nil, task.error);
- } else {
- OSSGetObjectResult * result = task.result;
- onCompleted(result.downloadedData, nil);
- }
- return nil;
- }];
- return bcts;
- }
- - (OSSTaskHandler *)downloadToFileFromBucket:(NSString *)bucketName
- objectKey:(NSString *)objectKey
- toFile:(NSString *)filePath
- onCompleted:(void (^)(BOOL, NSError *))onCompleted
- onProgress:(void (^)(float))onProgress {
- OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
- [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withBlock:^id(OSSTask *task) {
- OSSGetObjectRequest * get = [OSSGetObjectRequest new];
- get.bucketName = bucketName;
- get.objectKey = objectKey;
- get.downloadToFileURL = [NSURL fileURLWithPath:filePath];
- get.downloadProgress = ^(int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) {
- if (totalBytesExpectedToWrite) {
- onProgress((float)totalBytesWritten / totalBytesExpectedToWrite);
- }
- };
- [bcts.token registerCancellationObserverWithBlock:^{
- [get cancel];
- }];
- OSSTask * getTask = [self getObject:get];
- [getTask waitUntilFinished];
- onProgress(1.0f);
- return getTask;
- }] continueWithBlock:^id(OSSTask *task) {
- if (task.error) {
- onCompleted(NO, task.error);
- } else {
- onCompleted(YES, nil);
- }
- return nil;
- }];
-
- return bcts;
- }
- - (void)deleteObjectInBucket:(NSString *)bucketName
- objectKey:(NSString *)objectKey
- onCompleted:(void (^)(BOOL, NSError *))onCompleted {
- [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withBlock:^id(OSSTask *task) {
- OSSDeleteObjectRequest * delete = [OSSDeleteObjectRequest new];
- delete.bucketName = bucketName;
- delete.objectKey = objectKey;
- OSSTask * deleteTask = [self deleteObject:delete];
- [deleteTask waitUntilFinished];
- return deleteTask;
- }] continueWithBlock:^id(OSSTask *task) {
- if (task.error) {
- onCompleted(NO, task.error);
- } else {
- onCompleted(YES, nil);
- }
- return nil;
- }];
- }
- - (OSSTaskHandler *)uploadFile:(NSString *)filePath
- withContentType:(NSString *)contentType
- withObjectMeta:(NSDictionary *)meta
- toBucketName:(NSString *)bucketName
- toObjectKey:(NSString *)objectKey
- onCompleted:(void (^)(BOOL, NSError *))onCompleted
- onProgress:(void (^)(float))onProgress {
- OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
- [[[OSSTask taskWithResult:nil] continueWithExecutor:self.ossOperationExecutor withSuccessBlock:^id(OSSTask *task) {
- OSSPutObjectRequest * put = [OSSPutObjectRequest new];
- put.bucketName = bucketName;
- put.objectKey = objectKey;
- put.objectMeta = meta;
- put.uploadingFileURL = [NSURL fileURLWithPath:filePath];
- put.contentType = contentType;
- put.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
- if (totalBytesExpectedToSend) {
- onProgress((float)totalBytesSent / totalBytesExpectedToSend);
- }
- };
- [bcts.token registerCancellationObserverWithBlock:^{
- [put cancel];
- }];
- OSSTask * putTask = [self putObject:put];
- [putTask waitUntilFinished];
- onProgress(1.0f);
- return putTask;
- }] continueWithBlock:^id(OSSTask *task) {
- if (task.error) {
- onCompleted(NO, task.error);
- } else {
- onCompleted(YES, nil);
- }
- return nil;
- }];
- return bcts;
- }
- - (OSSTaskHandler *)resumableUploadFile:(NSString *)filePath
- withContentType:(NSString *)contentType
- withObjectMeta:(NSDictionary *)meta
- toBucketName:(NSString *)bucketName
- toObjectKey:(NSString *)objectKey
- onCompleted:(void(^)(BOOL, NSError *))onComplete
- onProgress:(void(^)(float progress))onProgress {
- OSSTaskHandler * bcts = [OSSCancellationTokenSource cancellationTokenSource];
- [[[OSSTask taskWithResult:nil] continueWithBlock:^id(OSSTask *task) {
- NSURL * fileURL = [NSURL fileURLWithPath:filePath];
- NSDate * lastModified;
- NSError * error;
- [fileURL getResourceValue:&lastModified forKey:NSURLContentModificationDateKey error:&error];
- if (error) {
- return [OSSTask taskWithError:error];
- }
- OSSResumableUploadRequest * resumableUpload = [OSSResumableUploadRequest new];
- resumableUpload.bucketName = bucketName;
- resumableUpload.deleteUploadIdOnCancelling = NO;//cancel not delete record file
- resumableUpload.contentType = contentType;
- resumableUpload.completeMetaHeader = meta;
- NSString *cachesDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
- resumableUpload.recordDirectoryPath = cachesDir; //default record file path
- resumableUpload.uploadingFileURL = fileURL;
- resumableUpload.objectKey = objectKey;
- resumableUpload.uploadId = task.result;
- resumableUpload.uploadingFileURL = [NSURL fileURLWithPath:filePath];
- __weak OSSResumableUploadRequest * weakRef = resumableUpload;
- resumableUpload.uploadProgress = ^(int64_t bytesSent, int64_t totalBytesSent, int64_t totalBytesExpectedToSend) {
- onProgress((float)totalBytesSent/totalBytesExpectedToSend);
- if (bcts.token.isCancellationRequested || bcts.isCancellationRequested) {
- [weakRef cancel];
- }
- OSSLogDebugNoFile(@"%lld %lld %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
- };
- return [self resumableUpload:resumableUpload];
- }] continueWithBlock:^id(OSSTask *task) {
- if (task.cancelled) {
- onComplete(NO, [NSError errorWithDomain:OSSClientErrorDomain
- code:OSSClientErrorCodeTaskCancelled
- userInfo:@{OSSErrorMessageTOKEN: @"This task is cancelled"}]);
- } else if (task.error) {
- onComplete(NO, task.error);
- } else if (task.faulted) {
- onComplete(NO, [NSError errorWithDomain:OSSClientErrorDomain
- code:OSSClientErrorCodeExcpetionCatched
- userInfo:@{OSSErrorMessageTOKEN: [NSString stringWithFormat:@"Catch exception - %@", task.exception]}]);
- } else {
- onComplete(YES, nil);
- }
- return nil;
- }];
- return bcts;
- }
- @end
|