-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFBCachedFile.m
More file actions
118 lines (95 loc) · 2.26 KB
/
Copy pathFBCachedFile.m
File metadata and controls
118 lines (95 loc) · 2.26 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
//
// FBCachedFile.m
// FBFileCache
//
// Created by Hiroshi Hashiguchi on 11/03/02.
// Copyright 2011 Five-technology Co.,Ltd.. All rights reserved.
//
#import "FBCachedFile.h"
@interface FBCachedFile()
@property (nonatomic, retain) NSURL* URL;
@end
@implementation FBCachedFile
@synthesize URL = url_;
#pragma mark -
#pragma mark Initialization and deallocation
- (id)initWithFile:(NSString*)filePath
{
self = [super init];
if (self) {
self.URL = [NSURL fileURLWithPath:filePath];
}
return self;
}
- (void)dealloc {
self.URL = nil;
[super dealloc];
}
#pragma mark -
#pragma mark API
- (void)updateAccessTime
{
FILE* fp = fopen([self.path UTF8String], "r");
fgetc(fp);
fclose(fp);
}
+ (FBCachedFile*)cachedFile:(NSString*)filePath
{
NSFileManager* fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
return [[[self alloc] initWithFile:filePath] autorelease];
} else {
return nil;
}
}
#pragma mark -
#pragma mark Private
- (NSDictionary*)_attributes
{
NSError* error = nil;
NSDictionary* attributes = [[NSFileManager defaultManager]
attributesOfItemAtPath:self.path error:&error];
if (error) {
NSLog(@"%s|[ERROR] %@", __PRETTY_FUNCTION__, error);
return nil;
}
return attributes;
}
#pragma mark -
#pragma mark Properties
- (NSString*)path
{
return [self.URL path];
}
- (NSURL*)URL
{
return url_;
}
- (NSDate*)creationDate
{
return [[self _attributes] objectForKey:NSFileCreationDate];
}
- (NSTimeInterval)timeIntervalSinceNow
{
return [self.creationDate timeIntervalSinceNow];
}
- (NSData*)data
{
return [NSData dataWithContentsOfURL:self.URL];
}
- (NSDate*)lastModifiedDate
{
return [[self _attributes] objectForKey:NSFileModificationDate];
}
- (void)setLastModifiedDate:(NSDate*)date
{
NSDictionary* attributes =
[NSDictionary dictionaryWithObject:date forKey:NSFileModificationDate];
NSFileManager* fileManager = [NSFileManager defaultManager];
NSError* error = nil;
if (![fileManager setAttributes:attributes
ofItemAtPath:self.path error:&error]) {
NSLog(@"%s|[ERROR] %@", __PRETTY_FUNCTION__, error);
}
}
@end