This repository has been archived by the owner on Dec 18, 2022. It is now read-only.
/
SKBlob.m
318 lines (253 loc) · 12.1 KB
/
SKBlob.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
//
// SKBlob.m
// SnapchatKit
//
// Created by Tanner Bennett on 6/13/15.
// Copyright (c) 2015 Tanner Bennett. All rights reserved.
//
#import "SKBlob.h"
#import "SnapchatKit-Constants.h"
#import "NSData+SnapchatKit.h"
#import "NSString+SnapchatKit.h"
#import "SKStory.h"
#import "SKRequest.h"
#import "SSZipArchive.h"
@import AVFoundation;
@implementation SKBlob
@synthesize zipData = _zipData;
+ (instancetype)blobWithContentsOfPath:(NSString *)path {
return [[self alloc] initWithContentsOfPath:path];
}
+ (instancetype)blobWithData:(NSData *)data {
return [[self alloc] initWithData:data];
}
+ (void)blobWithStoryData:(NSData *)blobData forStory:(SKStory *)story isThumb:(BOOL)thumb completion:(ResponseBlock)completion {
NSParameterAssert(blobData); NSParameterAssert(story); NSParameterAssert(completion);
[self decryptBlob:blobData forStory:story isThumb:thumb completion:completion];
}
+ (void)decompressBlob:(NSData *)blobData forStory:(SKStory *)story completion:(ResponseBlock)completion {
NSParameterAssert(blobData); NSParameterAssert(story); NSParameterAssert(completion);
NSString *path = [SKTempDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"sk-zip~%@.tmp", story.identifier]];
NSString *unzip = [SKTempDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"sk~%@.tmp", story.identifier]];
[blobData writeToFile:path atomically:YES];
[SSZipArchive unzipFileAtPath:path toDestination:unzip completion:^(NSString *path, BOOL succeeded, NSError *error) {
if (succeeded) {
completion([SKBlob blobWithContentsOfPath:unzip], nil);
} else {
completion(nil, error);
}
NSError *e = nil;
[[NSFileManager defaultManager] removeItemAtPath:path error:&e];
if (e && kVerboseLog) SKLog(@"Error deleting blob: %@", e); e = nil;
[[NSFileManager defaultManager] removeItemAtPath:unzip error:&e];
if (e && kVerboseLog) SKLog(@"Error deleting blob: %@", e); e = nil;
}];
}
+ (void)decryptBlob:(NSData *)blobData forStory:(SKStory *)story isThumb:(BOOL)thumb completion:(ResponseBlock)completion {
NSParameterAssert(blobData); NSParameterAssert(story); NSParameterAssert(completion);
if ((!blobData.isCompressed && !blobData.isMedia)) {
if (thumb)
blobData = [blobData decryptStoryWithKey:story.mediaKey iv:story.thumbIV];
else
blobData = [blobData decryptStoryWithKey:story.mediaKey iv:story.mediaIV];
}
if (blobData.isCompressed) {
[self decompressBlob:blobData forStory:story completion:completion];
} else {
SKBlob *blob = [SKBlob blobWithData:blobData];
if (blob)
completion(blob, blobData.isMedia ? nil : [SKRequest errorWithMessage:@"Unknown blob format" code:1]);
else
completion(nil, [SKRequest errorWithMessage:@"Error initializing blob with data" code:1]);
}
}
- (id)initWithData:(NSData *)data {
NSParameterAssert(data.length);
self = [super init];
if (self) {
_data = data;
_isImage = _data.isJPEG || _data.isPNG;
_isVideo = _data.isMPEG4;
}
return self;
}
- (id)initWithContentsOfPath:(NSString *)path {
NSParameterAssert(path);
self = [super init];
if (self) {
BOOL isFolder = NO;
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isFolder];
if (exists) {
// Video
if (isFolder) {
NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil];
if (!files.count)
return nil;
// Single file?
if (files.count == 1) {
_data = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:files[0]]];
if (kVerboseLog)
SKLog(@"Single file in zip: %@", files[0]);
}
// Load video blob and image blob
else if ([files[0] containsString:@"media~"]) {
_data = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:files[0]]];
_overlay = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:files[1]]];
} else if ([files[1] containsString:@"media~"]) {
_data = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:files[1]]];
_overlay = [[NSData alloc] initWithContentsOfFile:[path stringByAppendingPathComponent:files[0]]];
}
// Errors
if (!_data)
return nil;
if (!_overlay && kVerboseLog)
SKLog(@"Single file in zip: %@", files);
// Image
} else {
_data = [[NSData alloc] initWithContentsOfFile:path];
if (!_data)
return nil;
}
// Delete file(s)
// NSError *error = nil;
// [[NSFileManager defaultManager] removeItemAtPath:path error:&error];
// if (error && kVerboseLog)
// SKLog(@"Error deleting blob: %@", error);
} else {
return nil;
}
_isImage = _data.isJPEG || _data.isPNG;
_isVideo = _data.isMPEG4;
}
return self;
}
- (BOOL)isEqual:(id)object {
if ([object isKindOfClass:[SKBlob class]])
return [self isEqualToBlob:object];
return [super isEqual:object];
}
- (BOOL)isEqualToBlob:(SKBlob *)blob {
return [self.data isEqualToData:blob.data];
}
- (NSString *)description {
return [NSString stringWithFormat:@"<%@ isImage=%d, has overlay=%d, bytes=%lu>",
NSStringFromClass(self.class), self.isImage, (BOOL)self.overlay, (unsigned long)self.data.length];
}
- (NSArray *)writeToPath:(NSString *)path filename:(NSString *)filename atomically:(BOOL)atomically {
NSParameterAssert(path);
if (!self.overlay) {
path = [path stringByAppendingPathComponent:[filename stringByAppendingString:self.data.appropriateFileExtension]];
[self.data writeToFile:path atomically:atomically];
return @[path];
} else {
path = [path stringByAppendingPathComponent:filename];
NSString *overlay = [path stringByAppendingPathComponent:[filename stringByAppendingString:[@"-overlay" stringByAppendingString:_overlay.appropriateFileExtension]]];
NSString *video = [path stringByAppendingPathComponent:[filename stringByAppendingString:_data.appropriateFileExtension]];
[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
[self.data writeToFile:video atomically:atomically];
[self.overlay writeToFile:overlay atomically:atomically];
return @[video, overlay];
}
}
- (void)decompress:(ResponseBlock)completion {
if (self.data.isCompressed) {
NSString *uuid = [NSUUID UUID].UUIDString;
NSString *path = [SKTempDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"sk-zip~%@.tmp", uuid]];
NSString *unzip = [SKTempDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"sk~%@.tmp", uuid]];
[self.data writeToFile:path atomically:YES];
[SSZipArchive unzipFileAtPath:path toDestination:unzip completion:^(NSString *path, BOOL succeeded, NSError *error) {
if (succeeded) {
SKBlob *blob = [SKBlob blobWithContentsOfPath:unzip];
if (blob) {
completion(blob, nil);
} else {
completion(nil, [SKRequest errorWithMessage:@"Error initializing blob" code:2]);
}
} else {
SKLog(@"%@", error);
}
}];
} else {
completion(self, nil);
}
}
- (NSData *)zipData {
if (_zipData) return _zipData;
if (self.overlay) {
NSString *tmpDir = SKUniqueIdentifier();
NSString *videoName = [@"media~zip-" stringByAppendingString:SKUniqueIdentifier().capitalizedString];
NSString *overlayName = [@"overlay~zip-" stringByAppendingString:SKUniqueIdentifier().capitalizedString];
NSString *folder = [SKTempDirectory() stringByAppendingPathComponent:tmpDir];
NSString *archive = [SKTempDirectory() stringByAppendingPathComponent:[tmpDir stringByAppendingString:@".zip"]];
NSString *video = [folder stringByAppendingPathComponent:videoName];
NSString *overlay = [folder stringByAppendingPathComponent:overlayName];
[[NSFileManager defaultManager] createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:nil];
[self.data writeToFile:video atomically:YES];
[self.overlay writeToFile:overlay atomically:YES];
BOOL success = [SSZipArchive createZipFileAtPath:archive withContentsOfDirectory:folder];
if (success) {
_zipData = [NSData dataWithContentsOfFile:archive];
// zip failed
if (_zipData.length == 22) _zipData = nil;
}
// Cleanup
[[NSFileManager defaultManager] removeItemAtPath:archive error:nil];
[[NSFileManager defaultManager] removeItemAtPath:folder error:nil];
}
return _zipData;
}
- (NSData *)videoThumbnail {
if (_videoThumbnail) return _videoThumbnail;
if (self.isVideo) {
NSString *path = [SKTempDirectory() stringByAppendingPathComponent:@"tmp-video.mp4"];
[self.data writeToFile:path atomically:YES];
AVURLAsset *asset = [AVURLAsset assetWithURL:[NSURL fileURLWithPath:path]];
AVAssetImageGenerator *gen = [AVAssetImageGenerator assetImageGeneratorWithAsset:asset];
CGImageRef image = [gen copyCGImageAtTime:CMTimeMake(0, 600) actualTime:NULL error:NULL];
if (image != NULL) {
_videoThumbnail = SKThumbnailFromGCImage(image);
CGImageRelease(image);
}
// Cleanup
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
}
return _videoThumbnail;
}
@end
NSData * SKThumbnailFromGCImage(CGImageRef image) {
CGFloat width = CGImageGetWidth(image);
CGFloat height = CGImageGetHeight(image);
CGFloat s = MIN(MIN(width, height), 102);
// Images too small to scale
if (s < 102) {
CGFloat cx = s/2.f - 51;
CGFloat cy = s/2.f - 51;
CGImageRef cropped = CGImageCreateWithImageInRect(image, CGRectMake(cx, cy, s, s));
return CFBridgingRelease(CGDataProviderCopyData(CGImageGetDataProvider(cropped)));
}
CGFloat scale = MIN(width, height) / 102.f;
NSInteger bitsPerComponent = CGImageGetBitsPerComponent(image);
NSInteger bytesPerRow = CGImageGetBytesPerRow(image);
CGColorSpaceRef colorSpace = CGImageGetColorSpace(image);
CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(image);
width /= scale; height /= scale;
CGFloat cx = (width - s) / 2.f;
CGFloat cy = (height - s) / 2.f;
CGContextRef context = CGBitmapContextCreate(nil, width, height, bitsPerComponent, bytesPerRow, colorSpace, bitmapInfo);
CGContextSetInterpolationQuality(context, kCGInterpolationHigh);
CGContextDrawImage(context, CGRectMake(0, 0, width, height), image);
CGImageRef scaled = CGBitmapContextCreateImage(context);
CGImageRef cropped = CGImageCreateWithImageInRect(scaled, CGRectMake(cx, cy, s, s));
// Cleanup
CGImageRelease(scaled);
NSData *ret;
#if __has_include(<UIKit/UIImage.h>)
ret = UIImagePNGRepresentation([UIImage imageWithCGImage:cropped]);
#elif __has_include(<AppKit/NSImage.h>)
ret = [[[NSBitmapImageRep alloc] initWithCGImage:cropped] representationUsingType:NSPNGFileType properties:@{}];
#else
#warning Target platform is missing a way to convert a CGImage to NSData.
#endif
CGImageRelease(cropped);
return ret;
}