This repository was archived by the owner on Sep 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathSBBDataArchive.m
358 lines (303 loc) · 13.9 KB
/
SBBDataArchive.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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//
// SBBDataArchive.m
// BridgeSDK
//
// Copyright (c) 2015-2017 Sage Bionetworks. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation and/or
// other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder(s) nor the names of any contributors
// may be used to endorse or promote products derived from this software without
// specific prior written permission. No license is granted to the trademarks of
// the copyright holders even if such marks are included in this software.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#import "SBBDataArchive.h"
#import "BridgeSDK+Internal.h"
#import <objc/runtime.h>
#import "ZipZap.h"
#import "SBBEncryptor.h"
static NSString * kFileInfoNameKey = @"filename";
static NSString * kUnencryptedArchiveFilename = @"unencrypted.zip";
static NSString * kFileInfoTimeStampKey = @"timestamp";
static NSString * kFileInfoContentTypeKey = @"contentType";
static NSString * kTaskRunKey = @"taskRun";
static NSString * kFilesKey = @"files";
static NSString * kAppNameKey = @"appName";
static NSString * kAppVersionKey = @"appVersion";
static NSString * kPhoneInfoKey = @"phoneInfo";
static NSString * kDataFilenameKey = @"dataFilename";
static NSString * kFormatKey = @"format";
static NSString * kItemKey = @"item";
static NSString * kJsonPathExtension = @"json";
static NSString * kJsonInfoFilename = @"info.json";
static NSString * kV2GenericFormat = @"v2_generic";
static NSString * kV1LegacyFormat = @"v1_legacy";
@interface SBBDataArchive ()
@property (nonatomic, strong) NSString *reference;
@property (nonatomic, strong) ZZArchive *zipArchive;
@property (nonatomic, strong) NSMutableArray *zipEntries;
@property (nonatomic, strong) NSMutableArray *filesList;
@property (nonatomic, strong) NSMutableDictionary *infoDict;
@property (nonatomic, strong) NSMutableArray *expectedJsonFilenames;
@end
@implementation SBBDataArchive
- (instancetype)init {
@throw [NSException exceptionWithName: NSInternalInconsistencyException
reason: @"method unavailable"
userInfo: nil];
return nil;
}
//designated initializer
- (instancetype)initWithReference:(NSString *)reference
jsonValidationMapping:(nullable NSDictionary <NSString *, NSPredicate *> *)jsonValidationMapping {
self = [super init];
if (self) {
_reference = [reference copy];
_jsonValidationMapping = [jsonValidationMapping copy];
_expectedJsonFilenames = [[jsonValidationMapping allKeys] mutableCopy];
[self commonInit];
}
return self;
}
//create a new zip archive at the reference path
- (void)commonInit
{
NSURL *zipArchiveURL = [NSURL fileURLWithPath:[[self workingDirectoryPath] stringByAppendingPathComponent:kUnencryptedArchiveFilename]];
_unencryptedURL = zipArchiveURL;
_zipEntries = [NSMutableArray array];
_filesList = [NSMutableArray array];
_infoDict = [NSMutableDictionary dictionary];
_dataFilename = @"answers.json";
_usesV1LegacySchema = false;
NSError * error;
_zipArchive = [[ZZArchive alloc] initWithURL:zipArchiveURL
options:@{ZZOpenOptionsCreateIfMissingKey : @YES}
error:&error];
if (!_zipArchive) {
NSAssert(NO, @"Error creating zip archive:\n%@", error);
}
}
//A sandbox in the temporary directory for this archive to be cleaned up on completion.
- (NSString *)workingDirectoryPath
{
NSString *workingDirectoryPath = [NSTemporaryDirectory() stringByAppendingPathComponent:self.reference];
if (![[NSFileManager defaultManager] fileExistsAtPath:workingDirectoryPath]) {
NSError * fileError;
BOOL created = [[NSFileManager defaultManager] createDirectoryAtPath:workingDirectoryPath withIntermediateDirectories:YES attributes:@{ NSFileProtectionKey : NSFileProtectionCompleteUntilFirstUserAuthentication } error:&fileError];
if (!created) {
workingDirectoryPath = nil;
NSAssert(NO, @"Error creating working directory %@:\n%@", workingDirectoryPath, fileError);
}
}
return workingDirectoryPath;
}
- (void)setArchiveInfoObject:(id)object forKey:(NSString*)key {
self.infoDict[key] = [[SBBObjectManager objectManager] bridgeJSONFromObject:object];
}
- (void)insertAnswersDictionary:(NSDictionary *)dictionary
{
[self insertDictionaryIntoArchive:dictionary filename:self.dataFilename createdOn:[NSDate date]];
}
- (void)insertURLIntoArchive:(NSURL*)url fileName:(NSString *)filename
{
#if DEBUG
SBBLog(@"Archiving %@: %@", filename, url);
#endif
NSDictionary *fileAttrs = [[NSFileManager defaultManager] attributesOfItemAtPath:url.path error:nil];
NSDate *createdOn = [NSDate date]; // fallback in case there's a problem getting the file attributes
if (fileAttrs) {
createdOn = [fileAttrs fileCreationDate];
}
NSData *dataToInsert = [NSData dataWithContentsOfURL:url];
[self insertDataIntoArchive:dataToInsert filename:filename createdOn:createdOn];
}
- (void)insertDictionaryIntoArchive:(NSDictionary *)dictionary filename:(NSString *)filename createdOn:(nonnull NSDate *)createdOn
{
#if DEBUG
SBBLog(@"Archiving %@: %@", filename, dictionary);
#endif
NSPredicate *validationPredicate = self.jsonValidationMapping[filename];
if (validationPredicate && ![validationPredicate evaluateWithObject:dictionary]) {
NSError *error = [NSError errorWithDomain:NSStringFromClass([self class]) code:1
userInfo:@{ @"filename": filename,
@"json": dictionary,
@"validationPredicate": validationPredicate}];
NSAssert1(false, @"%@", error);
}
[self.expectedJsonFilenames removeObject:filename];
NSError * serializationError;
NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:&serializationError];
if (jsonData != nil) {
[self insertDataIntoArchive:jsonData filename:filename createdOn:createdOn];
}
else {
NSAssert(NO, @"Error serializing JSON dictionary:\n%@", serializationError);
}
}
- (void)insertDictionaryIntoArchive:(NSDictionary *)dictionary filename:(NSString *)filename
{
[self insertDictionaryIntoArchive:dictionary filename:filename createdOn:[NSDate date]];
}
- (void)insertDataIntoArchive:(NSData *)data filename:(NSString *)filename createdOn:(nonnull NSDate *)createdOn
{
// Check that the file has not already been added
if ([self.filesList filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"%K = %@", kFileInfoNameKey, filename]].count != 0) {
NSAssert1(NO, @"File has already been added: %@", filename);
return;
}
[self.zipEntries addObject: [ZZArchiveEntry archiveEntryWithFileName: filename
compress:YES
dataBlock:^(NSError** error)
{
SBBLog(@"%@", *error);
return data;
}]];
// Check if this is the answers dictionary and set the property if it is.
if ([self.dataFilename isEqualToString:filename]) {
id obj = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
if ([obj isKindOfClass:[NSDictionary class]]) {
_answersDictionary = obj;
}
}
//add the fileInfoEntry
NSString *extension = [filename pathExtension] ? : kJsonPathExtension;
NSDictionary *fileInfoEntry = @{ kFileInfoNameKey: filename,
kFileInfoTimeStampKey: [createdOn ISO8601String],
kFileInfoContentTypeKey: [self contentTypeForFileExtension:extension] };
[self.filesList addObject:fileInfoEntry];
}
- (void)insertDataIntoArchive:(NSData *)data filename:(NSString *)filename
{
[self insertDataIntoArchive:data filename:filename createdOn:[NSDate date]];
}
+ (NSString *)appVersion
{
static NSString *appVersion = nil;
if (!appVersion)
{
NSString *version = [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
NSString *build = NSBundle.mainBundle.appVersion;
appVersion = [NSString stringWithFormat: @"version %@, build %@", version, build];
}
return appVersion;
}
- (BOOL)isEmpty
{
return self.filesList.count == 0;
}
//Compiles the final info.json file and inserts it into the zip archive.
- (BOOL)completeArchive:(NSError **)error
{
// Exit early if this method has already been called.
if (self.isCompleted) {
return YES;
}
BOOL success = YES;
NSError *internalError = nil;
if (!self.isEmpty) {
if (self.expectedJsonFilenames.count > 0) {
NSString *filenames = [self.expectedJsonFilenames componentsJoinedByString:@","];
NSError *validationError = [NSError errorWithDomain:NSStringFromClass([self class]) code:1
userInfo:@{ @"message": [NSString stringWithFormat:@"Missing expected json files: %@", filenames]
}];
NSAssert1(false, @"%@", validationError);
if (error) {
*error = validationError;
return NO;
}
}
[self.infoDict setObject:self.filesList forKey:kFilesKey];
[self.infoDict setObject:self.reference forKey:kItemKey];
[self.infoDict setObject:[[NSBundle mainBundle] appName] forKey:kAppNameKey];
[self.infoDict setObject:[self.class appVersion] forKey:kAppVersionKey];
[self.infoDict setObject:[[UIDevice currentDevice] deviceInfo] forKey:kPhoneInfoKey];
[self.infoDict setObject:self.dataFilename forKey:kDataFilenameKey];
NSString *format = self.usesV1LegacySchema ? kV1LegacyFormat : kV2GenericFormat;
[self.infoDict setObject:format forKey:kFormatKey];
[self insertDictionaryIntoArchive:self.infoDict filename:kJsonInfoFilename createdOn:[NSDate date]];
if (![self.zipArchive updateEntries:self.zipEntries error:&internalError]) {
SBBLog(@"%@", internalError);
success = NO;
if (error) {
*error = internalError;
}
}
}
_completed = success;
return success;
}
- (void)encryptAndUploadArchive
{
// Check that the archive has been closed.
if (!self.isCompleted) {
if (![self completeArchive:nil]) {
return;
}
}
SBBEncryptor *encryptor = [SBBEncryptor new];
[encryptor encryptFileAtURL:_unencryptedURL withCompletion:^(NSURL *url, NSError *error) {
if (!error) {
//remove the archive after encryption
[self removeArchive];
//upload the encrypted archive
[SBBComponent(SBBUploadManager) uploadFileToBridge:url contentType:@"application/zip" completion:^(NSError *error) {
if (!error) {
#if DEBUG
SBBLog(@"SBBDataArchive uploaded file: %@", url.relativePath.lastPathComponent);
#endif
[encryptor removeDirectory];
// TODO: emm 2016-05-18 Fire off a delayed validation check and output the response
} else {
SBBLog(@"SBBDataArchive error returned from SBBUploadManager:\n%@\n%@", error.localizedDescription, error.localizedFailureReason);
}
}];
}
}];
}
+ (void)encryptAndUploadArchives:(NSArray<SBBDataArchive *> *)archives
{
for (SBBDataArchive *archive in archives) {
[archive encryptAndUploadArchive];
}
}
//delete the workingDirectoryPath, and therefore its contents.
-(void)removeArchive
{
NSError *err;
if (![[NSFileManager defaultManager] removeItemAtPath:[self workingDirectoryPath] error:&err]) {
NSAssert(false, @"Error removing unencrypted archive at %@:]n%@", [self workingDirectoryPath], err);
}
}
#pragma mark - Helpers
- (NSString *)contentTypeForFileExtension: (NSString *)extension
{
NSString *contentType;
if ([extension isEqualToString:@"csv"]) {
contentType = @"text/csv";
}else if ([extension isEqualToString:@"m4a"]) {
contentType = @"audio/mp4";
}else {
contentType = @"application/json";
}
return contentType;
}
@end