forked from svga/SVGAPlayer-iOS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSVGAParser.m
More file actions
179 lines (167 loc) · 7.39 KB
/
SVGAParser.m
File metadata and controls
179 lines (167 loc) · 7.39 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
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
//
// SVGAParser.m
// SVGAPlayer
//
// Created by 崔明辉 on 16/6/17.
// Copyright © 2016年 UED Center. All rights reserved.
//
#import "SVGAParser.h"
#import "SVGAVideoEntity.h"
#import <SSZipArchive/SSZipArchive.h>
#import <CommonCrypto/CommonDigest.h>
@interface SVGAParser ()
@end
@implementation SVGAParser
- (void)parseWithURL:(nonnull NSURL *)URL
completionBlock:(void ( ^ _Nonnull )(SVGAVideoEntity * _Nullable videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nullable error))failureBlock {
if ([[NSFileManager defaultManager] fileExistsAtPath:[self cacheDirectory:[self cacheKey:URL]]]) {
[self parseWithCacheKey:[self cacheKey:URL] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
if (completionBlock) {
completionBlock(videoItem);
}
} failureBlock:^(NSError * _Nonnull error) {
[self clearCache:[self cacheKey:URL]];
if (failureBlock) {
failureBlock(error);
}
}];
return;
}
[[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil && data != nil) {
[self parseWithData:data cacheKey:[self cacheKey:URL] completionBlock:^(SVGAVideoEntity * _Nonnull videoItem) {
if (completionBlock) {
completionBlock(videoItem);
}
} failureBlock:^(NSError * _Nonnull error) {
[self clearCache:[self cacheKey:URL]];
if (failureBlock) {
failureBlock(error);
}
}];
}
else {
if (failureBlock) {
failureBlock(error);
}
}
}] resume];
}
- (void)parseWithCacheKey:(nonnull NSString *)cacheKey
completionBlock:(void ( ^ _Nullable)(SVGAVideoEntity * _Nonnull videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nonnull error))failureBlock {
[[NSOperationQueue new] addOperationWithBlock:^{
SVGAVideoEntity *cacheItem = [SVGAVideoEntity readCache:cacheKey];
if (cacheItem != nil) {
if (completionBlock) {
completionBlock(cacheItem);
}
return;
}
NSString *cacheDir = [self cacheDirectory:cacheKey];
NSError *err;
NSData *JSONData = [NSData dataWithContentsOfFile:[cacheDir stringByAppendingString:@"/movie.spec"]];
if (JSONData != nil) {
NSDictionary *JSONObject = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:&err];
if ([JSONObject isKindOfClass:[NSDictionary class]]) {
SVGAVideoEntity *videoItem = [[SVGAVideoEntity alloc] initWithJSONObject:JSONObject cacheDir:cacheDir];
[videoItem resetImagesWithJSONObject:JSONObject];
[videoItem resetSpritesWithJSONObject:JSONObject];
[videoItem saveCache:cacheKey];
if (completionBlock) {
completionBlock(videoItem);
}
}
}
else {
if (failureBlock) {
failureBlock([NSError errorWithDomain:NSFilePathErrorKey code:-1 userInfo:nil]);
}
}
}];
}
- (void)clearCache:(nonnull NSString *)cacheKey {
NSString *cacheDir = [self cacheDirectory:cacheKey];
[[NSFileManager defaultManager] removeItemAtPath:cacheDir error:NULL];
}
- (void)parseWithData:(nonnull NSData *)data
cacheKey:(nonnull NSString *)cacheKey
completionBlock:(void ( ^ _Nullable)(SVGAVideoEntity * _Nonnull videoItem))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nonnull error))failureBlock {
[[NSOperationQueue new] addOperationWithBlock:^{
SVGAVideoEntity *cacheItem = [SVGAVideoEntity readCache:cacheKey];
if (cacheItem != nil) {
if (completionBlock) {
completionBlock(cacheItem);
}
return;
}
NSString *tmpPath = [NSTemporaryDirectory() stringByAppendingFormat:@"%u.svga", arc4random()];
if (data != nil) {
[data writeToFile:tmpPath atomically:YES];
NSString *cacheDir = [self cacheDirectory:cacheKey];
if ([cacheDir isKindOfClass:[NSString class]]) {
[[NSFileManager defaultManager] createDirectoryAtPath:cacheDir withIntermediateDirectories:NO attributes:nil error:nil];
[SSZipArchive unzipFileAtPath:tmpPath toDestination:[self cacheDirectory:cacheKey] progressHandler:nil completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
if (error != nil) {
if (failureBlock) {
failureBlock(error);
}
}
else {
NSError *err;
NSData *JSONData = [NSData dataWithContentsOfFile:[cacheDir stringByAppendingString:@"/movie.spec"]];
if (JSONData != nil) {
NSDictionary *JSONObject = [NSJSONSerialization JSONObjectWithData:JSONData options:kNilOptions error:&err];
if ([JSONObject isKindOfClass:[NSDictionary class]]) {
SVGAVideoEntity *videoItem = [[SVGAVideoEntity alloc] initWithJSONObject:JSONObject cacheDir:cacheDir];
[videoItem resetImagesWithJSONObject:JSONObject];
[videoItem resetSpritesWithJSONObject:JSONObject];
[videoItem saveCache:cacheKey];
if (completionBlock) {
completionBlock(videoItem);
}
}
}
else {
if (failureBlock) {
failureBlock([NSError errorWithDomain:NSFilePathErrorKey code:-1 userInfo:nil]);
}
}
}
}];
}
else {
if (failureBlock) {
failureBlock([NSError errorWithDomain:NSFilePathErrorKey code:-1 userInfo:nil]);
}
}
}
else {
if (failureBlock) {
failureBlock([NSError errorWithDomain:@"Data Error" code:-1 userInfo:nil]);
}
}
}];
}
- (nonnull NSString *)cacheKey:(NSURL *)URL {
return [self MD5String:URL.absoluteString];
}
- (nullable NSString *)cacheDirectory:(NSString *)cacheKey {
NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
return [cacheDir stringByAppendingFormat:@"/%@", cacheKey];
}
- (NSString *)MD5String:(NSString *)str {
const char *cstr = [str UTF8String];
unsigned char result[16];
CC_MD5(cstr, (CC_LONG)strlen(cstr), result);
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
}
@end