Permalink
Browse files

Improved the checking for un-uploaded files. It now uses the file's l…

…ast modified date, instead of storing dates in a dictionary.
  • Loading branch information...
1 parent 5128cc0 commit e374f606672d1fb7376da8bbd05589ddf95bd9c6 @drewmccormack committed Jan 30, 2013
@@ -24,7 +24,6 @@ extern NSString * const TICDSApplicationSyncManagerDidRefreshCloudTransferProgre
NSURL *_applicationContainingDirectoryLocation;
NSMetadataQuery *_cloudMetadataQuery;
NSMetadataQuery *_transferProgressMetadataQuery;
- NSMutableDictionary *_unfinishedUploadDatesByURL;
}
@property (readonly) unsigned long long cloudBytesToUpload, cloudBytesToDownload;
@@ -160,14 +160,6 @@ - (void)refreshCloudMetadataQuery
self.cloudMetadataQuery = newQuery;
}
-- (NSString *)unfinishedUploadsDataFile
-{
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES);
- NSString *directory = paths.lastObject;
- directory = [directory stringByAppendingPathComponent:[[[NSBundle mainBundle] infoDictionary] objectForKey:(NSString *)kCFBundleNameKey]];
- return [directory stringByAppendingPathComponent:@"UnfinishedCloudUploads.plist"];
-}
-
- (void)initiateDownloadsForURLs:(NSArray *)urls
{
NSFileManager *fm = [[NSFileManager alloc] init];
@@ -187,13 +179,6 @@ - (void)initiateDownloadsForURLs:(NSArray *)urls
- (void)checkUninitiatedUploadsForURLs:(NSArray *)urls
{
NSFileManager *fm = [[NSFileManager alloc] init];
-
- // Load unfinished upload dates
- NSString *uploadsFile = [self unfinishedUploadsDataFile];
- if ( !_unfinishedUploadDatesByURL ) {
- _unfinishedUploadDatesByURL = [[NSMutableDictionary alloc] initWithContentsOfFile:uploadsFile];
- if ( !_unfinishedUploadDatesByURL ) _unfinishedUploadDatesByURL = [[NSMutableDictionary alloc] init];
- }
// Create temporary directory for moved aside files
NSString *tempDirPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"MovedAsideCloudFiles"];
@@ -213,59 +198,40 @@ - (void)checkUninitiatedUploadsForURLs:(NSArray *)urls
[url getResourceValue:&uploaded forKey:NSURLUbiquitousItemIsUploadedKey error:NULL];
[url getResourceValue:&uploading forKey:NSURLUbiquitousItemIsUploadingKey error:NULL];
if ( (uploaded && !uploaded.boolValue) && (uploading && !uploading.boolValue) ) {
- NSDate *firstUploadFailureDate = [_unfinishedUploadDatesByURL objectForKey:url];
- if ( !firstUploadFailureDate ) {
- // New file
- [_unfinishedUploadDatesByURL setObject:[NSDate date] forKey:url];
- }
- else if ( [firstUploadFailureDate timeIntervalSinceNow] < -ReuploadTimeInterval ) {
- // Overdue file. Try to jolt.
- [_unfinishedUploadDatesByURL removeObjectForKey:url];
-
- NSString *tempFilePath = [tempDirPath stringByAppendingPathComponent:url.lastPathComponent];
- NSURL *tempURL = [NSURL fileURLWithPath:tempFilePath];
-
- __block NSError *anyError = nil;
- __block BOOL success = NO;
- NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
-
- [fileCoordinator coordinateReadingItemAtURL:url options:0 writingItemAtURL:tempURL options:NSFileCoordinatorWritingForReplacing error:&anyError byAccessor:^(NSURL *newReadingURL, NSURL *newWritingURL) {
- [fm removeItemAtURL:newWritingURL error:NULL];
- success = [fm copyItemAtURL:newReadingURL toURL:newWritingURL error:&anyError];
- if ( !success ) NSLog(@"%@", anyError);
- }];
-
- if ( success ) [fileCoordinator coordinateWritingItemAtURL:tempURL options:NSFileCoordinatorWritingForDeleting writingItemAtURL:url options:NSFileCoordinatorWritingForReplacing error:&anyError byAccessor:^(NSURL *newFromURL, NSURL *newToURL) {
- [fm removeItemAtURL:newToURL error:NULL];
- success = [fm moveItemAtURL:newFromURL toURL:newToURL error:&anyError];
- [fileCoordinator itemAtURL:newFromURL didMoveToURL:newToURL];
- if ( !success ) NSLog(@"%@", anyError);
- }];
-
- [fileCoordinator release];
+ NSDate *modifiedDate = nil;
+ BOOL success = [url getResourceValue:&modifiedDate forKey:NSURLContentModificationDateKey error:NULL];
+ BOOL fileIsOverdue = [modifiedDate timeIntervalSinceNow] < -ReuploadTimeInterval;
+ if ( !success || modifiedDate == nil || fileIsOverdue ) {
+ BOOL uploadStarted = [fm startDownloadingUbiquitousItemAtURL:url error:NULL]; // This triggers a sync with the server
+ if ( !uploadStarted || fileIsOverdue ) {
+ // Copy file out of container, and then back in
+ NSString *tempFilePath = [tempDirPath stringByAppendingPathComponent:url.lastPathComponent];
+ NSURL *tempURL = [NSURL fileURLWithPath:tempFilePath];
+
+ __block NSError *anyError = nil;
+ __block BOOL success = NO;
+ NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
+
+ [fileCoordinator coordinateReadingItemAtURL:url options:0 writingItemAtURL:tempURL options:NSFileCoordinatorWritingForReplacing error:&anyError byAccessor:^(NSURL *newReadingURL, NSURL *newWritingURL) {
+ [fm removeItemAtURL:newWritingURL error:NULL];
+ success = [fm copyItemAtURL:newReadingURL toURL:newWritingURL error:&anyError];
+ if ( !success ) NSLog(@"%@", anyError);
+ }];
+
+ if ( success ) [fileCoordinator coordinateWritingItemAtURL:tempURL options:NSFileCoordinatorWritingForDeleting writingItemAtURL:url options:NSFileCoordinatorWritingForReplacing error:&anyError byAccessor:^(NSURL *newFromURL, NSURL *newToURL) {
+ [fm removeItemAtURL:newToURL error:NULL];
+ success = [fm moveItemAtURL:newFromURL toURL:newToURL error:&anyError];
+ [fileCoordinator itemAtURL:newFromURL didMoveToURL:newToURL];
+ if ( !success ) NSLog(@"%@", anyError);
+ }];
+
+ [fileCoordinator release];
+ }
}
}
- else {
- // File is uploaded or irrelevant
- [_unfinishedUploadDatesByURL removeObjectForKey:url];
- }
}
}
- // Remove very old entries that are no longer relevant.
- const NSTimeInterval UploadEntryExpiryTimeInterval = 7*24*60*60; // 7 days
- NSArray *uploadURLs = [NSArray arrayWithArray:_unfinishedUploadDatesByURL.allKeys];
- for ( id url in uploadURLs ) {
- NSDate *date = [_unfinishedUploadDatesByURL objectForKey:url];
- if ( [date timeIntervalSinceNow] < -UploadEntryExpiryTimeInterval ) {
- [_unfinishedUploadDatesByURL removeObjectForKey:url];
- }
- }
-
- // Save unfinished uploads.
- [fm createDirectoryAtPath:[uploadsFile stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:NULL];
- [_unfinishedUploadDatesByURL writeToFile:uploadsFile atomically:YES];
-
[fm removeItemAtPath:tempDirPath error:NULL];
[fm release];
}
@@ -452,15 +418,6 @@ - (NSString *)pathToWholeStoreDirectoryForDocumentWithIdentifier:(NSString *)anI
#pragma mark -
#pragma mark Initialization and Deallocation
-- (id) init
-{
- self = [super init];
- if ( self ) {
- _unfinishedUploadDatesByURL = nil;
- }
- return self;
-}
-
- (void)dealloc
{
[_applicationContainingDirectoryLocation release], _applicationContainingDirectoryLocation = nil;
@@ -471,7 +428,6 @@ - (void)dealloc
[_cloudMetadataQuery release], _cloudMetadataQuery = nil;
[_transferProgressMetadataQuery stopQuery];
[_transferProgressMetadataQuery release], _transferProgressMetadataQuery = nil;
- [_unfinishedUploadDatesByURL release], _unfinishedUploadDatesByURL = nil;
[super dealloc];
}

0 comments on commit e374f60

Please sign in to comment.