Files
yuyin_ios/SweetParty/常用工具/svga缓存类/SVGAParser+AOESVGAMemory.m

323 lines
13 KiB
Mathematica
Raw Normal View History

2025-08-08 11:05:33 +08:00
//
// SVGAParser+AOESVGAMemory.m
// AOESVGASimple
//
// Created by on 2018/3/9.
// Copyright © 2018 . All rights reserved.
//
#import "SVGAParser+AOESVGAMemory.h"
#import <CommonCrypto/CommonDigest.h>
@implementation SVGAParser (AOESVGAMemory)
-(void)parseMemoryWithURL:(NSURL *)URL Version:(NSString * _Nullable)version completionBlock:(void (^ _Nonnull)(SVGAVideoEntity * _Nullable))completionBlock failureBlock:(void (^ _Nullable)(NSError * _Nullable))failureBlock{
NSFileManager * fileMgr = [NSFileManager defaultManager];
NSString *dir = [self memorySVGADir:URL];
NSString * svgaFilePath = [dir stringByAppendingString:[NSString stringWithFormat:@"/%@.svga",[self MD5StringExt:URL.absoluteString]]];
NSString * versionFilePath = [dir stringByAppendingString:@"/version.text"];
if ([fileMgr fileExistsAtPath:svgaFilePath]) { //
//version version
if ([fileMgr fileExistsAtPath:versionFilePath]) {
NSString * versionStr = [NSString stringWithContentsOfFile:versionFilePath encoding:NSUTF8StringEncoding error:nil];
if ([versionStr isEqualToString:version]) {
//
NSURL * locationUrl = [NSURL fileURLWithPath:svgaFilePath];
[self parseWithURL:locationUrl completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
if (completionBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionBlock(videoItem);
}];
}
} failureBlock:^(NSError * _Nullable error) {
if (failureBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
failureBlock(error);
}];
}
}];
}else{ //
//
//version svgaversion
[[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil && data != nil) {
[self parseWithURL:URL completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
if (completionBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionBlock(videoItem);
}];
}
} failureBlock:^(NSError * _Nullable error) {
if (failureBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
failureBlock(error);
}];
}
}];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//svga
BOOL svgaWrite = [data writeToFile:svgaFilePath atomically:YES];
if (svgaWrite == NO) {
[fileMgr removeItemAtPath:svgaFilePath error:nil];
}
//
NSString * svga_version = (version && ![version isEqualToString:@""]) ? version : @"0";
BOOL versionWrite = [svga_version writeToFile:versionFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (versionWrite == NO) {
[fileMgr removeItemAtPath:versionFilePath error:nil];
}
});
}else {
if (failureBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
failureBlock(error);
}];
}
}
}] resume];
}
}else{
//version svgaversion
[[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil && data != nil) {
[self parseWithURL:URL completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
if (completionBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionBlock(videoItem);
}];
}
} failureBlock:^(NSError * _Nullable error) {
if (failureBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
failureBlock(error);
}];
}
}];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//svga
BOOL svgaWrite = [data writeToFile:svgaFilePath atomically:YES];
if (svgaWrite == NO) {
[fileMgr removeItemAtPath:svgaFilePath error:nil];
}
//
NSString * svga_version = (version && ![version isEqualToString:@""]) ? version : @"0";
//Version,
if (![fileMgr fileExistsAtPath:versionFilePath]) {
[fileMgr createFileAtPath:versionFilePath contents:nil attributes:nil];
}
BOOL versionWrite = [svga_version writeToFile:versionFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (versionWrite == NO) {
[fileMgr removeItemAtPath:versionFilePath error:nil];
}
});
}else {
if (failureBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
failureBlock(error);
}];
}
}
}] resume];
}
return;
}
//svga svga
//svga
[[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (error == nil && data != nil) {
[self parseWithURL:URL completionBlock:^(SVGAVideoEntity * _Nullable videoItem) {
if (completionBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
completionBlock(videoItem);
}];
}
} failureBlock:^(NSError * _Nullable error) {
if (failureBlock) {
failureBlock(error);
}
}];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
//svga
[fileMgr createFileAtPath:svgaFilePath contents:data attributes:nil];
//
NSString * svga_version = (version && ![version isEqualToString:@""]) ? version : @"0";
//Version,
if (![fileMgr fileExistsAtPath:versionFilePath]) {
[fileMgr createFileAtPath:versionFilePath contents:nil attributes:nil];
}
BOOL versionWrite = [svga_version writeToFile:versionFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (versionWrite == NO) {
[fileMgr removeItemAtPath:versionFilePath error:nil];
}
});
}else {
if (failureBlock) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
failureBlock(error);
}];
}
}
}] resume];
}
+(void)memoryAllSzie:(void (^)(NSUInteger))sucBlock{
//
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *memoryDirPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:[NSString stringWithFormat:@"/%@",AOESVGAMemoryDir]];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dir = NO;
[fileManager fileExistsAtPath:memoryDirPath isDirectory:&dir];
NSUInteger totalByteSize = 0;
if (dir) { //SVGA
NSDirectoryEnumerator *enumerator = [fileManager enumeratorAtPath:memoryDirPath];
for (NSString *subpath in enumerator) {
//
NSString *fullSubpath = [memoryDirPath stringByAppendingPathComponent:subpath];
//
totalByteSize += [fileManager attributesOfItemAtPath:fullSubpath error:nil].fileSize;
}
}
dispatch_async(dispatch_get_main_queue(), ^{
if (sucBlock) {
sucBlock(totalByteSize);
}
});
});
}
+(void)removeAllMemorySvgas:(void (^)(BOOL))sucBlock{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *memoryDirPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:[NSString stringWithFormat:@"/%@",AOESVGAMemoryDir]];
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL dir = NO;
[fileManager fileExistsAtPath:memoryDirPath isDirectory:&dir];
BOOL isSuccess = NO;
if (dir) { //
isSuccess = [fileManager removeItemAtPath:memoryDirPath error:nil];
}else{
isSuccess = YES;
}
dispatch_async(dispatch_get_main_queue(), ^{
if (sucBlock) {
sucBlock(isSuccess);
}
});
});
}
#pragma mark - Private
//SVGA
-(nullable NSString *)memorySVGADir:(NSURL *)url{
NSFileManager * fileMgr = [NSFileManager defaultManager];
NSString *memoryDir = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingString:[NSString stringWithFormat:@"/%@/%@",AOESVGAMemoryDir,[self MD5StringExt:url.absoluteString]]];
BOOL dir = NO;
[fileMgr fileExistsAtPath:memoryDir isDirectory:&dir];
if (dir == NO) { //
[fileMgr createDirectoryAtPath:memoryDir withIntermediateDirectories:YES attributes:nil error:nil];
}
return memoryDir;
}
- (NSString *)MD5StringExt:(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