optimize file sharing

This commit is contained in:
Danmei Chen 2018-11-26 16:38:03 +01:00
parent 5ef016ebec
commit 4bbf4d8b9a
9 changed files with 88 additions and 62 deletions

View file

@ -85,8 +85,11 @@
- (IBAction)onEditionChangeClick:(id)sender;
- (IBAction)onEncryptedDevicesClick:(id)sender;
- (void)update;
- (void)openFile:(NSString *) filePath;
- (void)openFileWithURL:(NSURL *)url;
- (void)clearMessageView;
- (void)autoDownload:(LinphoneChatMessage *)message view:(ChatConversationView *)view inChat:(BOOL)inChat;
- (NSURL *)getICloudFileUrl:(NSString *)name;
- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL;
@end

View file

@ -997,20 +997,62 @@ void on_chat_room_conference_left(LinphoneChatRoom *cr, const LinphoneEventLog *
[PhoneMainView.instance popToView:view.compositeViewDescription];
}
- (void)openFile:(NSString *) filePath
- (void)openFileWithURL:(NSURL *)url
{
// Open the controller.
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:[NSURL fileURLWithPath:filePath]];
_documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:url];
_documentInteractionController.delegate = self;
BOOL canOpen = [_documentInteractionController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES];
//NO app can open the file
if (canOpen == NO) {
[[[UIAlertView alloc] initWithTitle:@"Info" message:@"There is no app found to open it" delegate:nil cancelButtonTitle:@"cancel" otherButtonTitles:nil, nil] show];
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", nil) message:NSLocalizedString(@"There is no app to open it.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:nil, nil] show];
}
}
- (NSURL *)getICloudFileUrl:(NSString *)name {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *icloudPath = [[fileManager URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"];
if (icloudPath) {
if (![fileManager fileExistsAtPath:icloudPath.path isDirectory:nil]) {
LOGI(@"Create directory");
[fileManager createDirectoryAtURL:icloudPath withIntermediateDirectories:YES attributes:nil error:nil];
}
return [icloudPath URLByAppendingPathComponent:name];
}
return nil;
}
- (void)writeFileInICloud:(NSData *)data fileURL:(NSURL *)fileURL {
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![[fileManager URLForUbiquityContainerIdentifier:nil]URLByAppendingPathComponent:@"Documents"]) {
//notify : set configuration to use icloud
[[[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Info", nil) message:NSLocalizedString(@"ICloud Drive is unavailable.", nil) delegate:nil cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:nil, nil] show];
return;
}
if ([fileManager isUbiquitousItemAtURL:fileURL]) {
// if it exists, replace the file
[data writeToURL:fileURL atomically:TRUE];
} else {
// get the url of localfile
NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileURL.lastPathComponent];
NSURL *localURL = nil;
if ([fileManager createFileAtPath:filePath contents:data attributes:nil]) {
localURL = [NSURL fileURLWithPath:filePath];
}
NSError *error;
if (![[NSFileManager defaultManager] setUbiquitous:YES itemAtURL:localURL destinationURL:fileURL error:&error]) {
LOGE(@"ICloud file error : %@", error);
}
}
}
- (void)deleteImageWithAssetId:(NSString *)assetId {
NSUInteger key = [_assetIdsArray indexOfObject:assetId];
[_imagesArray removeObjectAtIndex:key];

View file

@ -271,18 +271,8 @@
- (IBAction)onFileClick:(id)sender {
ChatConversationView *view = VIEW(ChatConversationView);
NSString *cachedFile = [LinphoneManager getMessageAppDataForKey:@"cachedfile" inMessage:self.message];
if (cachedFile) {
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:cachedFile]) {
[view openFile:cachedFile];
} else {
[self fileErrorBlock];
}
} else {
[LinphoneManager setValueInMessageAppData:@"onFileClick" forKey:@"icloudFileOption" inMessage:self.message];
[super getIcloudFiles];
}
NSString *name = [LinphoneManager getMessageAppDataForKey:@"localfile" inMessage:self.message];
[view openFileWithURL:[view getICloudFileUrl:name]];
}

View file

@ -51,7 +51,6 @@
- (void)setEvent:(LinphoneEventLog *)event;
- (void)setChatMessage:(LinphoneChatMessage *)message;
- (void)getIcloudFiles;
- (void)onDelete;
- (void)onResend;

View file

@ -262,36 +262,6 @@
[PhoneMainView.instance presentViewController:errView animated:YES completion:nil];
}
- (void)getIcloudFiles {
_documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"]
inMode:UIDocumentPickerModeImport];
_documentPicker.delegate = self;
_documentPicker.modalPresentationStyle = UIModalPresentationOverCurrentContext ;
ChatConversationView *view = VIEW(ChatConversationView);
[view presentViewController:_documentPicker animated:YES completion:nil];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
NSFileCoordinator *fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
[fileCoordinator coordinateReadingItemAtURL:url options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL * _Nonnull newURL) {
NSString *fileName = [newURL lastPathComponent];
NSData *data = [NSData dataWithContentsOfURL:newURL];
NSString *option = [LinphoneManager getMessageAppDataForKey:@"icloudFileOption" inMessage:self.message];
if ([option isEqualToString:@"onResend"])
[_chatRoomDelegate startFileUpload:data withName:fileName];
else if ([option isEqualToString:@"onFileClick"]) {
ChatConversationView *view = VIEW(ChatConversationView);
NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:data attributes:nil];
[view openFile:filePath];
}
}];
}
#pragma mark - Action Functions
- (void)onDelete {
@ -379,11 +349,9 @@
}];
} else if (localFile) {
[LinphoneManager setValueInMessageAppData:@"onResend" forKey:@"icloudFileOption" inMessage:_message];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL),
^(void) {
[self getIcloudFiles];
});
ChatConversationView *view = VIEW(ChatConversationView);
NSData *data = [NSData dataWithContentsOfURL:[view getICloudFileUrl:localFile]];
[_chatRoomDelegate startFileUpload:data withName:localFile];
}
} else {
[self onDelete];

View file

@ -173,20 +173,14 @@ static void linphone_iphone_file_transfer_recv(LinphoneChatMessage *message, con
[[LinphoneManager.instance fileTransferDelegates] removeObject:thiz];
NSString *key = @"localfile" ;
NSString *name =[NSString stringWithUTF8String:linphone_content_get_name(content)];
ChatConversationView *view = VIEW(ChatConversationView);
[LinphoneManager setValueInMessageAppData:@"saving..." forKey:key inMessage:message];
//write file to path
dispatch_async(dispatch_get_main_queue(), ^{
NSString *filePath = [[LinphoneManager cacheDirectory] stringByAppendingPathComponent:name];
//NSString *filePath = [LinphoneManager documentFile:name];
[[NSFileManager defaultManager] createFileAtPath:filePath
contents:thiz.data
attributes:nil];
[view writeFileInICloud:thiz.data fileURL:[view getICloudFileUrl:name]];
[LinphoneManager setValueInMessageAppData:name forKey:key inMessage:message];
[LinphoneManager setValueInMessageAppData:filePath forKey:@"cachedfile" inMessage:message];
[NSNotificationCenter.defaultCenter
postNotificationName:kLinphoneFileTransferRecvUpdate
object:thiz
@ -300,6 +294,10 @@ static LinphoneBuffer *linphone_iphone_file_transfer_send(LinphoneChatMessage *m
}
- (void)uploadFile:(NSData *)data forChatRoom:(LinphoneChatRoom *)chatRoom withName:(NSString *)name {
// we will write local files into ours folder of icloud
ChatConversationView *view = VIEW(ChatConversationView);
[view writeFileInICloud:data fileURL:[view getICloudFileUrl:name]];
[self uploadData:data forChatRoom:chatRoom type:@"file" subtype:nil name:name key:@"localfile" keyData:name qualityData:nil];
}

View file

@ -96,6 +96,18 @@
<string>Add tranfered files to your library</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Share photos with your friends and customize avatars</string>
<key>NSUbiquitousContainers</key>
<dict>
<key>iCloud.org.linphone.obiane</key>
<dict>
<key>NSUbiquitousContainerIsDocumentScopePublic</key>
<true/>
<key>NSUbiquitousContainerName</key>
<string>Obiane</string>
<key>NSUbiquitousContainerSupportedFolderLevels</key>
<string>Any</string>
</dict>
</dict>
<key>NSVoIPUsageDescription</key>
<string>Make audio/video calls</string>
<key>UIApplicationExitsOnSuspend</key>

View file

@ -4,6 +4,20 @@
<dict>
<key>aps-environment</key>
<string>development</string>
<key>com.apple.developer.icloud-container-identifiers</key>
<array>
<string>iCloud.$(CFBundleIdentifier)</string>
</array>
<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
</array>
<key>com.apple.developer.ubiquity-container-identifiers</key>
<array>
<string>iCloud.$(CFBundleIdentifier)</string>
</array>
<key>com.apple.developer.ubiquity-kvstore-identifier</key>
<string>$(TeamIdentifierPrefix)$(CFBundleIdentifier)</string>
<key>com.apple.security.application-groups</key>
<array>
<string>group.belledonne-communications.linphone.widget</string>

View file

@ -3652,7 +3652,7 @@
enabled = 1;
};
com.apple.iCloud = {
enabled = 0;
enabled = 1;
};
};
};