This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCOSQLiteStore+Attachments.m
143 lines (120 loc) · 3.59 KB
/
COSQLiteStore+Attachments.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
#import "COSQLiteStore.h"
#import "COSQLiteStore+Attachments.h"
#include <openssl/sha.h>
@implementation COSQLiteStore (Attachments)
- (NSURL *) attachmentsURL
{
return [url_ URLByAppendingPathComponent: @"attachments" isDirectory: YES];
}
static NSData *hashItemAtURL(NSURL *aURL)
{
SHA_CTX shactx;
if (1 != SHA1_Init(&shactx))
{
return nil;
}
NSFileHandle *fh = [NSFileHandle fileHandleForReadingFromURL: aURL
error: NULL];
int fd = [fh fileDescriptor];
unsigned char buf[4096];
while (1)
{
ssize_t bytesread = read(fd, buf, sizeof(buf));
if (bytesread == 0)
{
[fh closeFile];
break;
}
if (bytesread < 0)
{
[fh closeFile];
return nil;
}
if (1 != SHA1_Update(&shactx, buf, bytesread))
{
[fh closeFile];
return nil;
}
}
unsigned char digest[SHA_DIGEST_LENGTH];
if (1 != SHA1_Final(digest, &shactx))
{
return nil;
}
return [NSData dataWithBytes: digest length: SHA_DIGEST_LENGTH];
}
static NSString *hexString(NSData *aData)
{
const NSUInteger len = [aData length];
if (0 == len)
{
return @"";
}
const unsigned char *bytes = (const unsigned char *)[aData bytes];
NSMutableString *result = [NSMutableString stringWithCapacity: len * 2];
for (NSUInteger i = 0; i < len; i++)
{
[result appendFormat:@"%02x", (int)bytes[i]];
}
return result;
}
static NSData *dataFromHexString(NSString *hexString)
{
NSMutableData *result = [NSMutableData dataWithCapacity: [hexString length] / 2];
const char *cstring = [hexString UTF8String];
unsigned byteAsInt;
while (1 == sscanf(cstring, "%2x", &byteAsInt))
{
unsigned char byte = byteAsInt;
[result appendBytes: &byte length: 1];
cstring += 2;
}
return result;
}
- (NSURL *) URLForAttachmentID: (NSData *)aHash
{
NSParameterAssert([aHash length] == SHA_DIGEST_LENGTH);
return [[[self attachmentsURL] URLByAppendingPathComponent: hexString(aHash)]
URLByAppendingPathExtension: @"attachment"];
}
- (NSData *) importAttachmentFromURL: (NSURL *)aURL
{
NSFileManager *fm = [NSFileManager defaultManager];
if (![fm createDirectoryAtURL: [self attachmentsURL]
withIntermediateDirectories: NO
attributes: nil
error: NULL])
{
return nil;
}
// Hash it
NSData *hash = hashItemAtURL(aURL);
NSURL *attachmentURL = [self URLForAttachmentID: hash];
if (![fm fileExistsAtPath: [attachmentURL path]])
{
if (NO == [fm copyItemAtURL: aURL toURL: attachmentURL error: NULL])
{
return nil;
}
}
return hash;
}
- (NSArray *) attachments
{
NSMutableArray *result = [NSMutableArray array];
NSArray *files = [[NSFileManager defaultManager] directoryContentsAtPath: [[self attachmentsURL] path]];
for (NSString *file in files)
{
NSString *attachmentHexString = [file stringByDeletingPathExtension];
NSData *hash = dataFromHexString(attachmentHexString);
[result addObject: hash];
}
return result;
}
- (BOOL) deleteAttachment: (NSData *)hash
{
NSParameterAssert([hash length] == SHA_DIGEST_LENGTH);
return [[NSFileManager defaultManager] removeItemAtPath: [[self URLForAttachmentID: hash] path]
error: NULL];
}
@end