Files
peko-ios/YuMi/Modules/YMRoom/View/AnimationView/XPRoomGiftAnimationParser.m
2023-09-21 17:44:59 +08:00

94 lines
3.7 KiB
Objective-C

//
// YMRoomGiftAnimationParser.m
// YUMI
//
// Created by YUMI on 2022/3/17.
//
#import "XPRoomGiftAnimationParser.h"
#import <zlib.h>
#import <SSZipArchive/SSZipArchive.h>
#import <CommonCrypto/CommonDigest.h>
#import <AFNetworking.h>
#import "GCDHelper.h"
@implementation XPRoomGiftAnimationParser
- (void)parseWithURL:(nonnull NSString *)URL
completionBlock:(void ( ^ _Nonnull )(NSString * _Nullable videoUrl))completionBlock
failureBlock:(void ( ^ _Nullable)(NSError * _Nullable error))failureBlock {
[self parseWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:URL] cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:20.0]
completionBlock:completionBlock
failureBlock:failureBlock];
}
- (void)parseWithURLRequest:(NSURLRequest *)URLRequest completionBlock:(void (^)(NSString * _Nullable))completionBlock failureBlock:(void (^)(NSError * _Nullable))failureBlock {
if (URLRequest.URL == nil) {
if (failureBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
failureBlock([NSError errorWithDomain:@"vapParser" code:411 userInfo:@{NSLocalizedDescriptionKey: @"URL cannot be nil."}]);
}];
}
return;
}
if ([[NSFileManager defaultManager] fileExistsAtPath:[self cacheDirectory:[self cacheKey:URLRequest.URL]]]) {
if (completionBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionBlock([self cacheDirectory:[self cacheKey:URLRequest.URL]]);
}];
}
return;
}
[[[AFHTTPSessionManager manager] downloadTaskWithRequest:URLRequest progress:^(NSProgress * _Nonnull downloadProgress) {
} destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
return [NSURL fileURLWithPath:[self cacheDirectory:[self cacheKey:URLRequest.URL]]];
} completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
if (!error) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
NSString *str;
if ([filePath.absoluteString hasPrefix:@"file:///"]) {
str = [filePath.absoluteString stringByReplacingOccurrencesOfString:@"file://" withString:@""];
} else {
str = filePath.absoluteString;
}
completionBlock(str);
}];
} else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionBlock(nil);
}];
}
}] resume];
}
- (void)clearCache:(nonnull NSString *)cacheKey {
NSString *cacheDir = [self cacheDirectory:cacheKey];
[[NSFileManager defaultManager] removeItemAtPath:cacheDir error:NULL];
}
- (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:@"/%@.mp4", 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