This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSBBUploadManager.m
1209 lines (1063 loc) · 58 KB
/
SBBUploadManager.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// SBBUploadManager.m
// BridgeSDK
//
// Copyright (c) 2014-2018 Sage Bionetworks
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of Sage Bionetworks nor the names of BridgeSDk's
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL SAGE BIONETWORKS BE LIABLE FOR ANY
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "SBBUploadManagerInternal.h"
#import "NSData+SBBAdditions.h"
#import "SBBComponentManager.h"
#import "SBBAuthManager.h"
#import "SBBNetworkManagerInternal.h"
#import "SBBObjectManager.h"
#import "SBBUploadSession.h"
#import "SBBUploadRequest.h"
#import "NSError+SBBAdditions.h"
#import "SBBErrors.h"
#import "BridgeSDK+Internal.h"
#import "NSDate+SBBAdditions.h"
#import "NSString+SBBAdditions.h"
#import <CoreServices/CoreServices.h>
#define UPLOAD_API V3_API_PREFIX @"/uploads"
#define UPLOAD_STATUS_API V3_API_PREFIX @"/uploadstatuses"
static NSString * const uuidPrefixRegexPattern = @"^" UUID_REGEX_PATTERN @"_";
NSString * const kSBBUploadAPI = UPLOAD_API;
NSString * const kSBBUploadCompleteAPIFormat = UPLOAD_API @"/%@/complete";
NSString * const kSBBUploadStatusAPIFormat = UPLOAD_STATUS_API @"/%@";
NSString * const keysUpdatedKey = @"SBBUploadManagerKeysUpdatedKey2";
NSString * const kUploadFilesKey = @"SBBUploadFilesKey";
NSString * const kUploadRequestsKey = @"SBBUploadRequestsKey";
NSString * const kUploadSessionsKey = @"SBBUploadSessionsKey";
NSString * const kSBBUploadRetryAfterDelayKey = @"SBBUploadRetryAfterDelayKey";
NSTimeInterval kSBBDelayForRetries = 5. * 60.; // at least 5 minutes, actually whenever we get to it after that
#pragma mark - SBBUploadCompletionWrapper
@interface SBBUploadCompletionWrapper : NSObject
@property (nonatomic, copy) SBBUploadManagerCompletionBlock completion;
- (instancetype)initWithBlock:(SBBUploadManagerCompletionBlock)block;
@end
@implementation SBBUploadCompletionWrapper
- (instancetype)initWithBlock:(SBBUploadManagerCompletionBlock)block
{
if (self = [super init]) {
self.completion = block;
}
return self;
}
@end
#pragma mark - SBBUploadManager
@implementation SBBUploadManager
@synthesize uploadDelegate = _uploadDelegate;
+ (instancetype)defaultComponent
{
static SBBUploadManager *shared;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
shared = [self instanceWithRegisteredDependencies];
shared.networkManager.backgroundTransferDelegate = shared;
[shared migrateKeysIfNeeded];
// check if any uploads that got 503 status codes from S3 or Bridge errors are due for a retry
// whenever the app comes to the foreground
[[NSNotificationCenter defaultCenter] addObserverForName:UIApplicationDidBecomeActiveNotification object:nil queue:nil usingBlock:^(NSNotification * _Nonnull note) {
[shared checkAndRetryOrphanedUploads];
[shared retryUploadsAfterDelay];
}];
// also check right away
[shared retryUploadsAfterDelay];
});
return shared;
}
- (instancetype)init
{
if (self = [super init]) {
self.uploadCompletionHandlers = [NSMutableDictionary dictionary];
}
return self;
}
+ (SBBObjectManager *)cleanObjectManager
{
static SBBObjectManager *om = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
om = [SBBObjectManager objectManager];
});
return om;
}
- (SBBObjectManager *)cleanObjectManager
{
return self.class.cleanObjectManager;
}
- (void)migrateKeysIfNeeded
{
// In olden times we used full file paths as keys. Then we discovered the UUID of the app sandbox (which is part
// of the full file paths) can change between runs. Possibly only when a new version is installed, but still. Yikes.
// So we need to check if that's been updated and fix if not--on the background delegate queue, to serialize access.
[((SBBNetworkManager *)self.networkManager) performBlockOnBackgroundDelegateQueue:^{
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
BOOL keysUpgraded = [defaults boolForKey:keysUpdatedKey];
if (!keysUpgraded) {
// upgrade the files for temp files list keys
NSDictionary *filesForTempFiles = [defaults dictionaryForKey:kUploadFilesKey];
NSMutableDictionary *upgradedFilesForTempFiles = [NSMutableDictionary dictionary];
for (NSString *oldKey in filesForTempFiles.allKeys) {
NSString *oldKeyNormalized = [[NSURL fileURLWithPath:oldKey] URLByResolvingSymlinksInPath].path;
NSString *newKey = [oldKeyNormalized sandboxRelativePath];
// normalize the file it points at as well, since we're now storing the sandbox-relative part of that, too
NSString *oldFile = filesForTempFiles[oldKey];
oldFile = [[NSURL fileURLWithPath:oldFile] URLByResolvingSymlinksInPath].path;
upgradedFilesForTempFiles[newKey] = [oldFile sandboxRelativePath];
}
[defaults setObject:upgradedFilesForTempFiles forKey:kUploadFilesKey];
// upgrade the upload request keys
NSDictionary *uploadRequests = [defaults dictionaryForKey:kUploadRequestsKey];
NSMutableDictionary *upgradedUploadRequests = [NSMutableDictionary dictionary];
for (NSString *oldKey in uploadRequests.allKeys) {
NSString *oldKeyNormalized = [[NSURL fileURLWithPath:oldKey] URLByResolvingSymlinksInPath].path;
NSString *newKey = [oldKeyNormalized sandboxRelativePath];
upgradedUploadRequests[newKey] = uploadRequests[oldKey];
}
[defaults setObject:upgradedUploadRequests forKey:kUploadRequestsKey];
// upgrade the upload session keys
NSDictionary *uploadSessions = [defaults dictionaryForKey:kUploadSessionsKey];
NSMutableDictionary *upgradedUploadSessions = [NSMutableDictionary dictionary];
for (NSString *oldKey in uploadSessions.allKeys) {
NSString *oldKeyNormalized = [[NSURL fileURLWithPath:oldKey] URLByResolvingSymlinksInPath].path;
NSString *newKey = [oldKeyNormalized sandboxRelativePath];
upgradedUploadSessions[newKey] = uploadSessions[oldKey];
}
[defaults setObject:upgradedUploadSessions forKey:kUploadSessionsKey];
// upgrade the retry-after-delay keys
NSDictionary *retryUploads = [defaults dictionaryForKey:kSBBUploadRetryAfterDelayKey];
NSMutableDictionary *upgradedRetryUploads = [NSMutableDictionary dictionary];
for (NSString *oldKey in retryUploads.allKeys) {
NSString *oldKeyNormalized = [[NSURL fileURLWithPath:oldKey] URLByResolvingSymlinksInPath].path;
NSString *newKey = [oldKeyNormalized sandboxRelativePath];
upgradedRetryUploads[newKey] = retryUploads[oldKey];
}
[defaults setObject:upgradedRetryUploads forKey:kSBBUploadRetryAfterDelayKey];
// mark the keys as updated and synchronize defaults
[defaults setBool:YES forKey:keysUpdatedKey];
[defaults synchronize];
}
}];
}
- (NSURL *)tempUploadDirURL
{
static NSURL *uploadDirURL = nil;
if (!uploadDirURL) {
NSURL *baseDirURL;
NSString *appGroupIdentifier = SBBBridgeInfo.shared.appGroupIdentifier;
if (appGroupIdentifier.length > 0) {
baseDirURL = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:appGroupIdentifier];
} else {
NSURL *appSupportDir = [[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] firstObject];
NSString *bundleName = [[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleIdentifier"];
// for unit tests, the main bundle infoDictionary is empty, so...
baseDirURL = [[appSupportDir URLByAppendingPathComponent:bundleName ?: @"__test__"] URLByAppendingPathComponent:@"SBBUploadManager"];
}
uploadDirURL = [baseDirURL URLByAppendingPathComponent:@"SBBUploadManager"];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtURL:uploadDirURL withIntermediateDirectories:YES attributes:nil error:&error]) {
NSLog(@"Error attempting to create uploadDir at path %@:\n%@", uploadDirURL.absoluteURL, error);
uploadDirURL = nil;
}
}
return uploadDirURL;
}
- (NSURL *)tempFileForFileURL:(NSURL *)fileURL
{
// normalize the file url--i.e. /private/var-->/var (see docs for URLByResolvingSymlinksInPath, which removes /private as a special case
// even though /var is actually a symlink to /private/var in this case)
fileURL = [fileURL URLByResolvingSymlinksInPath];
// don't stack UUIDs in front of the base filename--replace any number of them with one new one; one UUID is enough to guarantee uniqueness
NSString *baseFileName = [fileURL lastPathComponent];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:uuidPrefixRegexPattern options:0 error:nil];
NSTextCheckingResult *match;
while ((match = [regex firstMatchInString:baseFileName options:0 range:NSMakeRange(0, baseFileName.length)])) {
baseFileName = [baseFileName substringFromIndex:match.range.length];
}
NSString *fileName = [NSString stringWithFormat:@"%@_%@", [[NSUUID UUID] UUIDString], baseFileName];
NSURL *tempFileURL = [[self tempUploadDirURL] URLByAppendingPathComponent:fileName];
__block NSError *error;
NSFileManager *fileMan = [NSFileManager defaultManager];
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSError *fileCoordinatorError = nil;
[fileCoordinator coordinateReadingItemAtURL:fileURL options:0 writingItemAtURL:tempFileURL options:0 error:&fileCoordinatorError byAccessor:^(NSURL * _Nonnull newReadingURL, NSURL * _Nonnull newWritingURL) {
[fileMan copyItemAtURL:newReadingURL toURL:newWritingURL error:&error];
}];
NSString *errorMessage;
if (fileCoordinatorError) {
errorMessage = [NSString stringWithFormat:@"File coordinator error copying file %@ to temp file %@:\n%@", [[fileURL path] sandboxRelativePath], [[tempFileURL path] sandboxRelativePath], fileCoordinatorError];
} else if (error) {
errorMessage = [NSString stringWithFormat:@"Error copying file %@ to temp file %@:\n%@", [[fileURL path] sandboxRelativePath], [[tempFileURL path] sandboxRelativePath], error];
}
if (errorMessage.length > 0) {
NSLog(@"%@", errorMessage);
tempFileURL = nil;
}
if (tempFileURL) {
// give the copy a fresh modification date for retry accounting purposes
[fileCoordinator coordinateWritingItemAtURL:tempFileURL options:0 error:nil byAccessor:^(NSURL * _Nonnull newURL) {
[fileMan setAttributes:@{NSFileModificationDate:[NSDate date]} ofItemAtPath:tempFileURL.path error:nil];
}];
// keep track of what file it's a copy of
[((SBBNetworkManager *)self.networkManager) performBlockOnBackgroundDelegateQueue:^{
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSMutableDictionary *filesForTempFiles = [[defaults dictionaryForKey:kUploadFilesKey] mutableCopy];
if (!filesForTempFiles) {
filesForTempFiles = [NSMutableDictionary dictionary];
}
[filesForTempFiles setObject:[[fileURL path] sandboxRelativePath] forKey:[[tempFileURL path] sandboxRelativePath]];
[defaults setObject:filesForTempFiles forKey:kUploadFilesKey];
[defaults synchronize];
}];
}
return tempFileURL;
}
- (NSString *)fileForTempFile:(NSString *)tempFilePath
{
NSString *file = nil;
if (tempFilePath.length) {
file = [[BridgeSDK sharedUserDefaults] dictionaryForKey:kUploadFilesKey][[tempFilePath sandboxRelativePath]];
}
return file;
}
- (void)removeFileForTempFile:(NSString *)tempFilePath
{
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSMutableDictionary *files = [[defaults dictionaryForKey:kUploadFilesKey] mutableCopy];
[files removeObjectForKey:[tempFilePath sandboxRelativePath]];
[defaults setObject:files forKey:kUploadFilesKey];
[defaults synchronize];
}
- (void)removeTempFilesWithOriginalFile:(NSString *)filePath exceptFile:(NSString *)saveFilePath
{
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSMutableDictionary *files = [[defaults dictionaryForKey:kUploadFilesKey] mutableCopy];
NSArray *allKeys = files.allKeys;
NSString *sandboxFilePath = [filePath sandboxRelativePath];
NSString *sandboxSavePath = [saveFilePath sandboxRelativePath];
for (NSString *tempFilePath in allKeys) {
NSString *thisFile = files[tempFilePath];
if ([thisFile isEqualToString:sandboxFilePath]) {
[files removeObjectForKey:tempFilePath];
if (![tempFilePath isEqualToString:sandboxSavePath]) {
// also delete any other temp files which were copies of the same original file
[self deleteTempFile:tempFilePath];
}
}
}
[defaults setObject:files forKey:kUploadFilesKey];
[defaults synchronize];
}
// use the fully qualified path
- (void)deleteTempFile:(NSString *)tempFilePath
{
NSURL *tempFile = [NSURL fileURLWithPath:[tempFilePath fullyQualifiedPath]];
if (!tempFile) {
NSLog(@"Could not create NSURL for temp file %@", tempFilePath);
}
__block NSError *error;
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSError *fileCoordinatorError = nil;
[fileCoordinator coordinateWritingItemAtURL:tempFile options:NSFileCoordinatorWritingForDeleting error:&fileCoordinatorError byAccessor:^(NSURL * _Nonnull newURL) {
if (![[NSFileManager defaultManager] removeItemAtURL:tempFile error:&error]) {
NSLog(@"Failed to remove temp file %@, error:\n%@", tempFile.path, error);
}
}];
if (fileCoordinatorError != nil) {
NSLog(@"Failed to coordinate removal of temp file %@, error:\n%@", tempFile.path, error);
}
}
// use the fully qualified path
- (void)cleanUpTempFile:(NSString *)tempFilePath
{
if (tempFilePath.length) {
// remove it from the upload files map
[self removeFileForTempFile:tempFilePath];
// delete the temp file
[self deleteTempFile:tempFilePath];
}
}
- (SBBUploadManagerCompletionBlock)completionBlockForFile:(NSString *)file
{
SBBUploadCompletionWrapper *wrapper = [_uploadCompletionHandlers objectForKey:file];
return wrapper.completion;
}
- (void)setCompletionBlock:(SBBUploadManagerCompletionBlock)completion forFile:(NSString *)file
{
if (!completion) {
[self removeCompletionBlockForFile:file];
return;
}
SBBUploadCompletionWrapper *wrapper = [[SBBUploadCompletionWrapper alloc] initWithBlock:completion];
[_uploadCompletionHandlers setObject:wrapper forKey:file];
}
- (void)removeCompletionBlockForFile:(NSString *)file
{
[_uploadCompletionHandlers removeObjectForKey:file];
}
- (void)setUploadRequestJSON:(id)json forFile:(NSString *)fileURLString
{
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSMutableDictionary *uploadRequests = [[defaults dictionaryForKey:kUploadRequestsKey] mutableCopy];
if (!uploadRequests) {
uploadRequests = [NSMutableDictionary dictionary];
}
if (json) {
[uploadRequests setObject:json forKey:[fileURLString sandboxRelativePath]];
} else {
[uploadRequests removeObjectForKey:[fileURLString sandboxRelativePath]];
}
[defaults setObject:uploadRequests forKey:kUploadRequestsKey];
[defaults synchronize];
}
- (NSDictionary *)uploadRequestJSONForFile:(NSString *)fileURLString
{
return [[BridgeSDK sharedUserDefaults] dictionaryForKey:kUploadRequestsKey][[fileURLString sandboxRelativePath]];
}
- (SBBUploadRequest *)uploadRequestForFile:(NSString *)fileURLString
{
SBBUploadRequest *uploadRequest = nil;
id json = [self uploadRequestJSONForFile:fileURLString];
if (json) {
uploadRequest = [self.cleanObjectManager objectFromBridgeJSON:json];
}
return uploadRequest;
}
- (void)setUploadSessionJSON:(id)json forFile:(NSString *)fileURLString
{
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSMutableDictionary *uploadSessions = [[defaults dictionaryForKey:kUploadSessionsKey] mutableCopy];
if (!uploadSessions) {
uploadSessions = [NSMutableDictionary dictionary];
}
if (json) {
[uploadSessions setObject:json forKey:[fileURLString sandboxRelativePath]];
} else {
[uploadSessions removeObjectForKey:[fileURLString sandboxRelativePath]];
}
[defaults setObject:uploadSessions forKey:kUploadSessionsKey];
[defaults synchronize];
}
- (SBBUploadSession *)uploadSessionForFile:(NSString *)fileURLString
{
SBBUploadSession *uploadSession = nil;
id json = [[BridgeSDK sharedUserDefaults] dictionaryForKey:kUploadSessionsKey][[fileURLString sandboxRelativePath]];
if (json) {
uploadSession = [self.cleanObjectManager objectFromBridgeJSON:json];
}
return uploadSession;
}
- (NSDate *)retryTimeForFile:(NSString *)fileURLString
{
NSDate *retryTime = nil;
NSString *jsonDate = [[BridgeSDK sharedUserDefaults] dictionaryForKey:kSBBUploadRetryAfterDelayKey][[fileURLString sandboxRelativePath]];
if (jsonDate) {
retryTime = [NSDate dateWithISO8601String:jsonDate];
}
return retryTime;
}
- (void)setRetryAfterDelayForFile:(NSString *)fileURLString
{
NSTimeInterval delay = kSBBDelayForRetries;
#if DEBUG
NSLog(@"Will retry upload of file %@ after %lf seconds.", fileURLString, delay);
#endif
NSDate *retryTime = [NSDate dateWithTimeIntervalSinceNow:delay];
[self setRetryTime:retryTime forFile:fileURLString];
}
- (void)setRetryTime:(NSDate *)retryTime forFile:(NSString *)fileURLString {
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSMutableDictionary *retryUploads = [[defaults dictionaryForKey:kSBBUploadRetryAfterDelayKey] mutableCopy];
if (!retryUploads) {
retryUploads = [NSMutableDictionary dictionary];
}
if (retryTime) {
retryUploads[[fileURLString sandboxRelativePath]] = [retryTime ISO8601String];
} else {
[retryUploads removeObjectForKey:[fileURLString sandboxRelativePath]];
}
[defaults setObject:retryUploads forKey:kSBBUploadRetryAfterDelayKey];
[defaults synchronize];
}
- (void)retryUploadsAfterDelay
{
[((SBBNetworkManager *)self.networkManager) performBlockOnBackgroundDelegateQueue:^{
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSMutableDictionary *retryUploads = [[defaults dictionaryForKey:kSBBUploadRetryAfterDelayKey] mutableCopy];
for (NSString *fileURLString in retryUploads.allKeys) {
@autoreleasepool {
NSString *filePath = [fileURLString fullyQualifiedPath];
NSDate *retryTime = [self retryTimeForFile:filePath];
if ([retryTime timeIntervalSinceNow] <= 0.) {
[self setRetryTime:nil forFile:filePath];
NSDictionary *uploadRequestJSON = [self uploadRequestJSONForFile:filePath];
SBBUploadManagerCompletionBlock completion = [self completionBlockForFile:filePath];
if (completion && uploadRequestJSON.allKeys.count) {
[self kickOffUploadForFile:filePath uploadRequestJSON:uploadRequestJSON];
} else {
[self retryOrphanedUploadFromScratch:filePath];
}
}
}
}
}];
}
// When uploading a file to Bridge, first we use background download to get an SBBUploadSession object from Bridge.
// Then we use the pre-signed URL in that SBBUploadSession to upload our file to S3.
// When that finishes, we tell Bridge that the requested upload is completed.
// To avoid having multiple layers of nested completion handlers in one method, they're broken out by step:
// --- Here's the completion handler for the step where we tell Bridge the upload completed:
- (void)toldBridgeFileUploadedWithTask:(NSURLSessionUploadTask *)uploadTask forBridgeSession:(SBBUploadSession *)uploadSession error:(NSError *)error
{
#if DEBUG
if (error) {
NSLog(@"Error calling upload complete for upload ID %@:\n%@", uploadSession.id, error);
} else {
NSString* uploadStatusUrlString = nil;
if ([self.networkManager isKindOfClass:[SBBNetworkManager class]]) {
NSString* relativeUrl = [NSString stringWithFormat:kSBBUploadStatusAPIFormat, uploadSession.id];
NSURL* url = [(SBBNetworkManager*) self.networkManager URLForRelativeorAbsoluteURLString:relativeUrl];
uploadStatusUrlString = [url absoluteString];
if ([_uploadDelegate respondsToSelector:@selector(uploadManager:uploadOfFile:completedWithVerificationURL:)]) {
[_uploadDelegate uploadManager:self uploadOfFile:uploadTask.taskDescription completedWithVerificationURL:url];
}
}
NSMutableDictionary *headers = [NSMutableDictionary dictionary];
[self.authManager addAuthHeaderToHeaders:headers];
NSMutableArray *headersStringArray = [@[] mutableCopy];
[headers enumerateKeysAndObjectsUsingBlock:^(id _Nonnull key, id _Nonnull obj, BOOL * _Nonnull stop) {
NSString *header = [NSString stringWithFormat:@"%@:%@", key, obj];
[headersStringArray addObject:header];
}];
NSString *headersString = [headersStringArray componentsJoinedByString:@","];
NSLog(@"Successfully called upload complete for upload ID %@, check status with curl -H \"%@\" %@", uploadSession.id, headersString, uploadStatusUrlString);
}
#endif
[self completeUploadOfFile:uploadTask.taskDescription withError:error];
}
- (void)handleUploadHTTPStatusCode:(NSInteger)httpStatusCode forUploadFilePath:(NSString *)filePath
{
switch (httpStatusCode) {
case 403:
case 409:
case 500:
case 503: {
// 403 for our purposes means the pre-signed url timed out before starting the actual upload to S3.
// 409 in our case it could only mean a temporary conflict (resource locked by another process, etc.) that should be retried.
// 500 means internal server error ("We encountered an internal error. Please try again.")
// 503 means service not available or the requests are coming too fast, so try again later.
// In any case, we'll retry after a minimum delay to avoid spamming retries.
#if DEBUG
NSLog(@"Background file upload to S3 failed with HTTP status %ld; retrying after delay", (long)httpStatusCode);
#endif
[self setRetryAfterDelayForFile:filePath];
} break;
default: {
// iOS handles redirects automatically so only e.g. 304 resource not changed etc. from the 300 range should end up here
// (along with all unhandled 4xx and 5xx of course).
NSString *description = [NSString stringWithFormat:@"Background file upload to S3 failed with HTTP status %ld", (long)httpStatusCode];
NSError *s3Error = [NSError errorWithDomain:SBB_ERROR_DOMAIN code:SBBErrorCodeS3UploadErrorResponse userInfo:@{NSLocalizedDescriptionKey: description}];
[self completeUploadOfFile:filePath withError:s3Error];
} break;
}
}
// --- Here's the completion handler for the step where we upload the file to S3:
- (void)uploadedFileToS3WithTask:(NSURLSessionUploadTask *)uploadTask response:(NSHTTPURLResponse *)httpResponse error:(NSError *)error
{
NSInteger httpStatusCode = httpResponse.statusCode;
NSString *filePath = uploadTask.taskDescription;
// client-side networking issue
if (error) {
[self completeUploadOfFile:filePath withError:error];
return;
}
// server didn't like the request, or otherwise hiccupped
if (httpStatusCode >= 300) {
[self handleUploadHTTPStatusCode:httpStatusCode forUploadFilePath:filePath];
// early exit
return;
}
// tell the API we done did it
SBBUploadSession *uploadSession = [self uploadSessionForFile:filePath];
[self setUploadSessionJSON:nil forFile:filePath];
NSString *uploadId = uploadSession.id;
if (!uploadId.length) {
NSAssert(uploadId, @"uploadId is nil");
}
NSString *ref = [NSString stringWithFormat:kSBBUploadCompleteAPIFormat, uploadId];
NSMutableDictionary *headers = [NSMutableDictionary dictionary];
[self.authManager addAuthHeaderToHeaders:headers];
[self.networkManager post:ref headers:headers parameters:nil background:YES completion:^(NSURLSessionTask *task, id responseObject, NSError *error) {
[self toldBridgeFileUploadedWithTask:uploadTask forBridgeSession:uploadSession error:error];
}];
}
// --- And here's the completion handler for getting the SBBUploadSession in the first place:
- (void)downloadedBridgeUploadSessionWithDownloadTask:(NSURLSessionDownloadTask *)downloadTask fileURL:(NSURL *)file
{
NSError *error;
NSString *uploadFileURL = downloadTask.taskDescription;
if ([self uploadSessionForFile:uploadFileURL]) {
// already got one--this means we retried requesting the upload session for the file because the initial
// Bridge request took too long to respond, but has now responded; but we only need one of these to
// go on to attempt the upload to S3, so we'll just ignore this one
return;
}
NSData *jsonData = [NSData dataWithContentsOfURL:file options:0 error:&error];
if (error) {
NSLog(@"Error reading downloaded UploadSession file into an NSData object:\n%@", error);
[self completeUploadOfFile:uploadFileURL withError:error];
return;
}
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
if (error) {
[self completeUploadOfFile:uploadFileURL withError:error];
NSLog(@"Error deserializing downloaded UploadSession data into objects:\n%@", error);
return;
}
SBBUploadSession *uploadSession = [self.cleanObjectManager objectFromBridgeJSON:jsonObject];
SBBUploadRequest *uploadRequest = [self uploadRequestForFile:uploadFileURL];
if (!uploadRequest || !uploadRequest.contentLength || !uploadRequest.contentType || !uploadRequest.contentMd5) {
NSLog(@"Failed to retrieve upload request headers for temp file %@", uploadFileURL);
NSString *desc = [NSString stringWithFormat:@"Error retrieving upload request headers for temp file URL:\n%@", uploadFileURL];
error = [NSError errorWithDomain:SBB_ERROR_DOMAIN code:SBBErrorCodeTempFileError userInfo:@{NSLocalizedDescriptionKey: desc}];
[self completeUploadOfFile:uploadFileURL withError:error];
return;
}
if ([uploadSession isKindOfClass:[SBBUploadSession class]]) {
#if DEBUG
NSLog(@"Successfully obtained upload session with upload ID %@", uploadSession.id);
#endif
[self setUploadSessionJSON:jsonObject forFile:uploadFileURL];
NSDictionary *uploadHeaders =
@{
@"Content-Length": [uploadRequest.contentLength stringValue],
@"Content-Type": uploadRequest.contentType,
@"Content-MD5": uploadRequest.contentMd5
};
NSURL *fileUrl = [NSURL fileURLWithPath:uploadFileURL];
[self.networkManager uploadFile:fileUrl httpHeaders:uploadHeaders toUrl:uploadSession.url taskDescription:uploadFileURL completion:^(NSURLSessionTask *task, NSHTTPURLResponse *response, NSError *error) {
#if DEBUG
if (error || response.statusCode >= 300) {
NSLog(@"Error uploading to S3 for upload ID %@\nHTTP status: %@\n%@", uploadSession.id, @(response.statusCode), error);
} else {
NSLog(@"Successfully uploaded to S3 for upload ID %@", uploadSession.id);
}
#endif
[self uploadedFileToS3WithTask:(NSURLSessionUploadTask *)task response:response error:error];
}];
} else {
// the response from Bridge was an error message; we already handled this in the task completion
// block for fetching the UploadSession
}
}
- (void)uploadFileToBridge:(NSURL *)fileUrl completion:(SBBUploadManagerCompletionBlock)completion
{
NSString *extension = fileUrl.pathExtension;
NSString *UTI = (__bridge_transfer NSString *)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)extension, NULL);
NSString *contentType = (__bridge_transfer NSString *)UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)UTI, kUTTagClassMIMEType);
if (!contentType) {
contentType = @"application/octet-stream";
}
[self uploadFileToBridge:fileUrl contentType:contentType completion:completion];
}
- (void)uploadFileToBridge:(NSURL *)fileUrl contentType:(NSString *)contentType completion:(SBBUploadManagerCompletionBlock)completion
{
[self tempFileForUploadFileToBridge:fileUrl contentType:contentType completion:completion];
}
// internal method returns temp file URL for unit tests
- (NSURL *)tempFileForUploadFileToBridge:(NSURL *)fileUrl contentType:(NSString *)contentType completion:(SBBUploadManagerCompletionBlock)completion
{
if (![fileUrl isFileURL] || ![[NSFileManager defaultManager] isReadableFileAtPath:[fileUrl path]]) {
NSLog(@"Attempting to upload an URL that's not a readable file URL:\n%@", fileUrl);
if (completion) {
completion([NSError generateSBBNotAFileURLErrorForURL:fileUrl]);
}
if (_uploadDelegate) {
[_uploadDelegate uploadManager:self uploadOfFile:[fileUrl absoluteString] completedWithError:[NSError generateSBBNotAFileURLErrorForURL:fileUrl]];
}
return nil;
}
// If we already "know" about this upload, don't add it again; if it's hung up somehow, the orphaned file checking will catch it.
// If we don't have a completion handler for it, though, set it up with the given one before returning.
// Compare just the sandbox-relative parts, as the app sandbox path may have changed. But completion handlers, being in-memory-only,
// are indexed by the full path.
NSArray *uploadTempFiles = [[BridgeSDK sharedUserDefaults] dictionaryForKey:kUploadFilesKey].allKeys;
NSString *sandboxRelativePath = [fileUrl.path sandboxRelativePath];
for (NSString *uploadTempFile in uploadTempFiles) {
NSString *uploadFile = [self fileForTempFile:uploadTempFile];
if ([uploadFile isEqualToString:sandboxRelativePath]) {
[((SBBNetworkManager *)self.networkManager) performBlockOnBackgroundDelegateQueue:^{
[self setCompletionBlock:completion forFile:[uploadTempFile fullyQualifiedPath]];
}];
return nil;
}
}
// make a temp copy with a unique name
NSURL *tempFileURL = [self tempFileForFileURL:fileUrl];
if (!tempFileURL) {
if (completion) {
completion([NSError generateSBBTempFileErrorForURL:fileUrl]);
}
return nil;
}
[((SBBNetworkManager *)self.networkManager) performBlockOnBackgroundDelegateQueue:^{
[self setCompletionBlock:completion forFile:[tempFileURL path]];
}];
// default to generic binary file if type not specified
if (!contentType) {
contentType = @"application/octet-stream";
}
NSString *name = [fileUrl lastPathComponent];
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
NSError *fileCoordinatorError = nil;
__block NSData *fileData = nil;
[fileCoordinator coordinateReadingItemAtURL:tempFileURL options:0 error:&fileCoordinatorError byAccessor:^(NSURL *newURL) {
fileData = [NSData dataWithContentsOfURL:newURL];
}];
if (!fileData) {
if (completion) {
NSError *error = fileCoordinatorError ?: [NSError generateSBBTempFileReadErrorForURL:fileUrl];
completion(error);
}
return nil;
}
SBBUploadRequest *uploadRequest = [SBBUploadRequest new];
uploadRequest.name = name;
uploadRequest.contentLengthValue = fileData.length;
uploadRequest.contentType = contentType;
uploadRequest.contentMd5 = [fileData contentMD5];
NSString *filePath = [tempFileURL path];
// don't use the shared SBBObjectManager--we want to use only SDK default objects for types
NSDictionary *uploadRequestJSON = [self.cleanObjectManager bridgeJSONFromObject:uploadRequest];
[((SBBNetworkManager *)self.networkManager) performBlockOnBackgroundDelegateQueue:^{
[self kickOffUploadForFile:filePath uploadRequestJSON:uploadRequestJSON];
}];
return tempFileURL;
}
// This presumes uploadFileToBridge:contentType:completion: was called to set things up for this file,
// so can be used for both the initial upload attempt and any retries due to 403 (presigned url timed out).
// It also presumes it's being called from within the background NSURLSession's delegate queue.
- (void)kickOffUploadForFile:(NSString *)filePath uploadRequestJSON:(NSDictionary *)uploadRequestJSON
{
// this starts the process by downloading the Bridge UploadSession.
// first, make sure we get rid of any existing UploadSession for this file, in case this is a retry.
[self setUploadSessionJSON:nil forFile:filePath];
[self setUploadRequestJSON:uploadRequestJSON forFile:filePath];
NSMutableDictionary *headers = [NSMutableDictionary dictionary];
[self.authManager addAuthHeaderToHeaders:headers];
__block NSURLSessionDownloadTask *downloadTask = [self.networkManager downloadFileFromURLString:kSBBUploadAPI method:@"POST" httpHeaders:headers parameters:uploadRequestJSON taskDescription:filePath downloadCompletion:^(NSURL *file) {
[self downloadedBridgeUploadSessionWithDownloadTask:downloadTask fileURL:file];
} taskCompletion:^(NSURLSessionTask *task, NSHTTPURLResponse *response, NSError *error) {
// We don't care about this unless there was a network or HTTP error.
// Otherwise we'll have gotten and handled the downloaded file url in the downloadCompletion block, above.
if (error) {
if ([task.response isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)task.response;
NSInteger statusCode = httpResponse.statusCode;
if (statusCode < 300) {
// these are not the codes you're looking for--move along
return;
}
error = [NSError generateSBBErrorForStatusCode:statusCode];
if (error.code == SBBErrorCodeUnsupportedAppVersion) {
// 410 app version not supported--set it for delayed retry so it will go through after they upgrade the app
[self setRetryAfterDelayForFile:filePath];
} else if (error.code == SBBErrorCodeServerPreconditionNotMet) {
// 412 not consented--don't retry; we shouldn't be uploading data in the first place
[self completeUploadOfFile:task.taskDescription withError:error];
} else if (statusCode >= 500) {
// server issue--retry after a bit to give it a chance to clear up
#if DEBUG
NSLog(@"Request to Bridge for UploadSession failed with HTTP status %@. Will retry after delay.", @(statusCode));
#endif
[self setRetryAfterDelayForFile:filePath];
} else {
// other 4xx--not client-recoverable, so just fail gracefully-ish
[self completeUploadOfFile:task.taskDescription withError:error];
}
} else {
// network error--we'll only get here if the error didn't include resume data
#if DEBUG
NSLog(@"Request to Bridge for UploadSession failed due to network error. Will retry after delay.");
#endif
[self setRetryAfterDelayForFile:filePath];
}
}
}];
}
// expects a fully-qualified path
- (void)retryOrphanedUploadFromScratch:(NSString *)filePath
{
NSFileManager *fileMan = [NSFileManager defaultManager];
if ([fileMan fileExistsAtPath:filePath]) {
// on the off chance the original completion block is still around...
SBBUploadManagerCompletionBlock completion = [self completionBlockForFile:filePath];
// if there's no old completion handler, get the old "original" file and delete it; the temp file is our new original
// also create a new completion handler to delete this file upon successful upload
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
if (!completion) {
NSString *originalFile = [[self fileForTempFile:filePath] fullyQualifiedPath];
if (originalFile.length) {
[fileCoordinator coordinateWritingItemAtURL:[NSURL fileURLWithPath:originalFile] options:NSFileCoordinatorWritingForDeleting error:nil byAccessor:^(NSURL * _Nonnull newURL) {
if ([fileMan fileExistsAtPath:newURL.path]) {
[fileMan removeItemAtURL:newURL error:nil];
}
}];
}
// now remove any and all other temp files which are copies of this same original
// (both the temp files themselves, and their map entries)
[self removeTempFilesWithOriginalFile:filePath exceptFile:filePath];
}
// remove it from the upload files map so we don't leave it hanging around
[self removeFileForTempFile:filePath];
// remove any existing uploads that map to this file, because we're going to check for that in
// the upload method to prevent duplicating uploads-in-progress when BridgeAppSDK calls us to re-try
// orphaned /tmp files
[self removeTempFilesWithOriginalFile:filePath exceptFile:filePath];
// let the SDK figure out the contentType
[self uploadFileToBridge:[NSURL fileURLWithPath:filePath] completion:^(NSError *error) {
if (!error) {
// now that it's been uploaded, delete it
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
if (!fileUrl) {
NSLog(@"Could not create NSURL for file %@", filePath);
}
[fileCoordinator coordinateWritingItemAtURL:fileUrl options:NSFileCoordinatorWritingForDeleting error:nil byAccessor:^(NSURL * _Nonnull newURL) {
NSError *error;
if (![fileMan removeItemAtURL:newURL error:&error]) {
NSLog(@"Failed to remove file %@, error:\n%@", fileUrl, error);
}
}];
}
if (completion) {
completion(error);
}
}];
} else {
// ¯\_(ツ)_/¯
NSLog(@"File %@ no longer exists, removing from upload files", filePath);
[self cleanUpTempFile:filePath];
}
}
- (NSArray<NSURL *> *)filesUnderDirectory:(NSURL *)baseDir
{
NSFileManager *fileMan = [NSFileManager defaultManager];
NSDirectoryEnumerator *directoryEnumerator =
[fileMan enumeratorAtURL:baseDir
includingPropertiesForKeys:@[NSURLNameKey, NSURLIsDirectoryKey]
options:NSDirectoryEnumerationSkipsHiddenFiles
errorHandler:nil];
NSMutableArray<NSURL *> *mutableFileURLs = [NSMutableArray array];
for (NSURL *fileURL in directoryEnumerator) {
NSNumber *isDirectory = nil;
[fileURL getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:nil];
if (![isDirectory boolValue]) {
// normalize the url and add to the list
[mutableFileURLs addObject:[fileURL URLByResolvingSymlinksInPath]];
}
}
return [mutableFileURLs copy];
}
- (NSArray<__kindof NSURLSessionTask *> * _Nullable)cancelTasksForFile:(NSString *)file inTasks:(NSArray<__kindof NSURLSessionTask *> * _Nullable)tasks
{
NSMutableArray<__kindof NSURLSessionTask *> *tasksForFile = [NSMutableArray array];
// first check if our file is the same as the task file
for (NSURLSessionTask *task in tasks) {
NSString *taskFile = task.taskDescription;
if ([file isEquivalentToPath:taskFile]) {
[tasksForFile addObject:task];
} else {
// now check if our file is a temp copy of the task file
NSString *fileForTempFile = [self fileForTempFile:file];
if ([fileForTempFile isEquivalentToPath:taskFile]) {
[tasksForFile addObject:task];
} else {
// now check if the task file is a temp copy of our file
NSString *fileForTaskTempFile = [self fileForTempFile:taskFile];
if ([fileForTaskTempFile isEquivalentToPath:file]) {
[tasksForFile addObject:task];
} else if ([fileForTaskTempFile isEquivalentToPath:fileForTempFile]) {
// if the task file and our file are both temp copies of the same file
[tasksForFile addObject:task];
}
}
}
}
NSMutableArray *newTasks = [tasks mutableCopy];
for (NSURLSessionTask *task in tasksForFile) {
[newTasks removeObject:task];
[task cancel];
NSString *taskFile = task.description;
if (![taskFile isEquivalentToPath:file]) {
// make sure to remove its reference from our known uploads in progress, since
// it's no longer an upload in progress
[self removeFileForTempFile:task.description];
}
}
return [newTasks copy];
}
+ (BOOL) isAppExtension
{
return [[[NSBundle mainBundle] executablePath] containsString:@".appex/"];
}
- (void)checkAndRetryOrphanedUploads
{
if ([SBBUploadManager isAppExtension]) {
// App extensions are severely memory constrained, so don't even try this if we're in one.
return;
}
NSURLSession *bgSession = ((SBBNetworkManager *)self.networkManager).backgroundSession;
void (^block)(NSArray<__kindof NSURLSessionTask *> * _Nullable tasks) = ^(NSArray<__kindof NSURLSessionTask *> *tasks) {
@autoreleasepool {
NSUserDefaults *defaults = [BridgeSDK sharedUserDefaults];
NSFileManager *fileMan = [NSFileManager defaultManager];
NSMutableSet *filesRetrying = [NSMutableSet set];
// assume any outstanding upload requests without corresponding upload sessions whose files are
// more than a day old are orphaned (note that under some unusual circumstances this may lead
// to duplication of uploads).
static const NSTimeInterval oneDay = 24. * 60. * 60.;
NSArray *uploadFiles = [defaults dictionaryForKey:kUploadFilesKey].allKeys;
NSDictionary *uploadRequests = [defaults dictionaryForKey:kUploadRequestsKey];
NSDictionary *uploadSessions = [defaults dictionaryForKey:kUploadSessionsKey];
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
for (NSString *relativePath in uploadRequests.allKeys) {
@autoreleasepool {
NSString *filePath = [relativePath fullyQualifiedPath];
if ([filesRetrying containsObject:filePath]) {
continue; // skip it, we're already retrying this one
}
if (!uploadSessions[relativePath]) {
// get the modification date of the file, if it still exists
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
__block NSDictionary *fileAttrs;
[fileCoordinator coordinateReadingItemAtURL:fileURL options:NSFileCoordinatorReadingImmediatelyAvailableMetadataOnly error:nil byAccessor:^(NSURL * _Nonnull newURL) {
fileAttrs = [fileMan attributesOfItemAtPath:filePath error:nil];
}];
if (fileAttrs) {
NSDate *modified = [fileAttrs fileModificationDate];
if ([modified timeIntervalSinceNow] < -oneDay) {
// it's more than a day old, so we will retry now
// but first, if it's in the retry queue, take it out
[self setRetryTime:nil forFile:filePath];
// ...and update its modification date so we don't keep thinking it's due for a retry
[fileCoordinator coordinateWritingItemAtURL:fileURL options:NSFileCoordinatorWritingContentIndependentMetadataOnly error:nil byAccessor:^(NSURL * _Nonnull newURL) {
[fileMan setAttributes:@{NSFileModificationDate:[NSDate date]} ofItemAtPath:filePath error:nil];
}];
// ...and cancel any existing tasks trying to upload the same file
tasks = [self cancelTasksForFile:filePath inTasks:tasks];
// ok, now do the retry
NSDictionary *uploadRequestJSON = [self uploadRequestJSONForFile:filePath];
[filesRetrying addObject:filePath];