KMCloudUploadOperationQueue.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // KMCloudUploadOperationQueue.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by 万军 on 2020/2/21.
  6. // Copyright © 2020 WanJun. All rights reserved.
  7. //
  8. #import "KMCloudUploadOperationQueue.h"
  9. #import "KMCloudOperation.h"
  10. @interface KMCloudUploadOperationQueue ()
  11. @end
  12. @implementation KMCloudUploadOperationQueue
  13. - (void)dealloc {
  14. [[NSNotificationCenter defaultCenter] removeObserver:self name:KMServerCloudFileManagerUploadSuccessfulNotification object:nil];
  15. [[NSNotificationCenter defaultCenter] removeObserver:self name:KMServerCloudFileManagerUploadFailureNotification object:nil];
  16. }
  17. + (instancetype)sharedInstance {
  18. static KMCloudUploadOperationQueue *sharedInstance;
  19. static dispatch_once_t onceToken;
  20. dispatch_once(&onceToken, ^{
  21. sharedInstance = [KMCloudUploadOperationQueue new];
  22. sharedInstance.uploadingOperations = [NSMutableDictionary dictionary];
  23. });
  24. return sharedInstance;
  25. }
  26. #pragma mark - download
  27. - (void)addUploadOperation:(NSOperation *)op
  28. {
  29. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverCloudFileManagerUploadSuccessfulNotification:) name:KMServerCloudFileManagerUploadSuccessfulNotification object:nil];
  30. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(serverCloudFileManagerUploadFailureNotification:) name:KMServerCloudFileManagerUploadFailureNotification object:nil];
  31. if ([op isKindOfClass:[KMCloudOperation class]]) {
  32. KMCloudOperation *operation = (KMCloudOperation *)op;
  33. if (operation.serverType == KMDropbox) {
  34. [self addDropboxUploadOperation:(KMCloudOperation *)op];
  35. } else if (operation.serverType == KMGoogleDrive) {
  36. [self addGoogleDriveUploadOperation:(KMCloudOperation *)op];
  37. }
  38. }
  39. }
  40. - (void)addDropboxUploadOperation:(KMCloudOperation *)op {
  41. if ([self isUploadOperationExisting:op.filePath]) {
  42. NSLog(@"KMDropboxOperation exists %@", op);
  43. return;
  44. }
  45. if (![self.uploadingOperations objectForKey:op.filePath]) {
  46. @synchronized (self) {
  47. [self.uploadingOperations setObject:op forKey:op.filePath];
  48. }
  49. [self addOperation:op];
  50. }
  51. }
  52. - (void)addGoogleDriveUploadOperation:(KMCloudOperation *)op {
  53. if ([self isUploadOperationExisting:op.filePath]) {
  54. NSLog(@"KMGoogleDriveOperation exists %@", op);
  55. return;
  56. }
  57. if (![self.uploadingOperations objectForKey:op.filePath]) {
  58. @synchronized (self) {
  59. [self.uploadingOperations setObject:op forKey:op.filePath];
  60. }
  61. [self addOperation:op];
  62. }
  63. }
  64. - (NSOperation *)isUploadOperationExisting:(NSString *)urlString {
  65. NSLog(@"self.uploadingOperations.allKeys == %@",self.uploadingOperations.allKeys);
  66. for (NSString *url in self.uploadingOperations.allKeys) {
  67. if ([url isEqualToString:urlString]) {
  68. return [self.uploadingOperations objectForKey:url];
  69. }
  70. }
  71. return nil;
  72. }
  73. #pragma mark - cancel
  74. - (void)cancel:(NSString *)urlString {
  75. NSOperation *op = [self.uploadingOperations objectForKey:urlString];
  76. if (op) {
  77. @synchronized (self) {
  78. [self.uploadingOperations removeObjectForKey:urlString];
  79. }
  80. }
  81. }
  82. - (void)cancelAll {
  83. @synchronized (self) {
  84. [self.uploadingOperations removeAllObjects];
  85. }
  86. [self cancelAllOperations];
  87. }
  88. #pragma mark - NSNotificationCenter
  89. - (void)serverCloudFileManagerUploadSuccessfulNotification:(NSNotification *)notification {
  90. if (notification.object) {
  91. KMCloudOperation *operation = (KMCloudOperation *)notification.object;
  92. if ([self.uploadingOperations objectForKey:operation.filePath]) {
  93. @synchronized (self) {
  94. [self.uploadingOperations removeObjectForKey:operation.filePath];
  95. }
  96. }
  97. }
  98. }
  99. - (void)serverCloudFileManagerUploadFailureNotification:(NSNotification *)notification {
  100. if (notification.object) {
  101. KMCloudOperation *operation = (KMCloudOperation *)notification.object;
  102. if ([self.uploadingOperations objectForKey:operation.filePath]) {
  103. @synchronized (self) {
  104. [self.uploadingOperations removeObjectForKey:operation.filePath];
  105. }
  106. }
  107. }
  108. }
  109. @end