QNFormUpload.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // QNFormUpload.m
  3. // QiniuSDK
  4. //
  5. // Created by bailong on 15/1/4.
  6. // Copyright (c) 2015年 Qiniu. All rights reserved.
  7. //
  8. #import "QNFormUpload.h"
  9. #import "QNConfiguration.h"
  10. #import "QNCrc32.h"
  11. #import "QNRecorderDelegate.h"
  12. #import "QNResponseInfo.h"
  13. #import "QNUploadManager.h"
  14. #import "QNUploadOption+Private.h"
  15. #import "QNUrlSafeBase64.h"
  16. @interface QNFormUpload ()
  17. @property (nonatomic, strong) NSData *data;
  18. @property (nonatomic, strong) id<QNHttpDelegate> httpManager;
  19. @property (nonatomic) int retryTimes;
  20. @property (nonatomic, strong) NSString *key;
  21. @property (nonatomic, strong) QNUpToken *token;
  22. @property (nonatomic, strong) QNUploadOption *option;
  23. @property (nonatomic, strong) QNUpCompletionHandler complete;
  24. @property (nonatomic, strong) QNConfiguration *config;
  25. @property (nonatomic, strong) NSString *fileName;
  26. @property (nonatomic) float previousPercent;
  27. @property (nonatomic, strong) NSString *access; //AK
  28. @end
  29. @implementation QNFormUpload
  30. - (instancetype)initWithData:(NSData *)data
  31. withKey:(NSString *)key
  32. withFileName:(NSString *)fileName
  33. withToken:(QNUpToken *)token
  34. withCompletionHandler:(QNUpCompletionHandler)block
  35. withOption:(QNUploadOption *)option
  36. withHttpManager:(id<QNHttpDelegate>)http
  37. withConfiguration:(QNConfiguration *)config {
  38. if (self = [super init]) {
  39. _data = data;
  40. _key = key;
  41. _token = token;
  42. _option = option != nil ? option : [QNUploadOption defaultOptions];
  43. _complete = block;
  44. _httpManager = http;
  45. _config = config;
  46. _fileName = fileName != nil ? fileName : @"?";
  47. _previousPercent = 0;
  48. _access = token.access;
  49. }
  50. return self;
  51. }
  52. - (void)put {
  53. NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
  54. if (_key) {
  55. parameters[@"key"] = _key;
  56. }
  57. parameters[@"token"] = _token.token;
  58. [parameters addEntriesFromDictionary:_option.params];
  59. parameters[@"crc32"] = [NSString stringWithFormat:@"%u", (unsigned int)[QNCrc32 data:_data]];
  60. QNInternalProgressBlock p = ^(long long totalBytesWritten, long long totalBytesExpectedToWrite) {
  61. float percent = (float)totalBytesWritten / (float)totalBytesExpectedToWrite;
  62. if (percent > 0.95) {
  63. percent = 0.95;
  64. }
  65. if (percent > _previousPercent) {
  66. _previousPercent = percent;
  67. } else {
  68. percent = _previousPercent;
  69. }
  70. _option.progressHandler(_key, percent);
  71. };
  72. __block NSString *upHost = [_config.zone up:_token isHttps:_config.useHttps frozenDomain:nil];
  73. QNCompleteBlock complete = ^(QNResponseInfo *info, NSDictionary *resp) {
  74. if (info.isOK) {
  75. _option.progressHandler(_key, 1.0);
  76. }
  77. if (info.isOK || !info.couldRetry) {
  78. _complete(info, _key, resp);
  79. return;
  80. }
  81. if (_option.cancellationSignal()) {
  82. _complete([QNResponseInfo cancel], _key, nil);
  83. return;
  84. }
  85. __block NSString *nextHost = upHost;
  86. if (info.isConnectionBroken || info.needSwitchServer) {
  87. nextHost = [_config.zone up:_token isHttps:_config.useHttps frozenDomain:nextHost];
  88. }
  89. QNCompleteBlock retriedComplete = ^(QNResponseInfo *info, NSDictionary *resp) {
  90. if (info.isOK) {
  91. _option.progressHandler(_key, 1.0);
  92. }
  93. if (info.isOK || !info.couldRetry) {
  94. _complete(info, _key, resp);
  95. return;
  96. }
  97. if (_option.cancellationSignal()) {
  98. _complete([QNResponseInfo cancel], _key, nil);
  99. return;
  100. }
  101. NSString *thirdHost = nextHost;
  102. if (info.isConnectionBroken || info.needSwitchServer) {
  103. thirdHost = [_config.zone up:_token isHttps:_config.useHttps frozenDomain:nextHost];
  104. }
  105. QNCompleteBlock thirdComplete = ^(QNResponseInfo *info, NSDictionary *resp) {
  106. if (info.isOK) {
  107. _option.progressHandler(_key, 1.0);
  108. }
  109. _complete(info, _key, resp);
  110. };
  111. [_httpManager multipartPost:thirdHost
  112. withData:_data
  113. withParams:parameters
  114. withFileName:_fileName
  115. withMimeType:_option.mimeType
  116. withCompleteBlock:thirdComplete
  117. withProgressBlock:p
  118. withCancelBlock:_option.cancellationSignal
  119. withAccess:_access];
  120. };
  121. [_httpManager multipartPost:nextHost
  122. withData:_data
  123. withParams:parameters
  124. withFileName:_fileName
  125. withMimeType:_option.mimeType
  126. withCompleteBlock:retriedComplete
  127. withProgressBlock:p
  128. withCancelBlock:_option.cancellationSignal
  129. withAccess:_access];
  130. };
  131. [_httpManager multipartPost:upHost
  132. withData:_data
  133. withParams:parameters
  134. withFileName:_fileName
  135. withMimeType:_option.mimeType
  136. withCompleteBlock:complete
  137. withProgressBlock:p
  138. withCancelBlock:_option.cancellationSignal
  139. withAccess:_access];
  140. }
  141. @end