CCManageAsset.m 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // CCManageAsset.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 23/07/15.
  6. // Copyright (c) 2014 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. @implementation CCManageAsset
  26. - (NSMutableArray *)getCameraRollNewItemsWithDatePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo
  27. {
  28. [self checkAssetsLibraryWithDatePhoto:datePhoto dateVideo:dateVideo];
  29. return self.assetsNewToUpload;
  30. }
  31. - (void)checkAssetsLibraryWithDatePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo
  32. {
  33. self.assetsNewToUpload = [[NSMutableArray alloc] init];
  34. ALAssetsLibrary *assetLibrary = [CCUtility defaultAssetsLibrary];
  35. if ([CCCoreData getCameraUploadActiveAccount:app.activeAccount]) {
  36. dispatch_semaphore_t semaphoreGroup = dispatch_semaphore_create(0);
  37. [assetLibrary enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
  38. if (group == nil) {
  39. dispatch_semaphore_signal(semaphoreGroup);
  40. return;
  41. }
  42. NSUInteger nType = [[group valueForProperty:ALAssetsGroupPropertyType] intValue];
  43. if (nType == ALAssetsGroupSavedPhotos){
  44. [self.assetGroups addObject:group];
  45. self.assetsNewToUpload = [self getArrayNewAssetsFromGroup:group datePhoto:datePhoto dateVideo:dateVideo];
  46. }
  47. } failureBlock:^(NSError *error) {
  48. 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];
  49. [alert show];
  50. NSLog(@"[LOG] checkAssetsLibrary : Access error at camera roll %@", [error description]);
  51. dispatch_semaphore_signal(semaphoreGroup);
  52. }];
  53. while (dispatch_semaphore_wait(semaphoreGroup, DISPATCH_TIME_NOW))
  54. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  55. }
  56. }
  57. - (NSMutableArray *)getArrayNewAssetsFromGroup:(ALAssetsGroup *)group datePhoto:(NSDate *)datePhoto dateVideo:(NSDate *)dateVideo
  58. {
  59. if (![CCCoreData getCameraUploadActiveAccount:app.activeAccount])
  60. return nil;
  61. NSMutableArray *tmpAssetsNew = [[NSMutableArray alloc] init];
  62. // Photo
  63. if ([CCCoreData getCameraUploadPhotoActiveAccount:app.activeAccount]) {
  64. dispatch_semaphore_t semaphoreAsset = dispatch_semaphore_create(0);
  65. [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  66. NSDate *assetDate = [result valueForProperty:ALAssetPropertyDate];
  67. NSString *assetType = [result valueForProperty:ALAssetPropertyType];
  68. if ([assetDate compare:datePhoto] == NSOrderedDescending) {
  69. if ([assetType isEqualToString:@"ALAssetTypePhoto"]) {
  70. NSLog(@"[LOG] Insert new asset %@ - %@", assetDate, assetType);
  71. [tmpAssetsNew insertObject:result atIndex:0];
  72. }
  73. } else {
  74. dispatch_semaphore_signal(semaphoreAsset);
  75. *stop = YES;
  76. }
  77. }];
  78. while (dispatch_semaphore_wait(semaphoreAsset, DISPATCH_TIME_NOW))
  79. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  80. }
  81. // Video
  82. if ([CCCoreData getCameraUploadVideoActiveAccount:app.activeAccount]) {
  83. dispatch_semaphore_t semaphoreAsset = dispatch_semaphore_create(0);
  84. [group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stop) {
  85. NSDate *assetDate = [result valueForProperty:ALAssetPropertyDate];
  86. NSString *assetType = [result valueForProperty:ALAssetPropertyType];
  87. if ([assetDate compare:dateVideo] == NSOrderedDescending) {
  88. if ([assetType isEqualToString:@"ALAssetTypeVideo"]) {
  89. NSLog(@"[LOG] Insert new asset %@ - %@", assetDate, assetType);
  90. [tmpAssetsNew insertObject:result atIndex:0];
  91. }
  92. } else {
  93. dispatch_semaphore_signal(semaphoreAsset);
  94. *stop = YES;
  95. }
  96. }];
  97. while (dispatch_semaphore_wait(semaphoreAsset, DISPATCH_TIME_NOW))
  98. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  99. }
  100. //NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"date" ascending:YES];
  101. return tmpAssetsNew; //[tmpAssetsNew sortedArrayUsingDescriptors:@[sort]];
  102. }
  103. - (void)removePhotoCameraRoll:(NSURL *)assetUrl
  104. {
  105. NSArray *urls = [[NSArray alloc] initWithObjects:assetUrl, nil];
  106. PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];
  107. [library performChanges:^{
  108. PHFetchResult *assetsToBeDeleted = [PHAsset fetchAssetsWithALAssetURLs:urls options:nil];
  109. [PHAssetChangeRequest deleteAssets:assetsToBeDeleted];
  110. } completionHandler:^(BOOL success, NSError *error) {
  111. //do something here
  112. }];
  113. }
  114. @end