NCRequestAsset.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. NSDate *assetDate = asset.creationDate;
  27. __block NSError *error = nil;
  28. // VIDEO
  29. if (assetMediaType == PHAssetMediaTypeVideo) {
  30. @autoreleasepool {
  31. dispatch_semaphore_t semaphoreGroup = dispatch_semaphore_create(0);
  32. PHVideoRequestOptions *options = [PHVideoRequestOptions new];
  33. options.networkAccessAllowed = true;
  34. [[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:options resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
  35. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]])
  36. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName] error:nil];
  37. _exportSession = [[AVAssetExportSession alloc] initWithAsset:playerItem.asset presetName:AVAssetExportPresetHighestQuality];
  38. if (_exportSession) {
  39. _exportSession.outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryUser, fileName]];
  40. _exportSession.outputFileType = AVFileTypeQuickTimeMovie;
  41. [_exportSession exportAsynchronouslyWithCompletionHandler:^{
  42. dispatch_semaphore_signal(semaphoreGroup);
  43. if (AVAssetExportSessionStatusCompleted == _exportSession.status) {
  44. //OK selectorUploadAutoUpload
  45. if ([selector isEqualToString:selectorUploadAutoUpload]) {
  46. if ([self.delegate respondsToSelector:@selector(addDatabaseAutoUpload:assetDate:assetMediaType:)])
  47. [self.delegate addDatabaseAutoUpload:metadataNet assetDate:assetDate assetMediaType:assetMediaType];
  48. } else {
  49. if ([self.delegate respondsToSelector:@selector(upload:serverUrl:cryptated:template:onlyPlist:fileNameTemplate:assetLocalIdentifier:session:taskStatus:selector:selectorPost:errorCode:delegate:)])
  50. [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];
  51. }
  52. } else if (AVAssetExportSessionStatusFailed == _exportSession.status) {
  53. // Delete record on Table Auto Upload
  54. if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
  55. [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
  56. // Activity
  57. [[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];
  58. // Error for uploadFileFailure
  59. dispatch_async(dispatch_get_main_queue(), ^{
  60. if (delegate)
  61. if ([delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  62. [delegate uploadFileFailure:nil fileID:nil serverUrl:serverUrl selector:selector message:@"_read_file_error_" errorCode:[NSError errorWithDomain:@"it.twsweb.cryptocloud" code:kCFURLErrorFileDoesNotExist userInfo:nil].code];
  63. });
  64. } else {
  65. NSLog(@"Export Session Status: %ld", (long)_exportSession.status);
  66. }
  67. }];
  68. } else {
  69. dispatch_semaphore_signal(semaphoreGroup);
  70. // Delete record on Table Auto Upload
  71. if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
  72. [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
  73. // Activity
  74. [[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];
  75. // Error for uploadFileFailure
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. if (delegate)
  78. if ([delegate respondsToSelector:@selector(uploadFileFailure:fileID:serverUrl:selector:message:errorCode:)])
  79. [delegate uploadFileFailure:nil fileID:nil serverUrl:serverUrl selector:selector message:@"_read_file_error_" errorCode:[NSError errorWithDomain:@"it.twsweb.cryptocloud" code:kCFURLErrorFileDoesNotExist userInfo:nil].code];
  80. });
  81. }
  82. }];
  83. while (dispatch_semaphore_wait(semaphoreGroup, DISPATCH_TIME_NOW))
  84. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
  85. }
  86. }
  87. // IMAGE
  88. if (assetMediaType == PHAssetMediaTypeImage) {
  89. @autoreleasepool {
  90. PHImageRequestOptions *options = [PHImageRequestOptions new];
  91. options.synchronous = NO;
  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:assetDate:assetMediaType:)])
  110. [self.delegate addDatabaseAutoUpload:metadataNet assetDate:assetDate assetMediaType:assetMediaType];
  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. }];
  117. }
  118. }
  119. }
  120. @end