QCloudGetObjectRequest.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // GetObject.m
  3. // GetObject
  4. //
  5. // Created by tencent
  6. // Copyright (c) 2015年 tencent. All rights reserved.
  7. //
  8. // ██████╗ ██████╗██╗ ██████╗ ██╗ ██╗██████╗ ████████╗███████╗██████╗ ███╗ ███╗██╗███╗ ██╗ █████╗ ██╗ ██╗ █████╗
  9. // ██████╗
  10. // ██╔═══██╗██╔════╝██║ ██╔═══██╗██║ ██║██╔══██╗ ╚══██╔══╝██╔════╝██╔══██╗████╗ ████║██║████╗ ██║██╔══██╗██║ ██║ ██╔══██╗██╔══██╗
  11. // ██║ ██║██║ ██║ ██║ ██║██║ ██║██║ ██║ ██║ █████╗ ██████╔╝██╔████╔██║██║██╔██╗ ██║███████║██║ ██║ ███████║██████╔╝
  12. // ██║▄▄ ██║██║ ██║ ██║ ██║██║ ██║██║ ██║ ██║ ██╔══╝ ██╔══██╗██║╚██╔╝██║██║██║╚██╗██║██╔══██║██║ ██║ ██╔══██║██╔══██╗
  13. // ╚██████╔╝╚██████╗███████╗╚██████╔╝╚██████╔╝██████╔╝ ██║ ███████╗██║ ██║██║ ╚═╝ ██║██║██║ ╚████║██║ ██║███████╗ ███████╗██║
  14. // ██║██████╔╝
  15. // ╚══▀▀═╝ ╚═════╝╚══════╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝╚═╝ ╚═══╝╚═╝ ╚═╝╚══════╝ ╚══════╝╚═╝ ╚═╝╚═════╝
  16. //
  17. //
  18. // _ __ _ _
  19. // (_) / _| | | | |
  20. // ___ ___ _ ____ ___ ___ ___ | |_ ___ _ __ __| | _____ _____| | ___ _ __ ___ _
  21. // __ ___
  22. // / __|/ _ \ '__\ \ / / |/ __/ _ \ | _/ _ \| '__| / _` |/ _ \ \ / / _ \ |/ _ \| '_ \ / _ \
  23. // '__/ __|
  24. // \__ \ __/ | \ V /| | (_| __/ | || (_) | | | (_| | __/\ V / __/ | (_) | |_) | __/
  25. // | \__
  26. // |___/\___|_| \_/ |_|\___\___| |_| \___/|_| \__,_|\___| \_/ \___|_|\___/| .__/
  27. // \___|_| |___/
  28. // ______ ______ ______ ______ ______ ______ ______ ______ | |
  29. // |______|______|______|______|______|______|______|______| |_|
  30. //
  31. #import "QCloudGetObjectRequest.h"
  32. #import <QCloudCore/QCloudSignatureFields.h>
  33. #import <QCloudCore/QCloudCore.h>
  34. #import <QCloudCore/QCloudConfiguration_Private.h>
  35. #import "QCloudGetObjectRequest+Custom.h"
  36. NS_ASSUME_NONNULL_BEGIN
  37. @implementation QCloudGetObjectRequest
  38. - (void)dealloc {
  39. }
  40. - (instancetype)init {
  41. self = [super init];
  42. if (!self) {
  43. return nil;
  44. }
  45. self.objectKeySimplifyCheck = YES;
  46. return self;
  47. }
  48. - (void)configureReuqestSerializer:(QCloudRequestSerializer *)requestSerializer responseSerializer:(QCloudResponseSerializer *)responseSerializer {
  49. NSArray *customRequestSerilizers = @[ QCloudURLFuseSimple, QCloudURLFuseWithURLEncodeParamters ];
  50. NSArray *responseSerializers = @[
  51. QCloudAcceptRespnseCodeBlock([NSSet setWithObjects:@(200), @(201), @(202), @(203), @(204), @(205), @(206), @(207), @(208), @(226), nil], nil),
  52. QCloudResponseAppendHeadersSerializerBlock,
  53. ];
  54. [requestSerializer setSerializerBlocks:customRequestSerilizers];
  55. [responseSerializer setSerializerBlocks:responseSerializers];
  56. requestSerializer.HTTPMethod = @"get";
  57. }
  58. - (BOOL)buildRequestData:(NSError *__autoreleasing *)error {
  59. if (![super buildRequestData:error]) {
  60. return NO;
  61. }
  62. if (!self.object || ([self.object isKindOfClass:NSString.class] && ((NSString *)self.object).length == 0)) {
  63. if (error != NULL) {
  64. *error = [NSError
  65. qcloud_errorWithCode:QCloudNetworkErrorCodeParamterInvalid
  66. message:[NSString stringWithFormat:
  67. @"InvalidArgument:paramter[object] is invalid (nil), it must have some value. please check it"]];
  68. return NO;
  69. }
  70. }
  71. if (self.objectKeySimplifyCheck && [[self simplifyPath:self.object] isEqualToString:@"/"]) {
  72. if (error != NULL) {
  73. *error = [NSError
  74. qcloud_errorWithCode:QCloudNetworkErrorCodeParamterInvalid
  75. message:[NSString stringWithFormat:
  76. @"The Getobject Key is illegal"]];
  77. return NO;
  78. }
  79. }
  80. if (!self.bucket || ([self.bucket isKindOfClass:NSString.class] && ((NSString *)self.bucket).length == 0)) {
  81. if (error != NULL) {
  82. *error = [NSError
  83. qcloud_errorWithCode:QCloudNetworkErrorCodeParamterInvalid
  84. message:[NSString stringWithFormat:
  85. @"InvalidArgument:paramter[bucket] is invalid (nil), it must have some value. please check it"]];
  86. return NO;
  87. }
  88. }
  89. if (self.responseContentType) {
  90. [self.requestData setValue:self.responseContentType forHTTPHeaderField:@"response-content-type"];
  91. }
  92. if (self.responseContentLanguage) {
  93. [self.requestData setValue:self.responseContentLanguage forHTTPHeaderField:@"response-content-language"];
  94. }
  95. if (self.responseContentExpires) {
  96. [self.requestData setValue:self.responseContentExpires forHTTPHeaderField:@"response-expires"];
  97. }
  98. if (self.responseCacheControl) {
  99. [self.requestData setValue:self.responseCacheControl forHTTPHeaderField:@"response-cache-control"];
  100. }
  101. if (self.responseContentDisposition) {
  102. [self.requestData setValue:self.responseContentDisposition forHTTPHeaderField:@"response-content-disposition"];
  103. }
  104. if (self.responseContentEncoding) {
  105. [self.requestData setValue:self.responseContentEncoding forHTTPHeaderField:@"response-content-encoding"];
  106. }
  107. if (self.localCacheDownloadOffset) {
  108. self.range = [NSString stringWithFormat:@"bytes=%lld-", self.localCacheDownloadOffset];
  109. }
  110. if (self.range) {
  111. [self.requestData setValue:self.range forHTTPHeaderField:@"Range"];
  112. }
  113. if (self.ifModifiedSince) {
  114. [self.requestData setValue:self.ifModifiedSince forHTTPHeaderField:@"If-Modified-Since"];
  115. }
  116. if (self.ifUnmodifiedModifiedSince) {
  117. [self.requestData setValue:self.ifUnmodifiedModifiedSince forHTTPHeaderField:@"If-Unmodified-Since"];
  118. }
  119. if (self.ifMatch) {
  120. [self.requestData setValue:self.ifMatch forHTTPHeaderField:@"If-Match"];
  121. }
  122. if (self.ifNoneMatch) {
  123. [self.requestData setValue:self.ifNoneMatch forHTTPHeaderField:@"If-None-Match"];
  124. }
  125. if (self.versionID) {
  126. [self.requestData setParameter:self.versionID withKey:@"versionId"];
  127. }
  128. if (self.trafficLimit) {
  129. [self.requestData setValue:@(self.trafficLimit).stringValue forHTTPHeaderField:@"x-cos-traffic-limit"];
  130. }
  131. if(![self.customHeaders isKindOfClass:NSMutableDictionary.class]){
  132. self.customHeaders = self.customHeaders.mutableCopy;
  133. }
  134. [self.customHeaders setObject:@"no-cache" forKey:@"Cache-Control"];
  135. NSURL *__serverURL = [self.runOnService.configuration.endpoint serverURLWithBucket:self.bucket
  136. appID:self.runOnService.configuration.appID
  137. regionName:self.regionName];
  138. self.requestData.serverURL = __serverURL.absoluteString;
  139. [self.requestData setValue:__serverURL.host forHTTPHeaderField:@"Host"];
  140. NSMutableArray *__pathComponents = [NSMutableArray arrayWithArray:self.requestData.URIComponents];
  141. if (self.object)
  142. [__pathComponents addObject:self.object];
  143. self.requestData.URIComponents = __pathComponents;
  144. if (self.watermarkRule) {
  145. [self.requestData setQueryStringParamter:@"" withKey:self.watermarkRule];
  146. }
  147. if (![self customBuildRequestData:error])
  148. return NO;
  149. for (NSString *key in self.customHeaders.allKeys.copy) {
  150. [self.requestData setValue:self.customHeaders[key] forHTTPHeaderField:key];
  151. }
  152. return YES;
  153. }
  154. - (QCloudSignatureFields *)signatureFields {
  155. QCloudSignatureFields *fileds = [QCloudSignatureFields new];
  156. return fileds;
  157. }
  158. - (void)setFinishBlock:(void (^_Nullable)(id _Nullable result, NSError *_Nullable error))finishBlock {
  159. if (finishBlock) {
  160. QCloudWeakSelf(self);
  161. [super setFinishBlock:^(id outputObject, NSError *error) {
  162. QCloudStrongSelf(self);
  163. NSError * lError;
  164. if (QCloudFileExist(strongself.downloadingTempURL.relativePath) && !error) {
  165. if (QCloudFileExist(strongself.downloadingURL.relativePath)) {
  166. QCloudRemoveFileByPath(strongself.downloadingURL.relativePath);
  167. }
  168. QCloudMoveFile(strongself.downloadingTempURL.relativePath, strongself.downloadingURL.relativePath, &lError);
  169. }
  170. finishBlock(outputObject,error);
  171. }];
  172. }
  173. }
  174. - (NSArray<NSMutableDictionary *> *)scopesArray {
  175. NSMutableDictionary *dic = [NSMutableDictionary dictionary];
  176. NSArray *separatetmpArray = [self.requestData.serverURL componentsSeparatedByString:@"://"];
  177. NSString *str = separatetmpArray[1];
  178. NSArray *separateArray = [str componentsSeparatedByString:@"."];
  179. dic[@"bucket"] = separateArray[0];
  180. dic[@"region"] = self.runOnService.configuration.endpoint.regionName;
  181. dic[@"prefix"] = self.object;
  182. dic[@"action"] = @"name/cos:GetObject";
  183. NSMutableArray *array = [NSMutableArray array];
  184. [array addObject:dic];
  185. return [array copy];
  186. }
  187. @end
  188. NS_ASSUME_NONNULL_END