NCRequestAsset.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. //
  2. // NCRequestAsset.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/06/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. #import "NCRequestAsset.h"
  9. #import "AppDelegate.h"
  10. #import "NCBridgeSwift.h"
  11. @implementation NCRequestAsset
  12. - (void)writeAssetToSandboxFileName:(NSString *)fileName assetLocalIdentifier:(NSString *)assetLocalIdentifier selector:(NSString *)selector selectorPost:(NSString *)selectorPost errorCode:(NSInteger)errorCode metadataNet:(CCMetadataNet *)metadataNet serverUrl:(NSString *)serverUrl activeUrl:(NSString *)activeUrl directoryUser:(NSString *)directoryUser cryptated:(BOOL)cryptated session:(NSString *)session taskStatus:(NSInteger)taskStatus delegate:(id)delegate
  13. {
  14. PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil];
  15. if (!result.count) {
  16. [[NCManageDatabase sharedInstance] addActivityClient:fileName fileID:assetLocalIdentifier action:k_activityDebugActionUpload selector:selector note:@"Internal error image/video not found" type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:activeUrl];
  17. dispatch_async(dispatch_get_main_queue(), ^{
  18. if (delegate)
  19. if ([delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  20. [delegate uploadFileFailure:nil fileID:nil serverUrl:serverUrl selector:selector message:@"Internal error image/video not found" errorCode: k_CCErrorInternalError];
  21. });
  22. return;
  23. }
  24. PHAsset *asset = result[0];
  25. PHAssetMediaType assetMediaType = asset.mediaType;
  26. __block NSError *error = nil;
  27. // VIDEO
  28. if (assetMediaType == PHAssetMediaTypeVideo) {
  29. @autoreleasepool {
  30. dispatch_semaphore_t semaphoreGroup = dispatch_semaphore_create(0);
  31. PHVideoRequestOptions *options = [PHVideoRequestOptions new];
  32. options.networkAccessAllowed = YES; // iCloud
  33. [[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:options resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
  34. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]])
  35. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName] error:nil];
  36. AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:playerItem.asset presetName:AVAssetExportPresetHighestQuality];
  37. if (exportSession) {
  38. exportSession.outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]];
  39. exportSession.outputFileType = AVFileTypeQuickTimeMovie;
  40. [exportSession exportAsynchronouslyWithCompletionHandler:^{
  41. dispatch_semaphore_signal(semaphoreGroup);
  42. if (AVAssetExportSessionStatusCompleted == exportSession.status) {
  43. //OK selectorUploadAutoUpload
  44. if ([selector isEqualToString:selectorUploadAutoUpload]) {
  45. if ([self.delegate respondsToSelector:@selector(addDatabaseAutoUpload:asset:)])
  46. [self.delegate addDatabaseAutoUpload:metadataNet asset:asset];
  47. } else {
  48. if ([self.delegate respondsToSelector:@selector(upload:serverUrl:cryptated:template:onlyPlist:fileNameTemplate:assetLocalIdentifier:session:taskStatus:selector:selectorPost:errorCode:delegate:)])
  49. [self.delegate upload:fileName serverUrl:serverUrl cryptated:cryptated template:NO onlyPlist:NO fileNameTemplate:nil assetLocalIdentifier:assetLocalIdentifier session:session taskStatus:taskStatus selector:selector selectorPost:selectorPost errorCode:errorCode delegate:delegate];
  50. }
  51. } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
  52. // Delete record on Table Auto Upload
  53. if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
  54. [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
  55. // Activity
  56. [[NCManageDatabase sharedInstance] addActivityClient:fileName fileID:assetLocalIdentifier action:k_activityDebugActionUpload selector:selector note:[NSString stringWithFormat:@"%@ [%@]",NSLocalizedString(@"_read_file_error_", nil), error.description] type:k_activityTypeFailure verbose:k_activityVerboseDefault activeUrl:activeUrl];
  57. // Error for uploadFileFailure
  58. dispatch_async(dispatch_get_main_queue(), ^{
  59. if (delegate)
  60. if ([delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  61. [delegate uploadFileFailure:nil fileID:nil serverUrl:serverUrl selector:selector message:@"_read_file_error_" errorCode:[NSError errorWithDomain:@"it.twsweb.cryptocloud" code:kCFURLErrorFileDoesNotExist userInfo:nil].code];
  62. });
  63. } else {
  64. NSLog(@"Export Session Status: %ld", (long)exportSession.status);
  65. }
  66. }];
  67. } else {
  68. dispatch_semaphore_signal(semaphoreGroup);
  69. // Delete record on Table Auto Upload
  70. if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
  71. [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
  72. // Activity
  73. [[NCManageDatabase sharedInstance] addActivityClient:fileName fileID:assetLocalIdentifier action:k_activityDebugActionUpload selector:selector note:[NSString stringWithFormat:@"%@ [%@]",NSLocalizedString(@"_read_file_error_", nil), error.description] type:k_activityTypeFailure verbose:k_activityVerboseDefault activeUrl:activeUrl];
  74. // Error for uploadFileFailure
  75. dispatch_async(dispatch_get_main_queue(), ^{
  76. if (delegate)
  77. if ([delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  78. [delegate uploadFileFailure:nil fileID:nil serverUrl:serverUrl selector:selector message:@"_read_file_error_" errorCode:[NSError errorWithDomain:@"it.twsweb.cryptocloud" code:kCFURLErrorFileDoesNotExist userInfo:nil].code];
  79. });
  80. }
  81. }];
  82. while (dispatch_semaphore_wait(semaphoreGroup, DISPATCH_TIME_NOW))
  83. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  84. }
  85. }
  86. // IMAGE
  87. if (assetMediaType == PHAssetMediaTypeImage) {
  88. @autoreleasepool {
  89. dispatch_semaphore_t semaphoreGroup = dispatch_semaphore_create(0);
  90. PHImageRequestOptions *options = [PHImageRequestOptions new];
  91. options.networkAccessAllowed = YES; // iCloud
  92. [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
  93. [imageData writeToFile:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName] options:NSDataWritingAtomic error:&error];
  94. if (error) {
  95. // Delete record on Table Auto Upload
  96. if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
  97. [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
  98. // Activity
  99. [[NCManageDatabase sharedInstance] addActivityClient:fileName fileID:assetLocalIdentifier action:k_activityDebugActionUpload selector:selector note:[NSString stringWithFormat:@"%@ [%@]",NSLocalizedString(@"_read_file_error_", nil), error.description] type:k_activityTypeFailure verbose:k_activityVerboseDefault activeUrl:activeUrl];
  100. // Error for uploadFileFailure
  101. dispatch_async(dispatch_get_main_queue(), ^{
  102. if (delegate)
  103. if ([delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  104. [delegate uploadFileFailure:nil fileID:nil serverUrl:serverUrl selector:selector message:@"_read_file_error_" errorCode:[NSError errorWithDomain:@"it.twsweb.cryptocloud" code:kCFURLErrorFileDoesNotExist userInfo:nil].code];
  105. });
  106. } else {
  107. //OK selectorUploadAutoUpload
  108. if ([selector isEqualToString:selectorUploadAutoUpload]) {
  109. if ([self.delegate respondsToSelector:@selector(addDatabaseAutoUpload:asset:)])
  110. [self.delegate addDatabaseAutoUpload:metadataNet asset:asset];
  111. } else {
  112. if ([self.delegate respondsToSelector:@selector(upload:serverUrl:cryptated:template:onlyPlist:fileNameTemplate:assetLocalIdentifier:session:taskStatus:selector:selectorPost:errorCode:delegate:)])
  113. [self.delegate upload:fileName serverUrl:serverUrl cryptated:cryptated template:NO onlyPlist:NO fileNameTemplate:nil assetLocalIdentifier:assetLocalIdentifier session:session taskStatus:taskStatus selector:selector selectorPost:selectorPost errorCode:errorCode delegate:delegate];
  114. }
  115. }
  116. dispatch_semaphore_signal(semaphoreGroup);
  117. }];
  118. while (dispatch_semaphore_wait(semaphoreGroup, DISPATCH_TIME_NOW))
  119. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  120. }
  121. }
  122. }
  123. @end