TCUploadHelper.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // UploadImageHelper.m
  3. // TCLVBIMDemo
  4. //
  5. // Created by felixlin on 16/8/2.
  6. // Copyright © 2016年 tencent. All rights reserved.
  7. //
  8. #import "TCUploadHelper.h"
  9. #import "TCUserInfoModel.h"
  10. //#import "TCLoginModel.h"
  11. #import "TCUtil.h"
  12. #import <QCloudCore/QCloudCore.h>
  13. #import <QCloudCOSXML/QCloudCOSXML.h>
  14. #import "QCloudAuthentationHeadV5Creator.h"
  15. #define kTCHeadUploadCosKey @"head_icon"
  16. @interface TCUploadHelper () <QCloudSignatureProvider>
  17. {
  18. QCloudAuthentationHeadV5Creator* _creator;
  19. }
  20. @end
  21. @implementation TCUploadHelper
  22. static TCUploadHelper *_shareInstance = nil;
  23. + (instancetype)shareInstance
  24. {
  25. static dispatch_once_t predicate;
  26. dispatch_once(&predicate, ^{
  27. _shareInstance = [[TCUploadHelper alloc] init];
  28. });
  29. return _shareInstance;
  30. }
  31. - (instancetype)init
  32. {
  33. if (self = [super init])
  34. {
  35. [self setupCOSXMLShareService];
  36. }
  37. return self;
  38. }
  39. - (void) setupCOSXMLShareService {
  40. QCloudServiceConfiguration* configuration = [QCloudServiceConfiguration new];
  41. TCUserInfoData *infoData = [[TCUserInfoModel sharedInstance] getUserProfile];
  42. configuration.appID = infoData.appid;
  43. configuration.signatureProvider = self;
  44. QCloudCOSXMLEndPoint* endpoint = [[QCloudCOSXMLEndPoint alloc] init];
  45. endpoint.regionName = infoData.region;
  46. configuration.endpoint = endpoint;
  47. [QCloudCOSXMLService registerCOSXMLWithConfiguration:configuration withKey:kTCHeadUploadCosKey];
  48. [QCloudCOSTransferMangerService registerCOSTransferMangerWithConfiguration:configuration withKey:kTCHeadUploadCosKey];
  49. }
  50. - (void) signatureWithFields:(QCloudSignatureFields*)fileds
  51. request:(QCloudBizHTTPRequest*)request
  52. urlRequest:(NSMutableURLRequest*)urlRequst
  53. compelete:(QCloudHTTPAuthentationContinueBlock)continueBlock
  54. {
  55. if (_creator != nil) {
  56. QCloudSignature* signature = [_creator signatureForData:urlRequst];
  57. continueBlock(signature, nil);
  58. }
  59. }
  60. - (void)upload:(NSString*)userId image:(UIImage *)image completion:(void (^)(NSInteger errCode, NSString *imageSaveUrl))completion
  61. {
  62. if (!image)
  63. {
  64. if (completion)
  65. {
  66. completion(-30001, nil);
  67. }
  68. return;
  69. }
  70. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  71. NSData *imageData = UIImageJPEGRepresentation(image, 0.3);
  72. // 以时间戳为文件名(毫秒为单位,跟Android保持一致)
  73. NSString *photoName = [[NSString alloc] initWithFormat:@"%.f", [[NSDate date] timeIntervalSince1970] * 1000];
  74. NSString *pathSave = [TCUtil getFileCachePath:photoName];
  75. BOOL succ = [imageData writeToFile:pathSave atomically:YES];
  76. if (succ)
  77. {
  78. //获取sign
  79. [self getCOSSign:^(int errCode) {
  80. if (0 == errCode)
  81. {
  82. QCloudCOSXMLUploadObjectRequest* upload = [QCloudCOSXMLUploadObjectRequest new];
  83. upload.body = [NSURL fileURLWithPath:pathSave];
  84. upload.bucket = [[TCUserInfoModel sharedInstance] getUserProfile].bucket;
  85. upload.object = [NSUUID UUID].UUIDString;
  86. upload.accessControlList = @"public-read";
  87. // __weak typeof(self) weakSelf = self;
  88. [upload setFinishBlock:^(QCloudUploadObjectResult *result, NSError * error) {
  89. dispatch_async(dispatch_get_main_queue(), ^{
  90. if (completion) {
  91. if (error) {
  92. completion(error.code, error.localizedDescription);
  93. } else {
  94. NSString * resultString = [result qcloud_modelToJSONString];
  95. NSDictionary * data = [TCUtil jsonData2Dictionary:resultString];
  96. if (data != nil) {
  97. completion(0, [data objectForKey:@"Location"]);
  98. } else {
  99. completion(-1, resultString);
  100. }
  101. }
  102. }
  103. });
  104. }];
  105. [[QCloudCOSTransferMangerService costransfermangerServiceForKey:kTCHeadUploadCosKey] UploadObject:upload];
  106. }
  107. else
  108. {
  109. if (completion)
  110. {
  111. dispatch_async(dispatch_get_main_queue(), ^{
  112. completion(errCode, nil);
  113. });
  114. }
  115. }
  116. }];
  117. }
  118. else
  119. {
  120. if (completion)
  121. {
  122. dispatch_async(dispatch_get_main_queue(), ^{
  123. completion(-30002, nil);
  124. });
  125. }
  126. }
  127. });
  128. }
  129. - (void)getCOSSign:(void (^)(int errCode))handler
  130. {
  131. // [[TCLoginModel sharedInstance] getCosSign:^(int errCode, NSString *msg, NSDictionary *resultDict) {
  132. // if (errCode != 200)
  133. // {
  134. // DebugLog(@"getCOSSign failed");
  135. // handler(errCode);
  136. // }
  137. // else
  138. // {
  139. // NSString* strSignKey = nil;
  140. // NSString* strKeyTime = nil;
  141. // if (resultDict && [resultDict objectForKey:@"signKey"])
  142. // {
  143. // strSignKey = resultDict[@"signKey"];
  144. // }
  145. // if (resultDict && [resultDict objectForKey:@"keyTime"])
  146. // {
  147. // strKeyTime = resultDict[@"keyTime"];
  148. // }
  149. //
  150. //
  151. // if (_creator == nil) {
  152. // _creator = [[QCloudAuthentationHeadV5Creator alloc] initWithSignKey:[[TCUserInfoModel sharedInstance] getUserProfile].secretId signKey:strSignKey keyTime:strKeyTime];
  153. // } else {
  154. // [_creator setSignKey:[[TCUserInfoModel sharedInstance] getUserProfile].secretId signKey:strSignKey keyTime:strKeyTime];
  155. // }
  156. // handler(errCode);
  157. // }
  158. // }];
  159. // NSDictionary* dictParam = @{@"Action" : @"GetCOSSignV2"};
  160. // [TCUtil asyncSendHttpRequest:dictParam handler:^(int result, NSDictionary *resultDict) {
  161. // if (result != 0)
  162. // {
  163. // DebugLog(@"getCOSSign failed");
  164. // handler(result);
  165. // }
  166. // else
  167. // {
  168. // NSString* strSignKey = nil;
  169. // NSString* strKeyTime = nil;
  170. // if (resultDict && [resultDict objectForKey:@"signKey"])
  171. // {
  172. // strSignKey = resultDict[@"signKey"];
  173. // }
  174. // if (resultDict && [resultDict objectForKey:@"keyTime"])
  175. // {
  176. // strKeyTime = resultDict[@"keyTime"];
  177. // }
  178. //
  179. //
  180. // if (_creator == nil) {
  181. // _creator = [[QCloudAuthentationHeadV5Creator alloc] initWithSignKey:kTCCOSSecretId signKey:strSignKey keyTime:strKeyTime];
  182. // } else {
  183. // [_creator setSignKey:kTCCOSSecretId signKey:strSignKey keyTime:strKeyTime];
  184. // }
  185. // handler(result);
  186. // }
  187. // }];
  188. }
  189. @end