-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxolawareCache.m
More file actions
72 lines (60 loc) · 1.98 KB
/
xolawareCache.m
File metadata and controls
72 lines (60 loc) · 1.98 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
//
// xolawareCache.m
// theGRID
//
// Created by xolaware on 2012.08.02.
#include "xolawareOpenSourceCopyright.h" // Copyright (c) 2012 xolaware.
#import "xolawareCache.h"
@implementation xolawareCache
static NSURL* _htmlCacheDirURL;
+ (NSURL*)htmlCacheDirURL
{
if (!_htmlCacheDirURL)
_htmlCacheDirURL
= [[NSFileManager.defaultManager URLsForDirectory:NSCachesDirectory
inDomains:NSUserDomainMask] lastObject];
return _htmlCacheDirURL;
}
+ (void)clearFilesInCacheDir:(NSString*)cacheDir {
__weak NSFileManager* fileMgr = NSFileManager.defaultManager;
NSString* cacheDirPath
= [xolawareCache.htmlCacheDirURL.path stringByAppendingPathComponent:cacheDir];
NSDirectoryEnumerator* dirIter = [fileMgr enumeratorAtPath:cacheDirPath];
NSError* err = nil;
NSString* file;
while (file = [dirIter nextObject])
{
NSString* fileToDelete = [cacheDirPath stringByAppendingPathComponent:file];
NSLog(@"deleting file %@", fileToDelete);
if (![fileMgr removeItemAtPath:fileToDelete error:&err] && err)
NSLog(@"oops: %@", err);
}
}
- (id)initWithCacheDir:(NSString*)cacheDir {
NSString* cacheDirPath
= [xolawareCache.htmlCacheDirURL.path stringByAppendingPathComponent:cacheDir];
self = [super initFileURLWithPath:cacheDirPath isDirectory:YES];
if (self)
{
NSFileManager* fileMgr = NSFileManager.defaultManager;
NSError* error;
[fileMgr createDirectoryAtURL:self withIntermediateDirectories:YES attributes:nil
error:&error];
#if DEBUG
if (error) NSLog(@"%@", error);
#endif
}
return self;
}
- (BOOL)createFile:(NSString*)file contents:(NSData*)contents {
return [contents writeToURL:[self urlForCacheFile:file] atomically:YES];
}
- (BOOL)isFileInCache:(NSString*)file {
NSFileManager* fileMgr = NSFileManager.defaultManager;
NSString* filePath = [self.path stringByAppendingPathComponent:file];
return [fileMgr fileExistsAtPath:filePath isDirectory:NO];
}
- (NSURL*)urlForCacheFile:(NSString*)file {
return [NSURL URLWithString:file relativeToURL:self];
}
@end