-
Notifications
You must be signed in to change notification settings - Fork 0
/
TLFileSourceItem.m
268 lines (226 loc) · 9.3 KB
/
TLFileSourceItem.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
//
// TLFileSourceItem.m
// Tagalog
//
// Created by Nathan Vander Wilt on 6/5/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TLFileSourceItem.h"
#import "TLFilePhotoSource.h"
#import "TLCocoaToolbag.h"
#import "NSFileManager+TLExtensions.h"
#import "TLLocation.h"
#import "TLTimestamp.h"
extern NSDateFormatter* TLPhotoSourceExifTimestampParser(void);
extern BOOL TLPhotoAddMetadata(NSURL* originalURL, NSURL* targetURL,
NSDictionary* metadata, NSError** err);
@interface TLFileSourceItem ()
@property (nonatomic, readwrite, copy) NSString* filePath;
@property (nonatomic, copy) NSString* fileUTI;
@property (nonatomic, copy) NSDictionary* metadata;
@property (nonatomic, readonly) CGImageSourceRef imageSource;
- (void)updateWithProperties:(NSDictionary*)theProperties;
@end
@implementation TLFileSourceItem
@synthesize filePath;
@synthesize fileUTI;
@synthesize metadata;
@synthesize imageSource;
- (id)initWithSource:(TLFilePhotoSource*)source
filePath:(NSString*)theFilePath
error:(NSError**)err
{
self = [super initWithSource:source];
if (self) {
NSURL* fileURL = [NSURL fileURLWithPath:theFilePath isDirectory:NO];
NSString* theUTI = TLFileGetUTI(fileURL);
[self setFileUTI:theUTI];
CGImageSourceRef theImageSource = NULL;
if (UTTypeConformsTo((CFStringRef)theUTI, kUTTypeImage)) {
NSDictionary* sourceOptions = [NSDictionary dictionaryWithObjectsAndKeys:
theUTI, (id)kCGImageSourceTypeIdentifierHint, nil];
theImageSource = CGImageSourceCreateWithURL((CFURLRef)fileURL,
(CFDictionaryRef)sourceOptions);
}
if (theImageSource && !CGImageSourceGetCount(theImageSource)) {
CFRelease(theImageSource);
[super dealloc];
if (err) {
NSDictionary* errInfo = [NSDictionary dictionaryWithObjectsAndKeys:
theFilePath, NSFilePathErrorKey, nil];
*err = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadCorruptFileError userInfo:errInfo];
}
return nil;
}
if (theImageSource) {
CFDictionaryRef imgProperties = CGImageSourceCopyPropertiesAtIndex(theImageSource, 0, NULL);
if (imgProperties) {
[self updateWithProperties:(NSDictionary*)imgProperties];
CFRelease(imgProperties);
}
}
imageSource = theImageSource;
[self setFilePath:theFilePath];
}
return self;
}
- (void)dealloc {
[self setFilePath:nil];
[self setFileUTI:nil];
[self setMetadata:nil];
if (imageSource) CFRelease(imageSource), imageSource = NULL;
[super dealloc];
}
- (void)updateWithProperties:(NSDictionary*)theProperties {
//NSLog(@"%@", theProperties);
NSMutableDictionary* theMetadata = [[[self metadata] mutableCopy] autorelease];
if (!theMetadata) {
theMetadata = [NSMutableDictionary dictionary];
}
NSString* timestampString = [[theProperties objectForKey:(id)kCGImagePropertyExifDictionary]
objectForKey:(id)kCGImagePropertyExifDateTimeOriginal];
if (!timestampString) {
timestampString = [[theProperties objectForKey:(id)kCGImagePropertyExifDictionary]
objectForKey:(id)kCGImagePropertyExifDateTimeDigitized];
if (!timestampString) {
timestampString = [[theProperties objectForKey:(id)kCGImagePropertyTIFFDictionary]
objectForKey:(id)kCGImagePropertyTIFFDateTime];
}
}
NSDate* itemDate = nil;
if (timestampString) {
NSDateFormatter* timestampParser = TLPhotoSourceExifTimestampParser();
itemDate = [timestampParser dateFromString:timestampString];
}
if (itemDate) {
TLTimestamp* timestamp = [TLTimestamp timestampWithTime:itemDate
accuracy:TLTimestampAccuracyUnknown];
[theMetadata setObject:timestamp forKey:TLMetadataTimestampKey];
}
[self setMetadata:theMetadata];
}
- (BOOL)canGeotag {
// NOTE: we don't currently pay attention to which types ExifTool can actually tag
return UTTypeConformsTo((CFStringRef)[self fileUTI], kUTTypeImage);
}
- (NSString*)originalFilename {
return [[self filePath] lastPathComponent];
}
+ (CFDictionaryRef)thumbnailOptionsForSize:(CGFloat)size {
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailFromImageAlways,
[NSNumber numberWithDouble:size], (id)kCGImageSourceThumbnailMaxPixelSize,
(id)kCFBooleanTrue, (id)kCGImageSourceCreateThumbnailWithTransform, nil];
return (CFDictionaryRef)options;
}
- (CGImageRef)newThumbnailForSize:(CGFloat)approximateSize
options:(NSDictionary*)options
error:(NSError**)err
{
(void)approximateSize;
(void)options;
if (![self imageSource]) {
if (err) {
*err = [NSError errorWithDomain:NSCocoaErrorDomain
code:NSFileReadCorruptFileError
userInfo:nil];
}
return NULL;
}
CFDictionaryRef thumbnailOptions = [[self class] thumbnailOptionsForSize:approximateSize];
CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex([self imageSource], 0, thumbnailOptions);
if (!thumbnail && err) {
*err = [NSError errorWithDomain:NSCocoaErrorDomain code:NSFileReadCorruptFileError userInfo:nil];
}
return thumbnail;
}
- (BOOL)exportToPath:(NSURL*)path
metadata:(NSDictionary*)theMetadata
options:(NSDictionary*)options
error:(NSError**)err
{
(void)options;
if (theMetadata) {
return TLPhotoAddMetadata([NSURL fileURLWithPath:[self filePath] isDirectory:NO], path,
theMetadata, err);
}
else {
return [[NSFileManager tlThreadManager] copyItemAtPath:[self filePath]
toPath:[path path]
error:err];
}
}
@end
BOOL TLPhotoAddMetadata(NSURL* originalURL, NSURL* targetURL,
NSDictionary* metadata, NSError** err)
{
TLLocation* location = [metadata objectForKey:TLMetadataLocationKey];
TLTimestamp* timestamp = [metadata objectForKey:TLMetadataTimestampKey];
NSTimeZone* timeZone = [metadata objectForKey:TLMetadataTimezoneKey];
if (!targetURL) {
targetURL = originalURL;
}
// copy original file to target path
if (![targetURL isEqualTo:originalURL]) {
NSFileManager* fileManager = [[NSFileManager new] autorelease];
BOOL itemCopied = [fileManager copyItemAtPath:[originalURL path] toPath:[targetURL path] error:err];
if (!itemCopied) return NO;
}
NSMutableArray* exifToolArgs = [NSMutableArray array];
// remove any existing location metadata, so as to not to merge two different sets of values
[exifToolArgs addObject:@"-GPS:all="];
// add location (datum, lat/lon/altitude+refs)
[exifToolArgs addObject:@"-GPSMapDatum=WGS-84"];
TLCoordinate coord = [location coordinate];
[exifToolArgs addObject:[NSString stringWithFormat:@"-GPSLatitude=%f", fabs(coord.lat)]];
[exifToolArgs addObject:[NSString stringWithFormat:@"-GPSLatitudeRef=%c", (coord.lat < 0.0 ? 'S' : 'N')]];
[exifToolArgs addObject:[NSString stringWithFormat:@"-GPSLongitude=%f", fabs(coord.lon)]];
[exifToolArgs addObject:[NSString stringWithFormat:@"-GPSLongitudeRef=%c", (coord.lon < 0.0 ? 'W' : 'E')]];
TLCoordinateAltitude altitude = [location altitude];
if (altitude != TLCoordinateAltitudeUnknown) {
[exifToolArgs addObject:[NSString stringWithFormat:@"-GPSAltitude=%f", fabs(altitude)]];
[exifToolArgs addObject:[NSString stringWithFormat:@"-GPSAltitudeRef=%c", (altitude < 0.0 ? '1' : '0')]];
}
// TODO: set GPSDOP if possible
// add original time, adjusted for time zone
NSDate* photoDate = [timestamp time];
NSTimeInterval offset = [timeZone secondsFromGMTForDate:photoDate];
NSDate* adjustedDate = [photoDate addTimeInterval:offset];
NSDateFormatter* exifFormat = [[NSDateFormatter new] autorelease];
NSCalendar* gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
[exifFormat setCalendar:gregorian];
[exifFormat setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
[exifFormat setDateFormat:@"yyyy:MM:dd HH:mm:ss"];
NSString* dateString = [exifFormat stringFromDate:adjustedDate];
[exifToolArgs addObject:[NSString stringWithFormat:@"-DateTimeOriginal=%@", dateString]];
// TODO: add subseconds when available
// set software name
NSDictionary* appInfo = [[NSBundle mainBundle] infoDictionary];
NSString* appName = [appInfo objectForKey:(id)kCFBundleNameKey];
NSString* appVersion = [appInfo objectForKey:@"CFBundleShortVersionString"];
NSString* fullAppName = [NSString stringWithFormat:@"%@ v%@", appName, appVersion];
[exifToolArgs addObject:[NSString stringWithFormat:@"-Software=%@", fullAppName]];
[exifToolArgs addObject:@"-overwrite_original"]; // @"-overwrite_original_in_place" (slower, retains extended attributes)
[exifToolArgs addObject:@"-n"];
[exifToolArgs addObject:@"-q"];
[exifToolArgs addObject:[targetURL path]];
//NSLog(@"%@", exifToolArgs);
NSString* exifToolPath = [[NSBundle mainBundle] pathForResource:@"exiftool" ofType:nil];
NSTask* exifToolTask = [NSTask launchedTaskWithLaunchPath:exifToolPath arguments:exifToolArgs];
[exifToolTask waitUntilExit];
int taskResult = [exifToolTask terminationStatus];
if (taskResult) {
if (err) {
NSMutableDictionary* errInfo = [NSMutableDictionary dictionary];
[errInfo setObject:@"Could not add geotag to image"
forKey:NSLocalizedDescriptionKey];
[errInfo setObject:@"This is most likely to occur if your image was in an unsupported RAW format"
forKey:NSLocalizedFailureReasonErrorKey];
[errInfo setObject:[[originalURL path] lastPathComponent]
forKey:NSFilePathErrorKey];
*err = [NSError errorWithDomain:@"com.calftrail.tagalog" code:-40 userInfo:errInfo];
}
return NO;
}
return YES;
}