FIRStorageReference.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // Copyright 2017 Google
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #import "FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorageReference.h"
  15. #import "FirebaseStorageInternal/Sources/FIRStorageConstants_Private.h"
  16. #import "FirebaseStorageInternal/Sources/FIRStorageDeleteTask.h"
  17. #import "FirebaseStorageInternal/Sources/FIRStorageDownloadTask_Private.h"
  18. #import "FirebaseStorageInternal/Sources/FIRStorageGetDownloadURLTask.h"
  19. #import "FirebaseStorageInternal/Sources/FIRStorageGetMetadataTask.h"
  20. #import "FirebaseStorageInternal/Sources/FIRStorageListResult_Private.h"
  21. #import "FirebaseStorageInternal/Sources/FIRStorageListTask.h"
  22. #import "FirebaseStorageInternal/Sources/FIRStorageMetadata_Private.h"
  23. #import "FirebaseStorageInternal/Sources/FIRStorageReference_Private.h"
  24. #import "FirebaseStorageInternal/Sources/FIRStorageTaskSnapshot_Private.h"
  25. #import "FirebaseStorageInternal/Sources/FIRStorageTask_Private.h"
  26. #import "FirebaseStorageInternal/Sources/FIRStorageUpdateMetadataTask.h"
  27. #import "FirebaseStorageInternal/Sources/FIRStorageUploadTask_Private.h"
  28. #import "FirebaseStorageInternal/Sources/FIRStorageUtils.h"
  29. #import "FirebaseStorageInternal/Sources/FIRStorage_Private.h"
  30. #import "FirebaseStorageInternal/Sources/Public/FirebaseStorageInternal/FIRStorageTaskSnapshot.h"
  31. #import "FirebaseCore/Extension/FirebaseCoreInternal.h"
  32. #if SWIFT_PACKAGE
  33. @import GTMSessionFetcherCore;
  34. #else
  35. #import <GTMSessionFetcher/GTMSessionFetcher.h>
  36. #import <GTMSessionFetcher/GTMSessionFetcherService.h>
  37. #endif
  38. @implementation FIRIMPLStorageReference
  39. - (instancetype)initWithStorage:(FIRIMPLStorage *)storage path:(FIRStoragePath *)path {
  40. self = [super init];
  41. if (self) {
  42. _storage = storage;
  43. _path = path;
  44. }
  45. return self;
  46. }
  47. #pragma mark - NSObject overrides
  48. - (instancetype)copyWithZone:(NSZone *)zone {
  49. FIRIMPLStorageReference *copiedReference =
  50. [[[self class] allocWithZone:zone] initWithStorage:_storage path:_path];
  51. return copiedReference;
  52. }
  53. - (BOOL)isEqual:(id)object {
  54. if (self == object) {
  55. return YES;
  56. }
  57. if (![object isKindOfClass:[FIRIMPLStorageReference class]]) {
  58. return NO;
  59. }
  60. BOOL isObjectEqual = [self isEqualToFIRIMPLStorageReference:(FIRIMPLStorageReference *)object];
  61. return isObjectEqual;
  62. }
  63. - (BOOL)isEqualToFIRIMPLStorageReference:(FIRIMPLStorageReference *)reference {
  64. BOOL isEqual = [_storage isEqual:reference.storage] && [_path isEqual:reference.path];
  65. return isEqual;
  66. }
  67. - (NSUInteger)hash {
  68. NSUInteger hash = [_storage hash] ^ [_path hash];
  69. return hash;
  70. }
  71. - (NSString *)description {
  72. return [self stringValue];
  73. }
  74. - (NSString *)stringValue {
  75. NSString *value = [NSString stringWithFormat:@"gs://%@/%@", _path.bucket, _path.object ?: @""];
  76. return value;
  77. }
  78. #pragma mark - Property Getters
  79. - (NSString *)bucket {
  80. NSString *bucket = _path.bucket;
  81. return bucket;
  82. }
  83. - (NSString *)fullPath {
  84. NSString *path = _path.object;
  85. if (!path) {
  86. path = @"";
  87. }
  88. return path;
  89. }
  90. - (NSString *)name {
  91. NSString *name = [_path.object lastPathComponent];
  92. if (!name) {
  93. name = @"";
  94. }
  95. return name;
  96. }
  97. #pragma mark - Path Operations
  98. - (FIRIMPLStorageReference *)root {
  99. FIRStoragePath *rootPath = [_path root];
  100. FIRIMPLStorageReference *rootReference =
  101. [[FIRIMPLStorageReference alloc] initWithStorage:_storage path:rootPath];
  102. return rootReference;
  103. }
  104. - (nullable FIRIMPLStorageReference *)parent {
  105. FIRStoragePath *parentPath = [_path parent];
  106. if (!parentPath) {
  107. return nil;
  108. }
  109. FIRIMPLStorageReference *parentReference =
  110. [[FIRIMPLStorageReference alloc] initWithStorage:_storage path:parentPath];
  111. return parentReference;
  112. }
  113. - (FIRIMPLStorageReference *)child:(NSString *)path {
  114. FIRStoragePath *childPath = [_path child:path];
  115. FIRIMPLStorageReference *childReference =
  116. [[FIRIMPLStorageReference alloc] initWithStorage:_storage path:childPath];
  117. return childReference;
  118. }
  119. #pragma mark - Uploads
  120. - (FIRIMPLStorageUploadTask *)putData:(NSData *)uploadData {
  121. return [self putData:uploadData metadata:nil completion:nil];
  122. }
  123. - (FIRIMPLStorageUploadTask *)putData:(NSData *)uploadData
  124. metadata:(nullable FIRIMPLStorageMetadata *)metadata {
  125. return [self putData:uploadData metadata:metadata completion:nil];
  126. }
  127. - (FIRIMPLStorageUploadTask *)putData:(NSData *)uploadData
  128. metadata:(nullable FIRIMPLStorageMetadata *)metadata
  129. completion:(nullable FIRStorageVoidMetadataError)completion {
  130. if (!metadata) {
  131. metadata = [[FIRIMPLStorageMetadata alloc] init];
  132. }
  133. metadata.path = _path.object;
  134. metadata.name = [_path.object lastPathComponent];
  135. FIRIMPLStorageUploadTask *task =
  136. [[FIRIMPLStorageUploadTask alloc] initWithReference:self
  137. fetcherService:_storage.fetcherServiceForApp
  138. dispatchQueue:_storage.dispatchQueue
  139. data:uploadData
  140. metadata:metadata];
  141. if (completion) {
  142. __block BOOL completed = NO;
  143. dispatch_queue_t callbackQueue = _storage.fetcherServiceForApp.callbackQueue;
  144. if (!callbackQueue) {
  145. callbackQueue = dispatch_get_main_queue();
  146. }
  147. [task observeStatus:FIRIMPLStorageTaskStatusSuccess
  148. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  149. dispatch_async(callbackQueue, ^{
  150. if (!completed) {
  151. completed = YES;
  152. completion(snapshot.metadata, nil);
  153. }
  154. });
  155. }];
  156. [task observeStatus:FIRIMPLStorageTaskStatusFailure
  157. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  158. dispatch_async(callbackQueue, ^{
  159. if (!completed) {
  160. completed = YES;
  161. completion(nil, snapshot.error);
  162. }
  163. });
  164. }];
  165. }
  166. [task enqueue];
  167. return task;
  168. }
  169. - (FIRIMPLStorageUploadTask *)putFile:(NSURL *)fileURL {
  170. return [self putFile:fileURL metadata:nil completion:nil];
  171. }
  172. - (FIRIMPLStorageUploadTask *)putFile:(NSURL *)fileURL
  173. metadata:(nullable FIRIMPLStorageMetadata *)metadata {
  174. return [self putFile:fileURL metadata:metadata completion:nil];
  175. }
  176. - (FIRIMPLStorageUploadTask *)putFile:(NSURL *)fileURL
  177. metadata:(nullable FIRIMPLStorageMetadata *)metadata
  178. completion:(nullable FIRStorageVoidMetadataError)completion {
  179. if (!metadata) {
  180. metadata = [[FIRIMPLStorageMetadata alloc] init];
  181. }
  182. metadata.path = _path.object;
  183. metadata.name = [_path.object lastPathComponent];
  184. FIRIMPLStorageUploadTask *task =
  185. [[FIRIMPLStorageUploadTask alloc] initWithReference:self
  186. fetcherService:_storage.fetcherServiceForApp
  187. dispatchQueue:_storage.dispatchQueue
  188. file:fileURL
  189. metadata:metadata];
  190. if (completion) {
  191. __block BOOL completed = NO;
  192. dispatch_queue_t callbackQueue = _storage.fetcherServiceForApp.callbackQueue;
  193. if (!callbackQueue) {
  194. callbackQueue = dispatch_get_main_queue();
  195. }
  196. [task observeStatus:FIRIMPLStorageTaskStatusSuccess
  197. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  198. dispatch_async(callbackQueue, ^{
  199. if (!completed) {
  200. completed = YES;
  201. completion(snapshot.metadata, nil);
  202. }
  203. });
  204. }];
  205. [task observeStatus:FIRIMPLStorageTaskStatusFailure
  206. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  207. dispatch_async(callbackQueue, ^{
  208. if (!completed) {
  209. completed = YES;
  210. completion(nil, snapshot.error);
  211. }
  212. });
  213. }];
  214. }
  215. [task enqueue];
  216. return task;
  217. }
  218. #pragma mark - Downloads
  219. - (FIRIMPLStorageDownloadTask *)dataWithMaxSize:(int64_t)size
  220. completion:(FIRStorageVoidDataError)completion {
  221. __block BOOL completed = NO;
  222. FIRIMPLStorageDownloadTask *task =
  223. [[FIRIMPLStorageDownloadTask alloc] initWithReference:self
  224. fetcherService:_storage.fetcherServiceForApp
  225. dispatchQueue:_storage.dispatchQueue
  226. file:nil];
  227. dispatch_queue_t callbackQueue = _storage.fetcherServiceForApp.callbackQueue;
  228. if (!callbackQueue) {
  229. callbackQueue = dispatch_get_main_queue();
  230. }
  231. [task observeStatus:FIRIMPLStorageTaskStatusSuccess
  232. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  233. FIRIMPLStorageDownloadTask *task = snapshot.task;
  234. dispatch_async(callbackQueue, ^{
  235. if (!completed) {
  236. completed = YES;
  237. completion(task.downloadData, nil);
  238. }
  239. });
  240. }];
  241. [task observeStatus:FIRIMPLStorageTaskStatusFailure
  242. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  243. dispatch_async(callbackQueue, ^{
  244. if (!completed) {
  245. completed = YES;
  246. completion(nil, snapshot.error);
  247. }
  248. });
  249. }];
  250. [task
  251. observeStatus:FIRIMPLStorageTaskStatusProgress
  252. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  253. FIRIMPLStorageDownloadTask *task = snapshot.task;
  254. if (task.progress.totalUnitCount > size || task.progress.completedUnitCount > size) {
  255. NSDictionary *infoDictionary =
  256. @{@"totalSize" : @(task.progress.totalUnitCount),
  257. @"maxAllowedSize" : @(size)};
  258. NSError *error =
  259. [FIRStorageErrors errorWithCode:FIRIMPLStorageErrorCodeDownloadSizeExceeded
  260. infoDictionary:infoDictionary];
  261. [task cancelWithError:error];
  262. }
  263. }];
  264. [task enqueue];
  265. return task;
  266. }
  267. - (FIRIMPLStorageDownloadTask *)writeToFile:(NSURL *)fileURL {
  268. return [self writeToFile:fileURL completion:nil];
  269. }
  270. - (FIRIMPLStorageDownloadTask *)writeToFile:(NSURL *)fileURL
  271. completion:(FIRStorageVoidURLError)completion {
  272. FIRIMPLStorageDownloadTask *task =
  273. [[FIRIMPLStorageDownloadTask alloc] initWithReference:self
  274. fetcherService:_storage.fetcherServiceForApp
  275. dispatchQueue:_storage.dispatchQueue
  276. file:fileURL];
  277. if (completion) {
  278. __block BOOL completed = NO;
  279. dispatch_queue_t callbackQueue = _storage.fetcherServiceForApp.callbackQueue;
  280. if (!callbackQueue) {
  281. callbackQueue = dispatch_get_main_queue();
  282. }
  283. [task observeStatus:FIRIMPLStorageTaskStatusSuccess
  284. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  285. dispatch_async(callbackQueue, ^{
  286. if (!completed) {
  287. completed = YES;
  288. completion(fileURL, nil);
  289. }
  290. });
  291. }];
  292. [task observeStatus:FIRIMPLStorageTaskStatusFailure
  293. handler:^(FIRIMPLStorageTaskSnapshot *_Nonnull snapshot) {
  294. dispatch_async(callbackQueue, ^{
  295. if (!completed) {
  296. completed = YES;
  297. completion(nil, snapshot.error);
  298. }
  299. });
  300. }];
  301. }
  302. [task enqueue];
  303. return task;
  304. }
  305. - (void)downloadURLWithCompletion:(FIRStorageVoidURLError)completion {
  306. FIRStorageGetDownloadURLTask *task =
  307. [[FIRStorageGetDownloadURLTask alloc] initWithReference:self
  308. fetcherService:_storage.fetcherServiceForApp
  309. dispatchQueue:_storage.dispatchQueue
  310. completion:completion];
  311. [task enqueue];
  312. }
  313. #pragma mark - List
  314. - (void)listWithMaxResults:(int64_t)maxResults completion:(FIRStorageVoidListError)completion {
  315. if (maxResults <= 0 || maxResults > 1000) {
  316. completion(
  317. nil, [FIRStorageUtils storageErrorWithDescription:
  318. @"Argument 'maxResults' must be between 1 and 1000 inclusive."
  319. code:FIRIMPLStorageErrorCodeInvalidArgument]);
  320. } else {
  321. FIRStorageListTask *task =
  322. [[FIRStorageListTask alloc] initWithReference:self
  323. fetcherService:_storage.fetcherServiceForApp
  324. dispatchQueue:_storage.dispatchQueue
  325. pageSize:@(maxResults)
  326. previousPageToken:nil
  327. completion:completion];
  328. [task enqueue];
  329. }
  330. }
  331. - (void)listWithMaxResults:(int64_t)maxResults
  332. pageToken:(NSString *)pageToken
  333. completion:(FIRStorageVoidListError)completion {
  334. if (maxResults <= 0 || maxResults > 1000) {
  335. completion(
  336. nil, [FIRStorageUtils storageErrorWithDescription:
  337. @"Argument 'maxResults' must be between 1 and 1000 inclusive."
  338. code:FIRIMPLStorageErrorCodeInvalidArgument]);
  339. } else {
  340. FIRStorageListTask *task =
  341. [[FIRStorageListTask alloc] initWithReference:self
  342. fetcherService:_storage.fetcherServiceForApp
  343. dispatchQueue:_storage.dispatchQueue
  344. pageSize:@(maxResults)
  345. previousPageToken:pageToken
  346. completion:completion];
  347. [task enqueue];
  348. }
  349. }
  350. - (void)listAllWithCompletion:(FIRStorageVoidListError)completion {
  351. NSMutableArray *prefixes = [NSMutableArray new];
  352. NSMutableArray *items = [NSMutableArray new];
  353. __weak FIRIMPLStorageReference *weakSelf = self;
  354. __block FIRStorageVoidListError paginatedCompletion = ^(FIRIMPLStorageListResult *listResult,
  355. NSError *error) {
  356. if (error) {
  357. completion(nil, error);
  358. return;
  359. }
  360. FIRIMPLStorageReference *strongSelf = weakSelf;
  361. if (!strongSelf) {
  362. return;
  363. }
  364. [prefixes addObjectsFromArray:listResult.prefixes];
  365. [items addObjectsFromArray:listResult.items];
  366. if (listResult.pageToken) {
  367. FIRStorageListTask *nextPage =
  368. [[FIRStorageListTask alloc] initWithReference:self
  369. fetcherService:strongSelf->_storage.fetcherServiceForApp
  370. dispatchQueue:strongSelf->_storage.dispatchQueue
  371. pageSize:nil
  372. previousPageToken:listResult.pageToken
  373. completion:paginatedCompletion];
  374. [nextPage enqueue];
  375. } else {
  376. FIRIMPLStorageListResult *result = [[FIRIMPLStorageListResult alloc] initWithPrefixes:prefixes
  377. items:items
  378. pageToken:nil];
  379. // Break the retain cycle we set up indirectly by passing the callback to `nextPage`.
  380. paginatedCompletion = nil;
  381. completion(result, nil);
  382. }
  383. };
  384. FIRStorageListTask *task =
  385. [[FIRStorageListTask alloc] initWithReference:self
  386. fetcherService:_storage.fetcherServiceForApp
  387. dispatchQueue:_storage.dispatchQueue
  388. pageSize:nil
  389. previousPageToken:nil
  390. completion:paginatedCompletion];
  391. [task enqueue];
  392. }
  393. #pragma mark - Metadata Operations
  394. - (void)metadataWithCompletion:(FIRStorageVoidMetadataError)completion {
  395. FIRStorageGetMetadataTask *task =
  396. [[FIRStorageGetMetadataTask alloc] initWithReference:self
  397. fetcherService:_storage.fetcherServiceForApp
  398. dispatchQueue:_storage.dispatchQueue
  399. completion:completion];
  400. [task enqueue];
  401. }
  402. - (void)updateMetadata:(FIRIMPLStorageMetadata *)metadata
  403. completion:(nullable FIRStorageVoidMetadataError)completion {
  404. FIRStorageUpdateMetadataTask *task =
  405. [[FIRStorageUpdateMetadataTask alloc] initWithReference:self
  406. fetcherService:_storage.fetcherServiceForApp
  407. dispatchQueue:_storage.dispatchQueue
  408. metadata:metadata
  409. completion:completion];
  410. [task enqueue];
  411. }
  412. #pragma mark - Delete
  413. - (void)deleteWithCompletion:(nullable FIRStorageVoidError)completion {
  414. FIRStorageDeleteTask *task =
  415. [[FIRStorageDeleteTask alloc] initWithReference:self
  416. fetcherService:_storage.fetcherServiceForApp
  417. dispatchQueue:_storage.dispatchQueue
  418. completion:completion];
  419. [task enqueue];
  420. }
  421. @end