Skip to content

Commit

Permalink
adds a hook in IRDataStore’s file operations to allow magic number de…
Browse files Browse the repository at this point in the history
…tection against files without extensions from application subclass
  • Loading branch information
Evadne Wu committed May 22, 2012
1 parent 7e0e173 commit 2207cbe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
5 changes: 5 additions & 0 deletions IRFoundations/IRDataStore+FileOperations.h
Expand Up @@ -40,4 +40,9 @@

- (BOOL) updateObject:(NSManagedObject *)anObject inContext:(NSManagedObjectContext *)aContext takingBlobFromTemporaryFile:(NSString *)aPath usingResourceType:(NSString *)utiType forKeyPath:(NSString *)fileKeyPath matchingURL:(NSURL *)anURL forKeyPath:(NSString *)urlKeyPath error:(NSError **)outError;


// Hook for MagicKit integration thru custom application subclass

- (NSString *) pathExtensionForFileAtPath:(NSString *)aPath;

@end
39 changes: 26 additions & 13 deletions IRFoundations/IRDataStore+FileOperations.m
Expand Up @@ -208,24 +208,31 @@ - (BOOL) updateObject:(NSManagedObject *)anObject inContext:(NSManagedObjectCont
if (utiType)
preferredExtension = (__bridge_transfer NSString *)(UTTypeCopyPreferredTagWithClass((__bridge CFStringRef)utiType, kUTTagClassFilenameExtension));

if (!preferredExtension)
preferredExtension = [self pathExtensionForFileAtPath:[fileURL path]];

if (preferredExtension) {

NSURL *newFileURL = [NSURL fileURLWithPath:[[[fileURL path] stringByDeletingPathExtension] stringByAppendingPathExtension:preferredExtension]];

NSError *movingError = nil;
BOOL didMove = [[NSFileManager defaultManager] moveItemAtURL:fileURL toURL:newFileURL error:&movingError];
if (!didMove) {

if (outError)
*outError = [NSError errorWithDomain:@"com.iridia.dataStore" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Could not rename the underlying persistent file", NSLocalizedDescriptionKey,
nil]];

return NO;

if (![fileURL isEqual:newFileURL]) {

NSError *movingError = nil;
BOOL didMove = [[NSFileManager defaultManager] moveItemAtURL:fileURL toURL:newFileURL error:&movingError];
if (!didMove) {

if (outError)
*outError = [NSError errorWithDomain:@"com.iridia.dataStore" code:0 userInfo:[NSDictionary dictionaryWithObjectsAndKeys:
@"Could not rename the underlying persistent file", NSLocalizedDescriptionKey,
nil]];

return NO;

}

fileURL = newFileURL;

}

fileURL = newFileURL;

}

Expand All @@ -235,4 +242,10 @@ - (BOOL) updateObject:(NSManagedObject *)anObject inContext:(NSManagedObjectCont

}

- (NSString *) pathExtensionForFileAtPath:(NSString *)aPath {

return [aPath pathExtension];

}

@end

0 comments on commit 2207cbe

Please sign in to comment.