Selaa lähdekoodia

new class NCRequestAsset

Marino Faggiana 7 vuotta sitten
vanhempi
commit
aea3121c4e

+ 6 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -144,6 +144,7 @@
 		F7169A1E1EE590930086BD69 /* NCSharesCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = F7169A1B1EE590930086BD69 /* NCSharesCell.xib */; };
 		F720E01F1E48C73E001A4B9E /* CCActions.swift in Sources */ = {isa = PBXBuildFile; fileRef = F720E01E1E48C73E001A4B9E /* CCActions.swift */; };
 		F7226EDC1EE4089300EBECB1 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7226EDB1EE4089300EBECB1 /* Main.storyboard */; };
+		F723622C1EF3CD000012D1F9 /* NCRequestAsset.m in Sources */ = {isa = PBXBuildFile; fileRef = F723622B1EF3CD000012D1F9 /* NCRequestAsset.m */; };
 		F725437C1E12A44A009BF4C2 /* CCSection.m in Sources */ = {isa = PBXBuildFile; fileRef = F78F6FAF1CC8CCB700F4EA25 /* CCSection.m */; };
 		F72AAECA1E5C60C700BB17E1 /* AHKActionSheet.m in Sources */ = {isa = PBXBuildFile; fileRef = F72AAEC31E5C60C700BB17E1 /* AHKActionSheet.m */; };
 		F72AAECB1E5C60C700BB17E1 /* AHKActionSheetViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F72AAEC51E5C60C700BB17E1 /* AHKActionSheetViewController.m */; };
@@ -1135,6 +1136,8 @@
 		F72137301BAFF0920012B613 /* CCTemplates.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCTemplates.m; sourceTree = "<group>"; };
 		F7226EDB1EE4089300EBECB1 /* Main.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Main.storyboard; sourceTree = "<group>"; };
 		F7229B491DF71BB300E8C4E7 /* AUTHORS */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = AUTHORS; sourceTree = SOURCE_ROOT; };
+		F723622A1EF3CD000012D1F9 /* NCRequestAsset.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = NCRequestAsset.h; sourceTree = "<group>"; };
+		F723622B1EF3CD000012D1F9 /* NCRequestAsset.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NCRequestAsset.m; sourceTree = "<group>"; };
 		F72651E61ED03F4B002231E0 /* cryptocloud 10.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "cryptocloud 10.xcdatamodel"; sourceTree = "<group>"; };
 		F7296A661C8880ED001A7809 /* CCloadItemData.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CCloadItemData.swift; sourceTree = "<group>"; };
 		F72AAEC21E5C60C700BB17E1 /* AHKActionSheet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AHKActionSheet.h; sourceTree = "<group>"; };
@@ -3347,6 +3350,8 @@
 				F73049B91CB567F000C7C320 /* NSString+TruncateToWidth.m */,
 				F78071071EDAB65800EAFFF6 /* NSNotificationCenter+MainThread.h */,
 				F78071081EDAB65800EAFFF6 /* NSNotificationCenter+MainThread.m */,
+				F723622A1EF3CD000012D1F9 /* NCRequestAsset.h */,
+				F723622B1EF3CD000012D1F9 /* NCRequestAsset.m */,
 			);
 			path = Utility;
 			sourceTree = "<group>";
@@ -4680,6 +4685,7 @@
 				F7F06EBF1DBFACC600099AE9 /* CTAssetsPageViewController.m in Sources */,
 				F7F06EA11DBFACC600099AE9 /* CTAssetCollectionViewCell.m in Sources */,
 				F78964AF1EBB576C00403E13 /* JDStatusBarView.m in Sources */,
+				F723622C1EF3CD000012D1F9 /* NCRequestAsset.m in Sources */,
 				F7F54D0D1E5B14C800E19C62 /* MWZoomingScrollView.m in Sources */,
 				F762CB0B1EACB66200B38484 /* XLFormRowDescriptor.m in Sources */,
 				F762CB7A1EACB7D400B38484 /* UIXToolbarView.m in Sources */,

+ 44 - 0
iOSClient/Utility/NCRequestAsset.h

@@ -0,0 +1,44 @@
+//
+//  NCRequestAsset.h
+//  Nextcloud
+//
+//  Created by Marino Faggiana on 16/06/17.
+//  Copyright © 2017 TWS. All rights reserved.
+//
+//  Author Marino Faggiana <m.faggiana@twsweb.it>
+//
+//  This program is free software: you can redistribute it and/or modify
+//  it under the terms of the GNU General Public License as published by
+//  the Free Software Foundation, either version 3 of the License, or
+//  (at your option) any later version.
+//
+//  This program is distributed in the hope that it will be useful,
+//  but WITHOUT ANY WARRANTY; without even the implied warranty of
+//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+//  GNU General Public License for more details.
+//
+//  You should have received a copy of the GNU General Public License
+//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+//
+
+#import <Foundation/Foundation.h>
+#import <Photos/Photos.h>
+#import "CCNetworking.h"
+
+@protocol NCRequestAssetDelegate;
+
+@interface NCRequestAsset : NSObject
+
+@property (nonatomic, weak) id <NCRequestAssetDelegate> delegate;
+
+@property (nonatomic, strong) AVAssetExportSession *exportSession;
+
+- (void)writeAssetToSandbox:(NSString *)fileName assetLocalIdentifier:(NSString *)assetLocalIdentifier selector:(NSString *)selector metadataNet:(CCMetadataNet *)metadataNet;
+
+@end
+
+@protocol NCRequestAssetDelegate <NSObject>
+
+@optional - (void)addDatabaseAutoUpload:(CCMetadataNet *)metadataNet assetDate:(NSDate *)assetDate assetMediaType:(PHAssetMediaType)assetMediaType;
+
+@end

+ 112 - 0
iOSClient/Utility/NCRequestAsset.m

@@ -0,0 +1,112 @@
+//
+//  NCRequestAsset.m
+//  Nextcloud
+//
+//  Created by Marino Faggiana on 16/06/17.
+//  Copyright © 2017 TWS. All rights reserved.
+//
+
+#import "NCRequestAsset.h"
+#import "AppDelegate.h"
+
+#import "NCBridgeSwift.h"
+
+@implementation NCRequestAsset
+
+- (void)writeAssetToSandbox:(NSString *)fileName assetLocalIdentifier:(NSString *)assetLocalIdentifier selector:(NSString *)selector metadataNet:(CCMetadataNet *)metadataNet
+{
+    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
+    PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[assetLocalIdentifier] options:nil];
+    
+    PHAsset *asset = result[0];
+    PHAssetMediaType assetMediaType = asset.mediaType;
+    NSDate *assetDate = asset.creationDate;
+    __block NSError *error = nil;
+    
+    // VIDEO
+    if (assetMediaType == PHAssetMediaTypeVideo) {
+        
+        @autoreleasepool {
+            
+            PHVideoRequestOptions *options = [PHVideoRequestOptions new];
+            options.networkAccessAllowed = true;
+            
+            [[PHImageManager defaultManager] requestPlayerItemForVideo:asset options:options resultHandler:^(AVPlayerItem * _Nullable playerItem, NSDictionary * _Nullable info) {
+                
+                if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, fileName]])
+                    [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, fileName] error:nil];
+                
+                AVAssetExportSession *exportSession = [[AVAssetExportSession alloc] initWithAsset:playerItem.asset presetName:AVAssetExportPresetHighestQuality];
+                
+                if (exportSession) {
+                    
+                    exportSession.outputURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, fileName]];
+                    exportSession.outputFileType = AVFileTypeQuickTimeMovie;
+                    
+                    [exportSession exportAsynchronouslyWithCompletionHandler:^{
+                        
+                        if (AVAssetExportSessionStatusCompleted == exportSession.status) {
+                            
+                            //OK
+                            if ([self.delegate respondsToSelector:@selector(addDatabaseAutoUpload:assetDate:assetMediaType:)])
+                                [self.delegate addDatabaseAutoUpload:metadataNet assetDate:assetDate assetMediaType:assetMediaType];
+                            
+                        } else if (AVAssetExportSessionStatusFailed == exportSession.status) {
+                            
+                            // Delete record on Table Auto Upload
+                            if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
+                                [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
+                            
+                            [[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:appDelegate.activeUrl];
+                            
+                        } else {
+                            NSLog(@"Export Session Status: %ld", (long)exportSession.status);
+                        }
+                    }];
+                    
+                } else {
+                    
+                    // Delete record on Table Auto Upload
+                    if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
+                        [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
+                    
+                    [[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:appDelegate.activeUrl];
+                }
+            }];
+        }
+    }
+    
+    // IMAGE
+    if (assetMediaType == PHAssetMediaTypeImage) {
+        
+        @autoreleasepool {
+            
+            PHImageRequestOptions *options = [PHImageRequestOptions new];
+            options.synchronous = NO;
+            
+            [[PHImageManager defaultManager] requestImageDataForAsset:asset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
+                
+                [imageData writeToFile:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, fileName] options:NSDataWritingAtomic error:&error];
+                
+                if (error) {
+                    
+                    NSString *note = [NSString stringWithFormat:@"%@ [%@]",NSLocalizedString(@"_read_file_error_", nil), error.description];
+                    
+                    [[NCManageDatabase sharedInstance] addActivityClient:fileName fileID:assetLocalIdentifier action:k_activityDebugActionUpload selector:selector note:note type:k_activityTypeFailure verbose:k_activityVerboseDefault  activeUrl:app.activeUrl];
+                    
+                    // Delete record on Table Auto Upload
+                    if ([selector isEqualToString:selectorUploadAutoUpload] || [selector isEqualToString:selectorUploadAutoUploadAll])
+                        [[NCManageDatabase sharedInstance] deleteAutoUploadWithAssetLocalIdentifier:assetLocalIdentifier];
+                    
+                } else {
+                    
+                    //OK
+                    if ([self.delegate respondsToSelector:@selector(addDatabaseAutoUpload:assetDate:assetMediaType:)])
+                        [self.delegate addDatabaseAutoUpload:metadataNet assetDate:assetDate assetMediaType:assetMediaType];
+                }
+            }];
+        }
+    }
+}
+
+@end