|
@@ -1813,6 +1813,9 @@ void UncaughtExceptionHandler(NSException *exception) {
|
|
|
}
|
|
}
|
|
|
-(void) download_offline:(bool) start checkdiskspace:(bool) checkdiskspace
|
|
-(void) download_offline:(bool) start checkdiskspace:(bool) checkdiskspace
|
|
|
{
|
|
{
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
[ERPUtils googleAnalyticsSendRequestString:nil WithScreen:ScreenCodeOfflineSync Action:nil Extra:nil];
|
|
[ERPUtils googleAnalyticsSendRequestString:nil WithScreen:ScreenCodeOfflineSync Action:nil Extra:nil];
|
|
|
if(start)
|
|
if(start)
|
|
|
{
|
|
{
|
|
@@ -1940,7 +1943,8 @@ void UncaughtExceptionHandler(NSException *exception) {
|
|
|
|
|
|
|
|
if([itemjson[@"retry"] intValue]<DOWNLOAD_RETRY_TIMES)
|
|
if([itemjson[@"retry"] intValue]<DOWNLOAD_RETRY_TIMES)
|
|
|
{
|
|
{
|
|
|
- [self handle_download:url start:start type:type taskid:i];
|
|
|
|
|
|
|
+// [self handle_download:url start:start type:type taskid:i];
|
|
|
|
|
+ [self handle_download1:url start:start type:type taskid:i];
|
|
|
alldone=false;
|
|
alldone=false;
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
@@ -2142,8 +2146,8 @@ void UncaughtExceptionHandler(NSException *exception) {
|
|
|
|
|
|
|
|
if([itemjson[@"retry"] intValue]<DOWNLOAD_RETRY_TIMES)
|
|
if([itemjson[@"retry"] intValue]<DOWNLOAD_RETRY_TIMES)
|
|
|
{
|
|
{
|
|
|
- [self handle_download:url start:start type:type taskid:i];
|
|
|
|
|
-
|
|
|
|
|
|
|
+// [self handle_download:url start:start type:type taskid:i];
|
|
|
|
|
+ [self handle_download1:url start:start type:type taskid:i];
|
|
|
break;
|
|
break;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -2750,7 +2754,7 @@ void UncaughtExceptionHandler(NSException *exception) {
|
|
|
}
|
|
}
|
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
|
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
|
|
|
{
|
|
{
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
|
|
NSUserDefaults *defaults =[NSUserDefaults standardUserDefaults];
|
|
|
// 蓝牙扫描枪
|
|
// 蓝牙扫描枪
|
|
|
#ifdef USE_BLE_SCANNER
|
|
#ifdef USE_BLE_SCANNER
|
|
@@ -4219,6 +4223,511 @@ void UncaughtExceptionHandler(NSException *exception) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+#pragma mark new_download
|
|
|
|
|
+- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
|
|
|
|
|
+ if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
|
|
|
|
|
+ if(/*[challenge.protectionSpace.host isEqualToString:@"96.75.188.41"]*/ true){
|
|
|
|
|
+ NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
|
|
|
|
|
+ completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+-(void)handle_download1:(NSString *)url start:(bool) start type:(NSString*) type taskid:(int)taskidx
|
|
|
|
|
+{
|
|
|
|
|
+ //操作有两种,start stop;
|
|
|
|
|
+ //结果有四种, wait , downloading, pause, finish
|
|
|
|
|
+
|
|
|
|
|
+ dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
|
+ // debug;
|
|
|
|
|
+// NSString* file_name=[url lastPathComponent];
|
|
|
|
|
+ //暂停<->开始转换
|
|
|
|
|
+
|
|
|
|
|
+ // bool isdownloading = [self isdownloading:file_name];
|
|
|
|
|
+ if (start)
|
|
|
|
|
+ {//开始下载
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ DebugLog(@"%d start",taskidx);
|
|
|
|
|
+
|
|
|
|
|
+ self.current_task=taskidx;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// NSURL* _url = [NSURL URLWithString:url];//@"http://localhost/test.zip"];
|
|
|
|
|
+//
|
|
|
|
|
+// // _url = [NSURL URLWithString:@"https://rate.apexshipping.com/temp/apache-tomcat-8.0.22.zip"];
|
|
|
|
|
+// //
|
|
|
|
|
+// //
|
|
|
|
|
+// // NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
|
|
|
|
|
+// //
|
|
|
|
|
+// // //设置请求头(GET)
|
|
|
|
|
+// // NSString *range = [NSString stringWithFormat:@"bytes=%lld-", [self getcurrentLength:taskidx]];
|
|
|
|
|
+// // [request setValue:range forHTTPHeaderField:@"Range"];
|
|
|
|
|
+// //
|
|
|
|
|
+// // self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+// NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:_url];
|
|
|
|
|
+//
|
|
|
|
|
+// //设置请求头(GET)
|
|
|
|
|
+// NSString *range = [NSString stringWithFormat:@"bytes=%lld-", [self getcurrentLength:taskidx]];
|
|
|
|
|
+// [request setValue:range forHTTPHeaderField:@"Range"];
|
|
|
|
|
+//
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+// RANetworkTaskDelegate *delegate = [RANetworkTaskDelegate sharedInstance];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSURLSession *session = nil;
|
|
|
|
|
+
|
|
|
|
|
+ NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
|
|
|
|
|
+ config.timeoutIntervalForRequest = JSON_TIMEOUT;
|
|
|
|
|
+
|
|
|
|
|
+ NSOperationQueue *queue = [[NSOperationQueue alloc] init];
|
|
|
|
|
+
|
|
|
|
|
+ session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:queue];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
|
|
|
|
|
+
|
|
|
|
|
+ req.HTTPMethod = @"GET";
|
|
|
|
|
+// long offset = [self getcurrentLength:taskidx];
|
|
|
|
|
+// if (offset > 0) {
|
|
|
|
|
+// [req addValue:[NSString stringWithFormat:@"bytes=%ld-",(unsigned long)offset] forHTTPHeaderField:@"Range"];
|
|
|
|
|
+// }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSString *range = [NSString stringWithFormat:@"bytes=%lld-", [self getcurrentLength:taskidx]];
|
|
|
|
|
+ [req setValue:range forHTTPHeaderField:@"Range"];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSURLSessionDataTask *downloadTask = [session dataTaskWithRequest:req];
|
|
|
|
|
+
|
|
|
|
|
+ [downloadTask resume];
|
|
|
|
|
+ self.session =session;
|
|
|
|
|
+ self.curr_task = downloadTask;
|
|
|
|
|
+
|
|
|
|
|
+ }else{ //暂停
|
|
|
|
|
+ DebugLog(@"%d pause",taskidx);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ [self.curr_task cancel];
|
|
|
|
|
+
|
|
|
|
|
+ self.curr_task = nil;
|
|
|
|
|
+// [self.connection cancel];
|
|
|
|
|
+// self.connection = nil;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
|
|
|
|
|
+
|
|
|
|
|
+ NSHTTPURLResponse* httpresponse = (NSHTTPURLResponse *)response;
|
|
|
|
|
+
|
|
|
|
|
+ if (httpresponse.statusCode == 206) {//!!!断点续传的状态码为206
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ DebugLog(@"%d begin",self.current_task);
|
|
|
|
|
+
|
|
|
|
|
+ long long position=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"position"] longLongValue];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //获取下载文件大小
|
|
|
|
|
+ // self.fileLength = response.expectedContentLength;
|
|
|
|
|
+ NSMutableDictionary* currenttask=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]] mutableCopy];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //获取文件名
|
|
|
|
|
+ //self.fileName = response.suggestedFilename;
|
|
|
|
|
+
|
|
|
|
|
+ currenttask[@"fileName"] = response.suggestedFilename;
|
|
|
|
|
+ currenttask[@"response"] = @"true";
|
|
|
|
|
+ [self add_downloadlog:[NSString stringWithFormat:@"Begin download file %@.",currenttask[@"fileName"]]];
|
|
|
|
|
+
|
|
|
|
|
+ self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]]=currenttask;
|
|
|
|
|
+
|
|
|
|
|
+ //文件路径
|
|
|
|
|
+ NSString *temp = NSTemporaryDirectory();//[NSSearchPathForDirectoriesInDomains(NSTemporaryDirectory(), NSUserDomainMask, YES) lastObject];
|
|
|
|
|
+
|
|
|
|
|
+ NSString * ver = self.download_task[@"ver"];
|
|
|
|
|
+ NSString* location=[NSString stringWithFormat:@"%@/%@",ver,response.suggestedFilename];
|
|
|
|
|
+ NSString *filePath = [temp stringByAppendingPathComponent:location];
|
|
|
|
|
+ DebugLog(@"save to: %@",filePath);
|
|
|
|
|
+
|
|
|
|
|
+ if (position>0) {
|
|
|
|
|
+ //不需要创建文件;
|
|
|
|
|
+ //创建一个用来写数据的文件句柄
|
|
|
|
|
+ self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ // 第一次获取的才是文件正确大小
|
|
|
|
|
+ currenttask[@"fileLength"] = [NSNumber numberWithLongLong:response.expectedContentLength];
|
|
|
|
|
+ //创建一个空的文件到沙盒
|
|
|
|
|
+ NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
|
|
+ [fileManager createFileAtPath:filePath contents:nil attributes:nil];
|
|
|
|
|
+
|
|
|
|
|
+ //创建一个用来写数据的文件句柄
|
|
|
|
|
+ self.fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
|
|
|
|
|
+ }else{
|
|
|
|
|
+
|
|
|
|
|
+ //debug 这里缺少文件的情况还没处理
|
|
|
|
|
+
|
|
|
|
|
+ [self.connection cancel];
|
|
|
|
|
+ self.connection = nil;
|
|
|
|
|
+ DebugLog(@"该文件不存在");
|
|
|
|
|
+ NSString* str_status=[NSString stringWithFormat:@"Server return %d.",(int)httpresponse.statusCode];
|
|
|
|
|
+
|
|
|
|
|
+ [self add_downloadlog:@"Offline file does not exist,please contact your administrator."];
|
|
|
|
|
+ [self add_downloadlog:str_status];
|
|
|
|
|
+ }
|
|
|
|
|
+ completionHandler(NSURLSessionResponseAllow);
|
|
|
|
|
+}
|
|
|
|
|
+- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ //移动到文件末尾
|
|
|
|
|
+ [self.fileHandle seekToEndOfFile];
|
|
|
|
|
+
|
|
|
|
|
+ //写入数据到文件
|
|
|
|
|
+ [self.fileHandle writeData:data];
|
|
|
|
|
+
|
|
|
|
|
+ NSMutableDictionary* currenttask=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]] mutableCopy];
|
|
|
|
|
+
|
|
|
|
|
+ long long position=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"position"] longLongValue];
|
|
|
|
|
+ position += data.length;
|
|
|
|
|
+ currenttask[@"position"] = [NSNumber numberWithLongLong:position];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]]=currenttask;
|
|
|
|
|
+
|
|
|
|
|
+ //更新画面中的进度
|
|
|
|
|
+ int fileLength=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"fileLength"] intValue];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ double progress = (double)position/fileLength;
|
|
|
|
|
+
|
|
|
|
|
+ self.file_progress = progress;
|
|
|
|
|
+ [self.settingUI onProgressUpdate];
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
|
|
|
|
|
+
|
|
|
|
|
+// double progress = (double)totalBytesSent / totalBytesExpectedToSend;
|
|
|
|
|
+
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
|
|
|
|
|
+
|
|
|
|
|
+// if (self.decryptHandler) {
|
|
|
|
|
+//
|
|
|
|
|
+// self.result.decryptHandler = self.decryptHandler;
|
|
|
|
|
+//
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// if (error) {
|
|
|
|
|
+//
|
|
|
|
|
+// self.result.error = error;
|
|
|
|
|
+//
|
|
|
|
|
+// DebugLog(@"Delegate recv Error: %@",error.localizedDescription);
|
|
|
|
|
+//
|
|
|
|
|
+// }
|
|
|
|
|
+//
|
|
|
|
|
+// self.result.data = self.recvData;
|
|
|
|
|
+//
|
|
|
|
|
+// DebugLog(@"Delegate recv data %@",self.recvData);
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+//
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if (error) {
|
|
|
|
|
+ DebugLog(@"%d error message:%@",self.current_task,error);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSMutableDictionary* currenttask=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]] mutableCopy];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ // self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"position"] = [NSNumber numberWithLongLong:0];
|
|
|
|
|
+ currenttask[@"status"] = @"pause";
|
|
|
|
|
+ int retry=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"retry"] intValue];
|
|
|
|
|
+ currenttask[@"retry"]= [NSNumber numberWithInt:retry+1];
|
|
|
|
|
+ self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]]=currenttask;
|
|
|
|
|
+
|
|
|
|
|
+ // self.currentLength = 0;
|
|
|
|
|
+ // self.fileLength = 0;
|
|
|
|
|
+ [self.fileHandle closeFile];
|
|
|
|
|
+ self.fileHandle = nil;
|
|
|
|
|
+ [self.connection cancel];
|
|
|
|
|
+ self.connection = nil;
|
|
|
|
|
+
|
|
|
|
|
+ [self download_offline:true checkdiskspace:false];
|
|
|
|
|
+
|
|
|
|
|
+ } else {
|
|
|
|
|
+ NSMutableDictionary* currenttask=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]] mutableCopy];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ [self add_downloadlog:[NSString stringWithFormat:@"Finish download file %@.",currenttask[@"fileName"]]];
|
|
|
|
|
+
|
|
|
|
|
+ DebugLog(@"%d finish",self.current_task);
|
|
|
|
|
+ currenttask[@"position"] = [NSNumber numberWithLongLong:0];
|
|
|
|
|
+
|
|
|
|
|
+ self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]]=currenttask;
|
|
|
|
|
+ // self.currentLength = 0;
|
|
|
|
|
+ // self.fileLength = 0;
|
|
|
|
|
+ [self.fileHandle closeFile];
|
|
|
|
|
+ self.fileHandle = nil;
|
|
|
|
|
+ int result=[self unpack];
|
|
|
|
|
+ NSString* temp = NSTemporaryDirectory();
|
|
|
|
|
+
|
|
|
|
|
+ NSString* filename = self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"fileName"];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSString * ver = self.download_task[@"ver"];
|
|
|
|
|
+
|
|
|
|
|
+ NSString* unziplocation=[NSString stringWithFormat:@"%@/%@",ver,[filename stringByDeletingPathExtension]];
|
|
|
|
|
+
|
|
|
|
|
+ NSString *unZipTo = [temp stringByAppendingPathComponent:unziplocation];
|
|
|
|
|
+
|
|
|
|
|
+ switch (result) {
|
|
|
|
|
+ case UNZIP_FILE_DAMAGE:
|
|
|
|
|
+ {
|
|
|
|
|
+ NSMutableDictionary* currenttask=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]] mutableCopy];
|
|
|
|
|
+ currenttask[@"status"] = @"pause";
|
|
|
|
|
+ int retry=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"retry"] intValue];
|
|
|
|
|
+ currenttask[@"retry"]= [NSNumber numberWithInt:retry+1];
|
|
|
|
|
+ self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]]=currenttask;
|
|
|
|
|
+
|
|
|
|
|
+ // self.currentLength = 0;
|
|
|
|
|
+ // self.fileLength = 0;
|
|
|
|
|
+ [self.fileHandle closeFile];
|
|
|
|
|
+ self.fileHandle = nil;
|
|
|
|
|
+ [self.connection cancel];
|
|
|
|
|
+ self.connection = nil;
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ case UNZIP_SUCCESS:
|
|
|
|
|
+ {
|
|
|
|
|
+ NSString* type = self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"type"];
|
|
|
|
|
+
|
|
|
|
|
+ if([type isEqualToString:@"img"])
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ NSString* parent_path = self.download_task[@"path"];
|
|
|
|
|
+ if(parent_path.length==0)
|
|
|
|
|
+ parent_path=@"";
|
|
|
|
|
+ parent_path = [parent_path stringByReplacingOccurrencesOfString:@"http://" withString:@""];
|
|
|
|
|
+ parent_path = [parent_path stringByReplacingOccurrencesOfString:@"https://" withString:@""];
|
|
|
|
|
+ NSString* cachefolder=[NSString stringWithFormat:@"%@/image_cache/%@",ver,parent_path];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSString *imagecache = [temp stringByAppendingPathComponent:cachefolder];
|
|
|
|
|
+
|
|
|
|
|
+ BOOL bdir=YES;
|
|
|
|
|
+ NSError *error = nil;
|
|
|
|
|
+ if(! [[NSFileManager defaultManager] fileExistsAtPath:imagecache isDirectory:&bdir])
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ bool bsuccess=[[NSFileManager defaultManager] createDirectoryAtPath:imagecache withIntermediateDirectories:YES attributes:nil error:&error];
|
|
|
|
|
+
|
|
|
|
|
+ if(!bsuccess)
|
|
|
|
|
+ DebugLog(@"Create temp folder failed");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ error=nil;
|
|
|
|
|
+ // NSString* backupfolder=[NSString stringWithFormat:@"%@/test.xml",ver];
|
|
|
|
|
+ // NSString* backup=[temp stringByAppendingPathComponent:backupfolder];
|
|
|
|
|
+
|
|
|
|
|
+ // NSError *error = nil;
|
|
|
|
|
+ // NSURL* uf=[NSURL fileURLWithPath:imagecache];
|
|
|
|
|
+ // if( [[NSFileManager defaultManager] replaceItemAtURL:[NSURL fileURLWithPath:imagecache]
|
|
|
|
|
+ // withItemAtURL:[NSURL fileURLWithPath:unZipTo]
|
|
|
|
|
+ // backupItemName:backup
|
|
|
|
|
+ // options:NSFileManagerItemReplacementWithoutDeletingBackupItem
|
|
|
|
|
+ // resultingItemURL:nil error:&error])
|
|
|
|
|
+ if(![RAUtils mergeContentsOfPath:unZipTo intoPath:imagecache error:&error])
|
|
|
|
|
+ // if([[NSFileManager defaultManager] moveItemAtPath:unZipTo toPath:imagecache error:&error]!=YES)// prePath 为原路径、 cenPath 为目标路径
|
|
|
|
|
+ {
|
|
|
|
|
+ DebugLog(@"move image folder failed");
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ currenttask[@"status"] = @"finish";
|
|
|
|
|
+ DebugLog(@"move image folder successful");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ else if([type isEqualToString:@"sql"])
|
|
|
|
|
+ {
|
|
|
|
|
+ // AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
|
|
|
|
|
+ NSString * ver = self.download_task[@"ver"];
|
|
|
|
|
+ NSString* tempdb=[NSString stringWithFormat:@"%@/%@",ver,DBNAME];
|
|
|
|
|
+ tempdb=[temp stringByAppendingPathComponent:tempdb];
|
|
|
|
|
+ sqlite3* tdb=[iSalesDB get_db_at:tempdb];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ [self add_downloadlog:@"updating local database..."];
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @try {
|
|
|
|
|
+ char *errmsg=NULL;
|
|
|
|
|
+ // [iSalesDB disable_trigger]
|
|
|
|
|
+ [iSalesDB disable_trigger];
|
|
|
|
|
+
|
|
|
|
|
+ if (sqlite3_exec(tdb, "begin", NULL, NULL, &errmsg)==SQLITE_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+
|
|
|
|
|
+ sqlite3_exec(tdb, "drop trigger if exists wishlist_insert;drop trigger if exists wishlist_update;drop trigger if exists wishlist_delete;drop trigger if exists offline_portfolio_insert;drop trigger if exists offline_portfolio_update;drop trigger if exists offline_portfolio_delete;drop trigger if exists offline_pdf_insert;drop trigger if exists offline_pdf_update;drop trigger if exists offline_pdf_delete;drop trigger if exists offline_pdf;", NULL, NULL, &errmsg);
|
|
|
|
|
+
|
|
|
|
|
+ sqlite3_free(errmsg);
|
|
|
|
|
+ DebugLog(@"开启事务");
|
|
|
|
|
+ NSString* jsonpath =[NSString stringWithFormat:@"%@/offLineSql/%@",unZipTo,@"SqlJson.txt"];
|
|
|
|
|
+
|
|
|
|
|
+ NSDictionary* orderjson=[RAUtils dictfromfile:jsonpath];
|
|
|
|
|
+ int count = [orderjson[@"count"] intValue];
|
|
|
|
|
+ for(int i=0;i<count;i++)
|
|
|
|
|
+ {
|
|
|
|
|
+ NSString* sqlfile=orderjson[[NSString stringWithFormat:@"item_%d",i]];
|
|
|
|
|
+ NSString* sqlpath =[NSString stringWithFormat:@"%@/offLineSql/%@",unZipTo,sqlfile];
|
|
|
|
|
+ NSError *error = nil;
|
|
|
|
|
+ NSString* sql = [NSString stringWithContentsOfFile:sqlpath encoding:NSUTF8StringEncoding error:&error];
|
|
|
|
|
+ //DebugLog(@"NSString类方法读取的内容是:\n%@",content);
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ if(sqlite3_exec(tdb, [sql UTF8String], NULL, NULL, &errmsg)!=SQLITE_OK)
|
|
|
|
|
+ {
|
|
|
|
|
+ //throw exception;
|
|
|
|
|
+ if(errmsg==nil)
|
|
|
|
|
+ errmsg="Unknown error.";
|
|
|
|
|
+ NSString *nsmsg = [[NSString alloc]initWithUTF8String:errmsg];
|
|
|
|
|
+ [NSException raise:@"offline update sql error" format:@"FILE:%@ MSG:%@",sqlfile,nsmsg];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sqlite3_free(errmsg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ sqlite3_exec(tdb, "CREATE TRIGGER if not exists wishlist_insert after insert on wishlist BEGIN select offline_dirty(); UPDATE wishlist SET modify_time= datetime('now', 'localtime') WHERE _id=new._id;END;CREATE TRIGGER if not exists wishlist_update after update on wishlist BEGIN select offline_dirty(); UPDATE wishlist SET modify_time= datetime('now', 'localtime'), is_dirty = 1 WHERE _id=new._id; END;CREATE TRIGGER if not exists wishlist_delete after DELETE ON wishlist BEGIN select offline_dirty(); END;CREATE TRIGGER if not exists offline_pdf_insert after insert on offline_pdf BEGIN select offline_dirty(); UPDATE offline_pdf SET modify_time= datetime('now', 'localtime') WHERE _id=new._id;END;CREATE TRIGGER if not exists offline_pdf_update after update on offline_pdf BEGIN select offline_dirty(); UPDATE offline_pdf SET modify_time= datetime('now', 'localtime'), is_dirty = 1 WHERE _id=new._id; END;CREATE TRIGGER if not exists offline_pdf_delete after DELETE ON offline_pdf BEGIN select offline_dirty(); END;CREATE TRIGGER if not exists offline_portfolio_insert after insert on offline_portfolio BEGIN select offline_dirty(); UPDATE offline_portfolio SET modify_time= datetime('now', 'localtime') WHERE _id=new._id;END;CREATE TRIGGER if not exists offline_portfolio_update after update on offline_portfolio BEGIN select offline_dirty(); UPDATE offline_portfolio SET modify_time= datetime('now', 'localtime'), is_dirty = 1 WHERE _id=new._id; END;CREATE TRIGGER if not exists offline_portfolio_delete after DELETE ON offline_portfolio BEGIN select offline_dirty(); END;", NULL, NULL, &errmsg);
|
|
|
|
|
+ }
|
|
|
|
|
+ //提交事务
|
|
|
|
|
+ if (sqlite3_exec(tdb, "commit", NULL, NULL, &errmsg)==SQLITE_OK) {
|
|
|
|
|
+
|
|
|
|
|
+ DebugLog(@"提交事务");
|
|
|
|
|
+
|
|
|
|
|
+ sqlite3_free(errmsg);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ [self add_downloadlog:@"updating local database successful"];
|
|
|
|
|
+ currenttask[@"status"] = @"finish";
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @catch (NSException *exception) {
|
|
|
|
|
+ char *errmsg=NULL;
|
|
|
|
|
+ [self add_downloadlog:[NSString stringWithFormat:@"updating local database error, %@",exception.description]];
|
|
|
|
|
+ [self add_downloadlog:@"updating local database failed rollback..."];
|
|
|
|
|
+ if (sqlite3_exec(tdb, "rollback", NULL, NULL, &errmsg)==SQLITE_OK) {
|
|
|
|
|
+ DebugLog(@"回滚事务");
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ else
|
|
|
|
|
+ {
|
|
|
|
|
+ [self add_downloadlog:@"rollback failed."];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ NSMutableDictionary* currenttask=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]] mutableCopy];
|
|
|
|
|
+ currenttask[@"status"] = @"pause";
|
|
|
|
|
+ //int retry=[self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]][@"retry"] intValue];
|
|
|
|
|
+ currenttask[@"retry"]= [NSNumber numberWithInt:DOWNLOAD_RETRY_TIMES];
|
|
|
|
|
+ self.download_task [[NSString stringWithFormat:@"item_%d",self.current_task]]=currenttask;
|
|
|
|
|
+
|
|
|
|
|
+ // self.currentLength = 0;
|
|
|
|
|
+ // self.fileLength = 0;
|
|
|
|
|
+ [self.fileHandle closeFile];
|
|
|
|
|
+ self.fileHandle = nil;
|
|
|
|
|
+ [self.connection cancel];
|
|
|
|
|
+ self.connection = nil;
|
|
|
|
|
+ //break;
|
|
|
|
|
+ }
|
|
|
|
|
+ @finally {
|
|
|
|
|
+
|
|
|
|
|
+ // [iSalesDB enable_trigger]
|
|
|
|
|
+ [iSalesDB enable_trigger];
|
|
|
|
|
+ sqlite3_close(tdb);
|
|
|
|
|
+ // [iSalesDB close_db:tdb];
|
|
|
|
|
+ // [self SelectData];
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ else if([type isEqualToString:@"script"])
|
|
|
|
|
+ {
|
|
|
|
|
+ currenttask[@"status"] = @"finish";
|
|
|
|
|
+ }
|
|
|
|
|
+ else if([type isEqualToString:@"resource"])
|
|
|
|
|
+ {
|
|
|
|
|
+ currenttask[@"status"] = @"finish";
|
|
|
|
|
+ }
|
|
|
|
|
+ // debug
|
|
|
|
|
+ // int c=self.current_task+1;
|
|
|
|
|
+ // double t=[ self.download_task[@"count"] doubleValue];
|
|
|
|
|
+ double oprogress=(self.current_task+1) / [ self.download_task[@"count"] doubleValue];
|
|
|
|
|
+ self.overall_progress = oprogress;
|
|
|
|
|
+ [self.settingUI onProgressUpdate];
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ case UNZIP_NO_SPACE:
|
|
|
|
|
+ {
|
|
|
|
|
+ [self add_downloadlog:@"Disk space not enough."];
|
|
|
|
|
+
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ case UNZIP_OPEN_FAILED:
|
|
|
|
|
+ {
|
|
|
|
|
+ [self add_downloadlog:[NSString stringWithFormat:@"Unpack file %@ failed, error: cannot unpack zip.",filename]];
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ default:
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ [self download_offline:true checkdiskspace:false];
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ printf("net work complete\n");
|
|
|
|
|
+
|
|
|
|
|
+//
|
|
|
|
|
+ [session invalidateAndCancel];
|
|
|
|
|
+}
|
|
|
@end
|
|
@end
|