-
Notifications
You must be signed in to change notification settings - Fork 0
/
FolderExportController.m
117 lines (99 loc) · 3.05 KB
/
FolderExportController.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
//
// FolderExportController.m
// Tagalog
//
// Created by Nathan Vander Wilt on 5/13/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "FolderExportController.h"
#import "TLMainThreadPerformer.h"
#import "NSFileManager+TLExtensions.h"
#import "TLPhotoSourceItem.h"
extern NSString* TLMakeUUID(void);
@implementation FolderExportController
@synthesize folder;
@synthesize exportedPaths = mutableExportedPaths;
- (id)init {
self = [super init];
if (self) {
mutableExportedPaths = [NSMutableArray new];
}
return self;
}
- (void)dealloc {
[self setFolder:nil];
[mutableExportedPaths release];
[super dealloc];
}
#pragma mark Export implementation
- (BOOL)prepareForExport:(NSError**)err {
BOOL isDirectory = NO;
BOOL exists = [[NSFileManager tlThreadManager] fileExistsAtPath:[self folder]
isDirectory:&isDirectory];
if (!exists) {
if (err) {
NSDictionary* errInfo = [NSDictionary dictionaryWithObject:[self folder]
forKey:NSFilePathErrorKey];
*err = [NSError errorWithDomain:NSPOSIXErrorDomain
code:ENOENT
userInfo:errInfo];
}
return NO;
}
if (!isDirectory) {
if (err) {
NSDictionary* errInfo = [NSDictionary dictionaryWithObject:[self folder]
forKey:NSFilePathErrorKey];
*err = [NSError errorWithDomain:NSPOSIXErrorDomain
code:ENOTDIR
userInfo:errInfo];
}
return NO;
}
return YES;
}
- (NSDictionary*)photoExportOptions {
return nil;
/* TODO: old code
[NSDictionary dictionaryWithObject:[NSTimeZone systemTimeZone]
forKey:TLPhotoTimezoneExportOption];
*/
}
- (BOOL)exportItem:(TLPhotoSourceItem*)item
withMetadata:(NSDictionary*)metadata
error:(NSError**)err
{
// export file to unique hidden path
NSString* photoName = [[item originalFilename] stringByDeletingPathExtension];
NSString* photoExtension = [[item originalFilename] pathExtension];
NSString* hiddenPhotoName = [NSString stringWithFormat:
@".temp.%@.%@.%@", photoName, TLMakeUUID(), photoExtension];
NSString* tempPath = [[self folder] stringByAppendingPathComponent:hiddenPhotoName];
BOOL exported = [item exportToPath:[NSURL fileURLWithPath:tempPath isDirectory:NO]
metadata:metadata
options:nil
error:err];
if (!exported) return NO;
// move hidden export to a unique visible file
NSString* exportPath = [[self folder] stringByAppendingPathComponent:[item originalFilename]];
NSString* finalExportPath = [[NSFileManager tlThreadManager] tlMoveItemAtPath:tempPath
toUniquePath:exportPath
error:err];
if (!finalExportPath) return NO;
[mutableExportedPaths addObject:finalExportPath];
return YES;
}
- (BOOL)copyItem:(TLPhotoSourceItem*)item
error:(NSError**)err
{
(void)item;
(void)err;
return [self exportItem:item withMetadata:nil error:err];
}
- (void)cancelExport {
for (NSString* exportedPath in [self exportedPaths]) {
(void)[[NSFileManager tlThreadManager] removeItemAtPath:exportedPath error:NULL];
}
[mutableExportedPaths removeAllObjects];
}
@end