CCManageAsset.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. //
  2. // CCManageAsset.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/07/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCManageAsset.h"
  24. #import "AppDelegate.h"
  25. #import "NCBridgeSwift.h"
  26. @implementation CCManageAsset
  27. - (NSMutableArray *)getCameraRollNewItemsWithDatePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo
  28. {
  29. [self checkAssetsLibraryWithDatePhoto:datePhoto dateVideo:dateVideo];
  30. return self.assetsNewToUpload;
  31. }
  32. - (void)checkAssetsLibraryWithDatePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo
  33. {
  34. self.assetsNewToUpload = [[NSMutableArray alloc] init];
  35. ALAssetsLibrary *assetLibrary = [CCUtility defaultAssetsLibrary];
  36. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  37. //if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  38. if (tableAccount.cameraUpload) {
  39. dispatch_semaphore_t semaphoreGroup = dispatch_semaphore_create(0);
  40. [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  41. if (group == nil) {
  42. dispatch_semaphore_signal(semaphoreGroup);
  43. return;
  44. }
  45. NSUInteger nType = [[group valueForProperty:ALAssetsGroupPropertyType] intValue];
  46. if (nType == ALAssetsGroupSavedPhotos){
  47. [self.assetGroups addObject:group];
  48. self.assetsNewToUpload = [self getArrayNewAssetsFromGroup:group datePhoto:datePhoto dateVideo:dateVideo];
  49. }
  50. } failureBlock:^(NSError *error) {
  51. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_access_photo_not_enabled_", nil) message: NSLocalizedString(@"_access_photo_not_enabled_msg_", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"_ok_", nil) otherButtonTitles:nil];
  52. [alert show];
  53. NSLog(@"[LOG] checkAssetsLibrary : Access error at camera roll %@", [error description]);
  54. dispatch_semaphore_signal(semaphoreGroup);
  55. }];
  56. while (dispatch_semaphore_wait(semaphoreGroup, DISPATCH_TIME_NOW))
  57. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  58. }
  59. }
  60. - (NSMutableArray *)getArrayNewAssetsFromGroup:(ALAssetsGroup *)group datePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo
  61. {
  62. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  63. if (!tableAccount.cameraUpload)
  64. return nil;
  65. // if (![CCCoreData getCameraUploadActiveAccount:app.activeAccount])
  66. // return nil;
  67. NSMutableArray *tmpAssetsNew = [[NSMutableArray alloc] init];
  68. // Photo
  69. //if ([CCCoreData getCameraUploadPhotoActiveAccount:app.activeAccount]) {
  70. if (tableAccount.cameraUploadPhoto) {
  71. dispatch_semaphore_t semaphoreAsset = dispatch_semaphore_create(0);
  72. [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  73. NSDate *assetDate = [result valueForProperty:ALAssetPropertyDate];
  74. NSString *assetType = [result valueForProperty:ALAssetPropertyType];
  75. if ([assetDate compare:datePhoto] == NSOrderedDescending) {
  76. if ([assetType isEqualToString:@"ALAssetTypePhoto"]) {
  77. NSLog(@"[LOG] Insert new asset %@ - %@", assetDate, assetType);
  78. [tmpAssetsNew insertObject:result atIndex:0];
  79. }
  80. } else {
  81. dispatch_semaphore_signal(semaphoreAsset);
  82. *stop = YES;
  83. }
  84. }];
  85. while (dispatch_semaphore_wait(semaphoreAsset, DISPATCH_TIME_NOW))
  86. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  87. }
  88. // Video
  89. //if ([CCCoreData getCameraUploadVideoActiveAccount:app.activeAccount]) {
  90. if (tableAccount.cameraUploadVideo) {
  91. dispatch_semaphore_t semaphoreAsset = dispatch_semaphore_create(0);
  92. [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  93. NSDate *assetDate = [result valueForProperty:ALAssetPropertyDate];
  94. NSString *assetType = [result valueForProperty:ALAssetPropertyType];
  95. if ([assetDate compare:dateVideo] == NSOrderedDescending) {
  96. if ([assetType isEqualToString:@"ALAssetTypeVideo"]) {
  97. NSLog(@"[LOG] Insert new asset %@ - %@", assetDate, assetType);
  98. [tmpAssetsNew insertObject:result atIndex:0];
  99. }
  100. } else {
  101. dispatch_semaphore_signal(semaphoreAsset);
  102. *stop = YES;
  103. }
  104. }];
  105. while (dispatch_semaphore_wait(semaphoreAsset, DISPATCH_TIME_NOW))
  106. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  107. }
  108. //NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
  109. return tmpAssetsNew; //[tmpAssetsNew sortedArrayUsingDescriptors:@[sort]];
  110. }
  111. - (void)removePhotoCameraRoll:(NSURL *)assetUrl
  112. {
  113. NSArray *urls = [[NSArray alloc] initWithObjects:assetUrl, nil];
  114. PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
  115. [library performChanges:^{
  116. PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:urls options:nil];
  117. [PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
  118. } completionHandler:^(BOOL success, NSError *error) {
  119. //do something here
  120. }];
  121. }
  122. @end