huangxiaodong 11 月之前
父节点
当前提交
f788b225b0

+ 11 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadCacheManager.h

@@ -25,6 +25,17 @@ NS_ASSUME_NONNULL_BEGIN
 /**  增加配置信息 */
 + (BOOL)saveFileInfoWithDict:(NSDictionary *)dict;
 
+/**  删除某个文件 */
++ (BOOL)deleteFileWithUrl:(NSString *)url;
+
+/**  清理所有下载文件及下载信息 */
++ (BOOL)clearDisks;
+
+/**  取消所有当前下载的文件 清理内存缓存的数据 */
++ (BOOL)clearMemory;
+
+/**  取消所有当前下载的文件 删除磁盘所有的下载 清理内存缓存的数据 */
++ (BOOL)clearMemoryAndDisk;
 
 + (NSString *)getFullDirector;
 

+ 48 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadCacheManager.m

@@ -117,5 +117,53 @@ static dispatch_semaphore_t _semaphore;
     
 }
 
+/**  删除配置信息 */
++ (BOOL)deleteFileWithUrl:(NSString *)url {
+    // 线程等待 分配信号量 (信号量 + 1)
+    dispatch_semaphore_wait(_semaphore, DISPATCH_TIME_FOREVER);
+    
+    // 删除缓存的文件
+    NSString *fileFolder = [mixDownloadCacheManager getFullDirector];
+    NSString *urlFileName = [url lastPathComponent];
+    NSString * decodeUrlFileName= [urlFileName stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
+    NSString *fileName = decodeUrlFileName;
+    NSString *fileURL = [fileFolder stringByAppendingPathComponent:fileName];
+
+    
+    BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:fileURL error:nil];
+//    BOOL flag = [[NSFileManager defaultManager] removeItemAtPath:dict[@"fullPath"] error:nil];
+    
+    // 删除plist的文件文件记录
+    NSString *key = [[url lastPathComponent] stringByDeletingPathExtension];
+//    NSDictionary *dict = SGDownloadList[key];
+    [[self getDownloadList] removeObjectForKey:key];
+    BOOL writeFlag = [[self getDownloadList] writeToFile:[self getNewPlistPath] atomically:YES];
+    
+    // 线程结束 释放信号量(信号量 - 1)
+    dispatch_semaphore_signal(_semaphore);
+    return (flag && writeFlag);
+}
+
+#pragma mark -
+
++ (BOOL)clearDisks {
+    // 1.删除所有的文件下载信息关联表
+    // 2.删除cache 下的download文件夹
+   return  [[NSFileManager defaultManager] removeItemAtPath:KFullDirector error:nil];
+      
+}
+
+/**  取消所有当前下载的文件 清理内存缓存的数据 */
++ (BOOL)clearMemory {
+    // 删除信息关联
+    _downloadList = nil;
+    
+    return YES;
+}
+
+/**  取消所有当前下载的文件 删除磁盘所有的下载 清理内存缓存的数据 */
++ (BOOL)clearMemoryAndDisk {
+    return ([self clearMemory] && [self clearDisks]);
+}
 
 @end

+ 17 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadManager.h

@@ -49,9 +49,26 @@ typedef void(^MixDownloadCompleteBlock)(NSDictionary *respose,NSError *error);
 - (void)downloadWithURL:(NSURL *)url
                complete:(MixDownloadCompleteBlock)complete;
 
+
+#pragma mark - 队列中的任务进行操作
+/** 开始任务(不会自动添加任务,列队中没有就直接返回) 后续改为会自动添加任务 */
+- (void)startDownLoadWithUrl:(NSString *)url;
+
+/** 暂停任务(暂停下载url内容的任务) */
+- (void)supendDownloadWithUrl:(NSString *)url;
+
+/** 取消任务(取消下载url内容的任务) */
+- (void)cancelDownloadWithUrl:(NSString *)url;
+
+/** 暂停当前所有的下载任务 下载任务不会从列队中删除 */
+- (void)suspendAllDownloadTask;
+
 /** 开启当前列队中所有被暂停的下载任务 */
 - (void)startAllDownloadTask;
 
+/** 停止当前所有的下载任务 调用此方法会清空所有列队下载任务 */
+- (void)stopAllDownloads;
+
 @end
 
 NS_ASSUME_NONNULL_END

+ 26 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadManager.m

@@ -84,12 +84,38 @@
     
 }
 
+#pragma mark - 队列中的任务进行操作
+- (void)startDownLoadWithUrl:(NSString *)url {
+    // 开始下载
+    [self.downloadSession startDownLoadWithUrl:url];
+}
+
+- (void)supendDownloadWithUrl:(NSString *)url {
+    // 暂停下载
+    [self.downloadSession supendDownloadWithUrl:url];
+}
+
+- (void)cancelDownloadWithUrl:(NSString *)url {
+    // 取消下载
+    [self.downloadSession cancelDownloadWithUrl:url];
+}
+
+/** 暂停当前所有的下载任务 下载任务不会从列队中删除 */
+- (void)suspendAllDownloadTask {
+    [self.downloadSession suspendAllDownloads];
+}
 
 /** 开启当前列队中所有被暂停的下载任务 */
 - (void)startAllDownloadTask {
     [self.downloadSession startAllDownloads];
 }
 
+/** 停止当前所有的下载任务 调用此方法会清空所有列队下载任务 */
+- (void)stopAllDownloads {
+    [self.downloadSession cancelAllDownloads];
+    self.downloadSession = nil;
+}
+
 #pragma mark - lazy load
 - (mixDownloadSession *)downloadSession {
     if (!_downloadSession) {

+ 6 - 4
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadOperation.h

@@ -8,6 +8,12 @@
 #import <Foundation/Foundation.h>
 #import "mixDownloadCacheManager.h"
 
+extern NSString * _Nullable const mixDownloadCompleteNoti;
+
+typedef void(^mixReceiveResponseOperation)(NSString *filePath);
+typedef void(^mixReceivDataOperation)(NSInteger completeSize,NSInteger expectSize);
+typedef void(^mixCompleteOperation)(NSDictionary *respose,NSError *error);
+
 /** 下载状态*/
 typedef  enum : NSUInteger {
     DownloadStateWaiting = 0,   /** 下载等待中 */
@@ -20,10 +26,6 @@ typedef  enum : NSUInteger {
 
 NS_ASSUME_NONNULL_BEGIN
 
-typedef void(^mixReceiveResponseOperation)(NSString *filePath);
-typedef void(^mixReceivDataOperation)(NSInteger completeSize,NSInteger expectSize);
-typedef void(^mixCompleteOperation)(NSDictionary *respose,NSError *error);
-
 @protocol mixDownloadOperationProtocol <NSObject>
                                     
 // 供queue管理方法

+ 11 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadQueue.h

@@ -9,6 +9,13 @@
 
 NS_ASSUME_NONNULL_BEGIN
 
+/** 下载处理 */
+typedef  enum : NSUInteger {
+    DownloadHandleTypeStart,    // 开始下载
+    DownloadHandleTypeSuspend,  // 暂停下载
+    DownloadHandleTypeCancel,   // 取消下载
+} DownloadHandleType;
+
 @interface mixDownloadQueue : NSObject
 
 /** 设置最大的并发下载个数 */
@@ -23,6 +30,10 @@ NS_ASSUME_NONNULL_BEGIN
                    progress:(void(^)(NSInteger completeSize,NSInteger expectSize))progress
                    complete:(void(^)(NSDictionary *respose,NSError *error))complet;
 
+// 对当前任务进行操作
+- (void)operateDownloadWithUrl:(NSString *)url session:(NSURLSession *)session handle:(DownloadHandleType)handle;
+
+- (void)suspendAllTasksWithSession:(NSURLSession *)session;
 - (void)startAllTasksWithSession:(NSURLSession *)session;
 
 // 供downloader 处理下载调用

+ 157 - 1
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadQueue.m

@@ -23,11 +23,22 @@
 
     if (self = [super init]) {
         _maxCount = 3;
-        
+        [self registeNotification];
     }
     return self;
 }
 
+- (void)dealloc {
+    [[NSNotificationCenter defaultCenter] removeObserver:self];
+}
+
+- (void)didResiveDownloadFileCompete:(NSNotification *)noti {
+    mixDownloadOperation *operation = noti.object;
+    if (operation) {
+        [self.operations removeObject:operation];
+    }
+}
+
 - (NSMutableArray *)getOperationDoing {
     
     NSMutableArray *doingArray = [NSMutableArray array];
@@ -39,6 +50,19 @@
     return doingArray;
 }
 
+- (NSMutableArray *)getOperationWaiting {
+    
+    NSMutableArray *waitingArray = [NSMutableArray array];
+    [self.operations enumerateObjectsUsingBlock:^(mixDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+        
+        mixDownloadOperation *operation = (mixDownloadOperation *)obj;
+        if (operation.downloadState == DownloadStateWaiting) {
+            [waitingArray addObject:operation];
+        }
+    }];
+    return waitingArray;
+}
+
 #pragma mark - handle Out operations
 - (void)addDownloadWithSession:(NSURLSession *)session URL:(NSURL *)url begin:(void(^)(NSString *))begin progress:(void(^)(NSInteger,NSInteger))progress complete:(void(^)(NSDictionary *,NSError *))complet {
     // 获取operation对象
@@ -73,6 +97,94 @@
 }
 
 
+- (void)operateDownloadWithUrl:(NSString *)url session:(NSURLSession *)session handle:(DownloadHandleType)handle {
+    // 1、任务列表里取任务
+    mixDownloadOperation *operation = [self operationWithUrl:url];
+    
+    // 2、本地plist文件里提取的任务
+    if (!operation) {
+        NSDictionary *dict = [mixDownloadCacheManager queryFileInfoWithUrl:url];
+        operation = [mixDownloadOperation mj_objectWithKeyValues:dict];
+    }
+
+    // 3、本地plist文件里提取的任务不存在dataTask
+    if (!operation.dataTask && operation.currentSize != operation.totalSize) {
+        operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
+        [self.operations addObject:operation];
+    }
+    
+    if (operation) {
+        
+        switch (handle) {
+            case DownloadHandleTypeStart:
+                
+                switch (operation.dataTask.state) {
+                    case NSURLSessionTaskStateRunning:
+                        HLog(@"NSURLSessionTaskStateRunning");
+                        break;
+                    
+                    case NSURLSessionTaskStateSuspended:
+                        HLog(@"NSURLSessionTaskStateSuspended");
+                        operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
+                        break;
+                        
+                    case NSURLSessionTaskStateCanceling:
+                        HLog(@"NSURLSessionTaskStateCanceling");
+                        operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
+                        break;
+                        
+                    case NSURLSessionTaskStateCompleted:
+                        HLog(@"NSURLSessionTaskStateCompleted");
+                        break;
+                        
+                    default:
+                        break;
+                }
+                
+                if ([self getOperationDoing].count >= self.maxCount) { // 下载中任务数超过最大任务限制
+                    HLog(@"下载中的任务数超过最大限制")
+//                    [operation.dataTask suspend]; // 暂停
+                    [self operationWaitingWithOperation:operation];
+                    return;
+                }
+                
+                [operation.dataTask resume]; // 开始
+                [self operationStartWithOperation:operation];
+                break;
+            case DownloadHandleTypeSuspend:
+                [operation.dataTask suspend]; // 暂停
+                [self operationSuspendWithOperation:operation];
+                break;
+            case DownloadHandleTypeCancel:
+                if (operation.dataTask) { // 任务列表删除任务
+                    [operation.dataTask cancel];  // 取消
+                    [self.operations removeObject:operation];
+                }
+                [mixDownloadCacheManager deleteFileWithUrl:url];   // plist删除任务
+                [self operationDeleteWithOperation:operation];
+                break;
+        }
+    }
+}
+
+- (void)suspendAllTasksWithSession:(NSURLSession *)session {
+    // 暂停所有的任务
+    [self.operations enumerateObjectsUsingBlock:^(mixDownloadOperation * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+        
+        mixDownloadOperation *operation = (mixDownloadOperation *)obj;
+        if (operation.currentSize != operation.totalSize) { // 暂停未完成的下载任务
+            if (!operation.dataTask) { // 给plist里的任务添加dataTask 添加到operations
+                operation.dataTask = [self.session mix_downloadDataTaskWithURLString:operation.url startSize:operation.currentSize];
+            }
+            [operation.dataTask suspend];
+            [self operationSuspendWithOperation:operation];
+        }else {
+            HLog(@"已完成的任务");
+        }
+
+    }];
+}
+
 - (void)startAllTasksWithSession:(NSURLSession *)session {
     
     // 开始所有的任务
@@ -118,11 +230,47 @@
     }];
 }
 
+#pragma mark-监听通知
+- (void)registeNotification {
+    // 监听完成通知
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didResiveDownloadFileCompete:) name:mixDownloadCompleteNoti object:nil];
+    
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:mixDownloadTaskExeEnd object:nil];
+//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeError object:nil];
+    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:mixDownloadTaskExeSuspend object:nil];
+//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExeEnd:) name:SGDownloadTaskExeDelete object:nil];
+}
+    
+- (void)taskExeEnd:(NSNotification *)notification
+{
+    NSMutableArray *operationWaiting = [self getOperationWaiting];
+    
+    [operationWaiting enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
+        mixDownloadOperation *operation = obj;
+        if ([self getOperationDoing].count < self.maxCount) {
+            [self operateDownloadWithUrl:operation.url session:self.session handle:DownloadHandleTypeStart];
+            
+        }else {
+            *stop = YES;
+        }
+    }];
+}
+
+#pragma mark 发送通知
+// 暂停某一个operation 保存本地 通知外界
+- (void)operationSuspendWithOperation:(mixDownloadOperation *)operation {
+    operation.downloadState = DownloadStateSuspended;
+    [mixDownloadCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
+    //没有必要再操作的时候通知外面
+    //[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeSuspend object:nil userInfo:@{@"operation" : operation}];
+}
+
 // 重启某一个operation 保存本地 通知外界
 - (void)operationStartWithOperation:(mixDownloadOperation *)operation {
     operation.downloadState = DownloadStateDoing;
     [mixDownloadCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
     
+    //没有必要再操作的时候通知外面
     //[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeing object:nil userInfo:@{@"operation" : operation}];
 }
 
@@ -131,9 +279,17 @@
     operation.downloadState = DownloadStateWaiting;
     [mixDownloadCacheManager saveFileInfoWithDict:[operation downLoadInfoWithFinished:NO]];
     
+    //没有必要再操作的时候通知外面
     //[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeSuspend object:nil userInfo:@{@"operation" : operation}];
 }
     
+// 删除某一个operation 保存本地 通知外界
+- (void)operationDeleteWithOperation:(mixDownloadOperation *)operation {
+    //1. 任务删除了为什么不删除本地数据?
+    
+    //[[NSNotificationCenter defaultCenter] postNotificationName:SGDownloadTaskExeDelete object:nil userInfo:nil];
+}
+
 #pragma mark - handle download
 - (void)dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response {
     [[self oprationWithDataTask:dataTask] operateWithResponse:response];

+ 7 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadSession.h

@@ -16,8 +16,15 @@ NS_ASSUME_NONNULL_BEGIN
                progress:(void(^)(NSInteger,NSInteger))progress
                complete:(void(^)(NSDictionary *,NSError *))complet;
 
+- (void)startDownLoadWithUrl:(NSString *)url;
 
+- (void)supendDownloadWithUrl:(NSString *)url;
+
+- (void)cancelDownloadWithUrl:(NSString *)url;
+
+- (void)cancelAllDownloads;
 - (void)startAllDownloads;
+- (void)suspendAllDownloads;
 
 @end
 

+ 30 - 0
创维盒子/双子星云手机/Class/Set/uploadFile/mixDownloadManager/mixDownloadSession.m

@@ -28,10 +28,40 @@
     
 }
 
+#pragma mark - 操作任务接口
+- (void)startDownLoadWithUrl:(NSString *)url {
+    [self.queue operateDownloadWithUrl:url session:self.session handle:DownloadHandleTypeStart];
+}
+
+- (void)supendDownloadWithUrl:(NSString *)url {
+    [self.queue operateDownloadWithUrl:url session:self.session handle:DownloadHandleTypeSuspend];
+}
+
+- (void)cancelDownloadWithUrl:(NSString *)url {
+    [self.queue operateDownloadWithUrl:url session:self.session handle:DownloadHandleTypeCancel];
+}
+
+- (void)cancelAllDownloads {
+    
+    dispatch_async(dispatch_get_global_queue(0, 0), ^{
+        // 取消所有session的任务
+        // 耗时操作
+        // 会调用 URLSession:task:didCompleteWithError: 方法抛出error取消
+        [self.session invalidateAndCancel];
+        
+        //删除本地新鲜
+        [mixDownloadCacheManager clearMemoryAndDisk];
+    });
+}
+
 - (void)startAllDownloads {
     [self.queue startAllTasksWithSession:self.session];
 }
 
+- (void)suspendAllDownloads {
+    [self.queue suspendAllTasksWithSession:self.session];
+}
+
 #pragma mark - <NSURLSessionDataDelegate>
 
 // ssl 服务 证书信任

+ 0 - 30
创维盒子/双子星云手机/CloudPlayer/PlayerViewController+downloadNasFile.m

@@ -46,36 +46,6 @@
     }
     
     
-//    NSString *addFileName = @"";
-//    if (![item.downloadURL containsString:@".quicktime"]
-//        && [pathStr containsString:@".quicktime"])
-//    {
-//        addFileName = @".quicktime";
-//    }
-//    else if (![item.downloadURL containsString:@".octet-stream"]
-//             && [pathStr containsString:@".octet-stream"])
-//     {
-//         addFileName = @".octet-stream";
-//     }
-//    
-//    if(addFileName.length >0)
-//    {
-//        //文件名被添加了 .quicktime
-//        
-//        NSError *error = nil;
-//        NSFileManager *fileManager = [NSFileManager defaultManager];
-//        // 新文件名路径
-//        NSString *newFilePath = [pathStr stringByReplacingOccurrencesOfString:addFileName withString:@""];
-//        // 尝试移动(即重命名)文件
-//        BOOL success = [fileManager moveItemAtPath:pathStr toPath:newFilePath error:&error];
-//          
-//        if (success) {
-//            pathStr = newFilePath;
-//            NSLog(@"文件重命名成功!");
-//        } else {
-//            NSLog(@"文件重命名失败: %@", error);
-//        }
-//    }
     
     HLog(@"%@,",pathStr);
     if (nameArr.count >= 2) {