CCManageAsset.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 (tableAccount.cameraUpload) {
  38. dispatch_semaphore_t semaphoreGroup = dispatch_semaphore_create(0);
  39. [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  40. if (group == nil) {
  41. dispatch_semaphore_signal(semaphoreGroup);
  42. return;
  43. }
  44. NSUInteger nType = [[group valueForProperty:ALAssetsGroupPropertyType] intValue];
  45. if (nType == ALAssetsGroupSavedPhotos){
  46. [self.assetGroups addObject:group];
  47. self.assetsNewToUpload = [self getArrayNewAssetsFromGroup:group datePhoto:datePhoto dateVideo:dateVideo];
  48. }
  49. } failureBlock:^(NSError *error) {
  50. 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];
  51. [alert show];
  52. NSLog(@"[LOG] checkAssetsLibrary : Access error at camera roll %@", [error description]);
  53. dispatch_semaphore_signal(semaphoreGroup);
  54. }];
  55. while (dispatch_semaphore_wait(semaphoreGroup, DISPATCH_TIME_NOW))
  56. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  57. }
  58. }
  59. - (NSMutableArray *)getArrayNewAssetsFromGroup:(ALAssetsGroup *)group datePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo
  60. {
  61. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  62. if (!tableAccount.cameraUpload)
  63. return nil;
  64. NSMutableArray *tmpAssetsNew = [[NSMutableArray alloc] init];
  65. // Photo
  66. if (tableAccount.cameraUploadPhoto) {
  67. dispatch_semaphore_t semaphoreAsset = dispatch_semaphore_create(0);
  68. [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  69. NSDate *assetDate = [result valueForProperty:ALAssetPropertyDate];
  70. NSString *assetType = [result valueForProperty:ALAssetPropertyType];
  71. if ([assetDate compare:datePhoto] == NSOrderedDescending) {
  72. if ([assetType isEqualToString:@"ALAssetTypePhoto"]) {
  73. NSLog(@"[LOG] Insert new asset %@ - %@", assetDate, assetType);
  74. [tmpAssetsNew insertObject:result atIndex:0];
  75. }
  76. } else {
  77. dispatch_semaphore_signal(semaphoreAsset);
  78. *stop = YES;
  79. }
  80. }];
  81. while (dispatch_semaphore_wait(semaphoreAsset, DISPATCH_TIME_NOW))
  82. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  83. }
  84. // Video
  85. if (tableAccount.cameraUploadVideo) {
  86. dispatch_semaphore_t semaphoreAsset = dispatch_semaphore_create(0);
  87. [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  88. NSDate *assetDate = [result valueForProperty:ALAssetPropertyDate];
  89. NSString *assetType = [result valueForProperty:ALAssetPropertyType];
  90. if ([assetDate compare:dateVideo] == NSOrderedDescending) {
  91. if ([assetType isEqualToString:@"ALAssetTypeVideo"]) {
  92. NSLog(@"[LOG] Insert new asset %@ - %@", assetDate, assetType);
  93. [tmpAssetsNew insertObject:result atIndex:0];
  94. }
  95. } else {
  96. dispatch_semaphore_signal(semaphoreAsset);
  97. *stop = YES;
  98. }
  99. }];
  100. while (dispatch_semaphore_wait(semaphoreAsset, DISPATCH_TIME_NOW))
  101. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  102. }
  103. //NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
  104. return tmpAssetsNew; //[tmpAssetsNew sortedArrayUsingDescriptors:@[sort]];
  105. }
  106. - (void)removePhotoCameraRoll:(NSURL *)assetUrl
  107. {
  108. NSArray *urls = [[NSArray alloc] initWithObjects:assetUrl, nil];
  109. PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
  110. [library performChanges:^{
  111. PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:urls options:nil];
  112. [PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
  113. } completionHandler:^(BOOL success, NSError *error) {
  114. //do something here
  115. }];
  116. }
  117. @end