Eliminate duplicate media

This commit is contained in:
Matthias Neeracher 2011-08-18 19:15:32 +02:00
parent 8614de9959
commit 23ae001590

View File

@ -42,17 +42,35 @@
- (void)addMediaURL:(NSURL *)url name:(NSString *)name date:(NSDate *)date
{
NSManagedObjectContext *moc = [self managedObjectContext];
MAMedia * media = [NSEntityDescription insertNewObjectForEntityForName:@"MAMedia" inManagedObjectContext:moc];
NSString * path = [url path];
NSNumber * size;
[url getResourceValue:&size forKey:NSURLFileSizeKey error:nil];
int64_t fileSize = [size longLongValue];
NSTimeInterval fileDate = [date timeIntervalSinceReferenceDate];
media.media = path;
media.date = [date timeIntervalSinceReferenceDate];
NSManagedObjectContext *moc = [self managedObjectContext];
MAMedia * media = nil;
//
// Look for existing file with same name, file size, and (preferably) date first.
// Since the date may be parsed from the file name (and subject to a later fix),
// we tolerate mismatches.
//
NSFetchRequest * fetch = [NSFetchRequest fetchRequestWithEntityName:@"MAMedia"];
[fetch setPredicate:[NSPredicate predicateWithFormat:@"name == %@", name]];
for (MAMedia * m in [moc executeFetchRequest:fetch error:nil])
if (m.size == fileSize) {
media = m;
if (m.date == fileDate)
break;
}
if (!media) {
media = [NSEntityDescription insertNewObjectForEntityForName:@"MAMedia" inManagedObjectContext:moc];
media.size = fileSize;
media.name = name;
media.notes = @"";
media.size = size ? [size longLongValue] : 0;
}
media.date = fileDate;
media.media = path;
}
#pragma mark Annotation management