forked from npctech/Tattle-UI-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTFileManager.m
More file actions
executable file
·296 lines (243 loc) · 9.67 KB
/
Copy pathTFileManager.m
File metadata and controls
executable file
·296 lines (243 loc) · 9.67 KB
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//
// TFileManager.m
/*
Copyright (c) 2014 TattleUI (http://www.npcompete.com/)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import "TFileManager.h"
#import "TConstants.h"
const NSString *SPOT_IT_DIR = @"/SpotIt";
const NSString *SCREEN_SHOT_DIR = @"/ScreenShots";
const NSString *AUDIO_DIR = @"/Audios";
const NSString *IMAGE_EXTENSION = @".png";
const NSInteger kMaxNumberOfAudios = 3;
@interface TFileManager()
@property(nonatomic, strong) NSString *recentFilePathForPlaying;
@end
@implementation TFileManager
#pragma mark API methods
#pragma mark sharedFileManager
+(TFileManager*)sharedFileManager
{
//Crearte singleton class
static dispatch_once_t predicate = 0;
static TFileManager *sharedInstance = nil;
dispatch_once(&predicate, ^{
sharedInstance = [[self alloc] init];
});
return sharedInstance;
}
#pragma mark Create a directory for given name
-(NSString*)documentsDirectoryAppendedWithPathComponent:(NSString*)append
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:(NSString*)SPOT_IT_DIR];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
{
NSError *error;
if(![[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error])
{
NSLog(@"Failed to create directory %@", dataPath);
return nil;
}
}
NSString *appendedPath=[dataPath stringByAppendingPathComponent:append];
return appendedPath;
}
#pragma mark create a directory for audio
-(NSString*)getAudioDirectoryPathWithAudioName:(NSString*)append
{
NSString *audioDir = [self documentsDirectoryAppendedWithPathComponent:(NSString*)AUDIO_DIR];
if(!audioDir)
{
return nil;
}
if (![[NSFileManager defaultManager] fileExistsAtPath:audioDir])
{
NSError *error;
if(![[NSFileManager defaultManager] createDirectoryAtPath:audioDir withIntermediateDirectories:NO attributes:nil error:&error])
{
NSLog(@"Failed to create directory %@", audioDir);
return nil;
}
}
NSString *appendedPath=[audioDir stringByAppendingPathComponent:append];
return appendedPath;
}
#pragma mark create screen name in screen shot directory
-(NSString*)getScreenShotDirectoryPathWithScreenName:(NSString*)append
{
NSString *screenDir = [self documentsDirectoryAppendedWithPathComponent:(NSString*)SCREEN_SHOT_DIR];
if(!screenDir)
{
return nil;
}
if (![[NSFileManager defaultManager] fileExistsAtPath:screenDir])
{
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:screenDir withIntermediateDirectories:NO attributes:nil error:&error];
}
NSString *appendedPath=[screenDir stringByAppendingPathComponent:append];
return appendedPath;
}
#pragma mark Archieve filepath
-(NSString*)getScreenShotFilePath
{
NSString *fileName = [NSString stringWithFormat:@"%@%@", [NSDate date], IMAGE_EXTENSION];
NSString *filePath = [self getScreenShotDirectoryPathWithScreenName:fileName];
NSLog(@"Image File path %@", filePath);
return filePath;
}
#pragma mark Archieve filepath
-(NSString*)getAudioFilePath
{
NSString *fileName = [NSString stringWithFormat:@"%@%@", [NSDate date], TAUDIO_EXTENSION];
NSString *filePath = [self getAudioDirectoryPathWithAudioName:fileName];
NSLog(@"Audio File path %@", filePath);
[self setRecentFilePathForPlaying:filePath];
return filePath;
}
#pragma mark get recently recorded file path
-(NSString*)getRecentlyRecordedAudioFilePath
{
return (self.recentFilePathForPlaying) ? self.recentFilePathForPlaying : nil;
}
#pragma mark clearFilefromDiskPath
-(BOOL)clearFileFromDisk:(NSString*)filePath
{
NSFileManager *fileMgnr = [NSFileManager defaultManager];
if([fileMgnr fileExistsAtPath:filePath])
{
NSError *error = nil;
return ([fileMgnr removeItemAtPath:filePath error:&error]) ? YES : NO;
}
else
{
NSLog(@"File doesn't exist at path: %@", filePath);
return NO;
}
}
#pragma mark (Private methods)
#pragma mark Check file path exist
-(BOOL)checkFileExistAtPath:(NSString*)fileName
{
return ([[NSFileManager defaultManager] fileExistsAtPath:fileName]) ? YES : NO;
}
#pragma mark Returns full filepath from directory name
-(NSMutableArray*)getFilePathArrayFromDirectory:(NSString*)archieveDirectoryPath
{
NSError *err = nil;
NSArray *docContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:archieveDirectoryPath error:&err];
if(!(docContents && docContents.count > 0))
{
NSLog(@"Error: Directory is empty!");
return nil;
}
NSMutableArray *filePathArray = [NSMutableArray arrayWithCapacity:docContents.count];
for (NSString *fileName in docContents)
{
NSString *filePath = [archieveDirectoryPath stringByAppendingPathComponent:fileName];
[filePathArray addObject:filePath];
}
return filePathArray;
}
#pragma mark get file path for screen shots
-(NSArray*)getScreenShots
{
NSString *screenShotDir = [self documentsDirectoryAppendedWithPathComponent:(NSString*)SCREEN_SHOT_DIR];
NSArray *screenShots = [self getFilePathArrayFromDirectory:screenShotDir];
return screenShots;
}
#pragma mark get file path for recorded audio
-(NSArray*)getRecordedAudios
{
NSString *audioDir = [self documentsDirectoryAppendedWithPathComponent:(NSString*)AUDIO_DIR];
NSArray *audios = [self getFilePathArrayFromDirectory:audioDir];
return audios;
}
#pragma mark save screen shot image
-(BOOL)saveImage:(UIImage *)image
{
NSData *imageData = UIImagePNGRepresentation(image);
if(!imageData)
return NO;
return [imageData writeToFile:[[TFileManager sharedFileManager]getScreenShotFilePath] atomically:YES];
}
#pragma mark clear all recorded
-(void)clearAllAudios
{
NSString *audioDir = [self documentsDirectoryAppendedWithPathComponent:(NSString*)AUDIO_DIR];
NSArray *audios = [self getFilePathArrayFromDirectory:audioDir];
for (NSString *filePath in audios)
[self clearFileFromDisk:filePath];
}
#pragma mark clear all screen shots
-(void)clearAllScreenShots
{
NSString *screenShotDir = [self documentsDirectoryAppendedWithPathComponent:(NSString*)SCREEN_SHOT_DIR];
NSArray *screenShots = [self getFilePathArrayFromDirectory:screenShotDir];
for (NSString *filePath in screenShots)
[self clearFileFromDisk:filePath];
}
#pragma mark sort file names recently created first
-(NSMutableArray*)sortFilePathByDate:(NSArray*)unsortedArray{
NSMutableArray *newSortedrray = [NSMutableArray arrayWithArray:unsortedArray];
for (NSInteger i = 0; i < newSortedrray.count - 1; i++)
{
for (NSInteger j = i; j < newSortedrray.count ; j++)
{
NSString *filePath1 = [newSortedrray objectAtIndex:i];
NSString *filePath2 = [newSortedrray objectAtIndex:j];
NSDate* d1 = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath1 error:nil] objectForKey:@"NSFileCreationDate"];
NSDate* d2 = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath2 error:nil] objectForKey:@"NSFileCreationDate"];
NSComparisonResult result = [d1 compare:d2];
if (result == NSOrderedAscending)
[newSortedrray exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
return newSortedrray;
}
#pragma Print the creation date of the file from file name
-(void)createdDatesOfFile:(NSArray*)arr
{
NSInteger i = 0;
for (NSString *filePath in arr)
{
NSDate* d1 = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] objectForKey:@"NSFileCreationDate"];
NSLog(@"%ld - Created Date %@", (long)i++, d1);
}
}
#pragma Roll back recordedx audios
-(void)rollbackTheRecordedAudios
{
NSString *audioDir = [self documentsDirectoryAppendedWithPathComponent:(NSString*)AUDIO_DIR];
NSArray *audios = [self getFilePathArrayFromDirectory:audioDir];
NSMutableArray *sortedArray = [self sortFilePathByDate:audios];
[self createdDatesOfFile:sortedArray];
if(sortedArray.count > kMaxNumberOfAudios)
{
do
{
if([self clearFileFromDisk:[sortedArray lastObject]])
[sortedArray removeLastObject];
} while (sortedArray.count > kMaxNumberOfAudios);
NSLog(@"After roll back");
[self createdDatesOfFile:sortedArray];
}
}
@end