Created
March 18, 2016 07:36
-
-
Save anonymous/69ee2c00d3da05565fdc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)AsynchronousDownloadSmallThumbnails:(NSString*)PID | |
{ | |
@try { | |
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; | |
self.manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration]; | |
[[self.manager operationQueue] setMaxConcurrentOperationCount:1]; | |
configuration.HTTPAdditionalHeaders = @{@"Accept" : @"application/json", | |
@"Content-Type" : @"application/json", | |
}; | |
NSDictionary *params = @{@"pId": [NSString stringWithFormat:@"%@",PID]}; | |
NSLog(@"Down parms : %@",params); | |
NSError *e; | |
NSMutableURLRequest * request1 = [[AFJSONRequestSerializer serializer] requestWithMethod:@"POST" URLString:[SPC_DOWNLOAD_SMALL_THUMB_SERVICE_CALL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:params error:&e]; | |
[request1 setValue:[NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] valueForKey:ACCESS_TOKEN]] forHTTPHeaderField:@"Authorization"]; | |
//[request1 setValue:[NSString stringWithFormat:@"Bearer %@",[[NSUserDefaults standardUserDefaults] valueForKey:ACCESS_TOKEN]] forKey:@"Authorization"]; | |
NSProgress *process; | |
NSURLSessionDownloadTask *downloadTask = [self.manager downloadTaskWithRequest:request1 progress:&process destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) { | |
NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil]; | |
return [documentsDirectoryURL URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",PID]]; | |
//[response suggestedFilename] //[NSString stringWithFormat:@"%@.pdf",self.CatalogName] ======[NSString stringWithFormat:@"%@.zip",ptObj.PID] | |
} | |
completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) { | |
NSLog(@"error : %@",error); | |
if(error == nil) | |
{ | |
if(filePath != nil) | |
{ | |
NSLog(@"====Zip file path : %@",filePath); | |
//File Unzip | |
NSString* documentDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; | |
NSString *output = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@",PID]]; | |
if([[NSFileManager defaultManager] fileExistsAtPath:[filePath path]]){ | |
ZipArchive *uzip = [[ZipArchive alloc] init]; | |
if( [uzip UnzipOpenFile:[filePath path]] ) { | |
if( [uzip UnzipFileTo:output overWrite:YES] != NO ) | |
{ | |
NSLog(@"UnZip success: %@",PID); | |
//Remove Zip file | |
NSError *e2; | |
if([[NSFileManager defaultManager] removeItemAtPath:[filePath path] error:&e2]){ | |
NSLog(@"Zip file removed successfully : %@",PID); | |
} | |
NSArray *thumbContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@/%@/Assets", documentDir,PID] error:nil]; | |
NSString *toDir = [documentDir stringByAppendingPathComponent:@"spc_small_t"]; | |
NSString *fromDir = [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/Assets",PID]];//[documentDir stringByAppendingPathComponent:@"/353/Assets"]; | |
NSError *e1; | |
if (![[NSFileManager defaultManager] fileExistsAtPath:toDir]) | |
[[NSFileManager defaultManager] createDirectoryAtPath:toDir withIntermediateDirectories:NO attributes:nil error:&e1]; | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | |
NSError *e3; | |
for (int i = 0; i < [thumbContents count]; i++) { | |
NSString *oldPath = [NSString stringWithFormat:@"%@/%@", fromDir, [thumbContents objectAtIndex:i]]; | |
NSString *newPath = [NSString stringWithFormat:@"%@/%@", toDir, [thumbContents objectAtIndex:i]]; | |
NSError* e; | |
if([[NSFileManager defaultManager] removeItemAtPath:newPath error:&e3]) | |
{ | |
NSLog(@"Small thumb removed successfully : %@",newPath); | |
} | |
if([[NSFileManager defaultManager] moveItemAtPath:oldPath toPath:newPath error:&e]) | |
{ | |
NSLog(@"file : %@",newPath); | |
} | |
else{ | |
NSLog(@"file e: %@",e); | |
} | |
} | |
}); | |
//======Parsing Json file | |
NSString *metadaPath = [NSString stringWithFormat:@"%@", [documentDir stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@/Metadata.txt",PID]]]; | |
if ([[NSFileManager defaultManager] fileExistsAtPath:metadaPath]) | |
{ | |
NSString* content = [[NSString alloc] initWithContentsOfFile:metadaPath encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew) error:&e2]; | |
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding]; | |
if(data!= nil) | |
{ | |
NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; | |
if([SPCDBManager storeTitles:parsedObject pID:PID] == 1){ | |
if([[NSFileManager defaultManager] removeItemAtPath:output error:&e2]){ | |
NSLog(@"UnZipped Dir removed successfully : %@",PID); | |
[SPCDBManager updatePtAvailableOffline:PID state:@"1"]; | |
} | |
} | |
}else{ | |
//NSString* content = [[NSString alloc] initWithContentsOfFile:metadaPath encoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew) error:&e2]; | |
NSString *content = [NSString stringWithContentsOfFile:metadaPath encoding:NSUTF8StringEncoding error:NULL]; | |
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding]; | |
if(data!= nil) | |
{ | |
NSArray *parsedObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; | |
if([SPCDBManager storeTitles:parsedObject pID:PID] == 1){ | |
if([[NSFileManager defaultManager] removeItemAtPath:output error:&e2]){ | |
NSLog(@"UnZipped Dir removed successfully : %@",PID); | |
[SPCDBManager updatePtAvailableOffline:PID state:@"1"]; | |
} | |
} | |
} | |
} | |
} | |
else if([[NSFileManager defaultManager] removeItemAtPath:output error:&e2]){ | |
NSLog(@"1. UnZipped Dir removed successfully : %@",PID); | |
[SPCDBManager updatePtAvailableOffline:PID state:@"0"]; | |
} | |
} | |
else { | |
NSError *e; | |
if([[NSFileManager defaultManager] removeItemAtPath:[filePath path] error:&e]){ | |
NSLog(@"1. Zip file doesnot supported for unzip : %@",PID); | |
} | |
} | |
[uzip UnzipCloseFile]; | |
} | |
else { | |
NSError *e; | |
if([[NSFileManager defaultManager] removeItemAtPath:[filePath path] error:&e]){ | |
NSLog(@"2. Zip file doesnot supported for unzip : %@",PID); | |
} | |
} | |
} | |
} | |
} | |
else{ | |
//[self downloadItem]; | |
} | |
//[self updateProgress]; | |
}]; | |
[downloadTask resume]; | |
// [self.manager setDownloadTaskDidWriteDataBlock:^(NSURLSession *session, NSURLSessionDownloadTask *downloadTask, int64_t bytesWritten, int64_t totalBytesWritten, int64_t totalBytesExpectedToWrite) { | |
// | |
// NSLog(@"Prog : %d, PID : %@",((int)round((float)totalBytesExpectedToWrite / 1024))/((int)round((float)totalBytesWritten / 1024)),PID); | |
// // [self updateProgress]; | |
// | |
// }]; | |
} | |
@catch (NSException *exception) { | |
NSLog(@"AsynchronousDownloadSmallThumbnails err : %@",exception); | |
} | |
@finally { | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)downloadPresentionDetails:(NSArray*)ptArr | |
{ | |
if([SPCNetworkHelper isNetworkConnected]){ | |
if(ptArr.count>0){ | |
for(int i=0;i<ptArr.count;i++){ | |
PresentationModel* ptObj = [ptArr objectAtIndex:i]; | |
NSLog(@"lff : %@ - %@",[SPCDBManager getIsAvailableOffline:ptObj.PID],ptObj.PID); | |
if(![[SPCDBManager getIsAvailableOffline:ptObj.PID] isEqualToString:@"1"]){ | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{ | |
[self AsynchronousDownloadSmallThumbnails:ptObj.PID]; | |
}); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment