mirror of
https://gitlab.linphone.org/BC/public/linphone-iphone.git
synced 2026-01-31 10:19:23 +00:00
Split Image sharing code
This commit is contained in:
parent
93e61518b5
commit
5159210394
7 changed files with 312 additions and 206 deletions
|
|
@ -25,16 +25,14 @@
|
|||
#import "HPGrowingTextView.h"
|
||||
#import "ChatModel.h"
|
||||
#import "ImagePickerViewController.h"
|
||||
#import "ImageSharing.h"
|
||||
|
||||
#include "linphonecore.h"
|
||||
|
||||
@interface ChatRoomViewController : UIViewController<HPGrowingTextViewDelegate, UICompositeViewDelegate, NSURLConnectionDataDelegate, ImagePickerDelegate> {
|
||||
@interface ChatRoomViewController : UIViewController<HPGrowingTextViewDelegate, UICompositeViewDelegate, ImagePickerDelegate, ImageSharingDelegate> {
|
||||
@private
|
||||
LinphoneChatRoom *chatRoom;
|
||||
NSURLConnection* uploadContext;
|
||||
NSURLConnection* downloadContext;
|
||||
NSMutableData* downloadedData;
|
||||
NSInteger totalBytesExpectedToRead;
|
||||
ImageSharing *imageSharing;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@
|
|||
self = [super initWithNibName:@"ChatRoomViewController" bundle:[NSBundle mainBundle]];
|
||||
if (self != nil) {
|
||||
self->chatRoom = NULL;
|
||||
self->imageSharing = NULL;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
|
@ -83,7 +84,6 @@
|
|||
}
|
||||
|
||||
|
||||
|
||||
#pragma mark - UICompositeViewDelegate Functions
|
||||
|
||||
static UICompositeViewDescription *compositeDescription = nil;
|
||||
|
|
@ -106,7 +106,6 @@ static UICompositeViewDescription *compositeDescription = nil;
|
|||
|
||||
#pragma mark - ViewController Functions
|
||||
|
||||
|
||||
- (void)viewDidLoad {
|
||||
[super viewDidLoad];
|
||||
|
||||
|
|
@ -120,7 +119,6 @@ static UICompositeViewDescription *compositeDescription = nil;
|
|||
messageField.font = [UIFont systemFontOfSize:18.0f];
|
||||
messageField.contentInset = UIEdgeInsetsZero;
|
||||
messageField.backgroundColor = [UIColor clearColor];
|
||||
[self enableTransferView:FALSE];
|
||||
[sendButton setEnabled:FALSE];
|
||||
}
|
||||
|
||||
|
|
@ -331,9 +329,9 @@ static void message_status(LinphoneChatMessage* msg,LinphoneChatMessageState sta
|
|||
|
||||
DTActionSheet *sheet = [[[DTActionSheet alloc] initWithTitle:NSLocalizedString(@"Incoming file stored to your photo library",nil)] autorelease];
|
||||
[sheet addButtonWithTitle:NSLocalizedString(@"Accept",nil) block:^(){
|
||||
[downloadContext release];
|
||||
downloadContext = [self downloadImageFrom:pendingFileUrl];
|
||||
[self startDownload];
|
||||
imageSharing = [ImageSharing imageSharingDownload:[NSURL URLWithString:pendingFileUrl] delegate:self];
|
||||
[footerView setHidden:TRUE];
|
||||
[transferView setHidden:FALSE];
|
||||
}];
|
||||
[sheet addCancelButtonWithTitle:NSLocalizedString(@"Ignore",nil)];
|
||||
[sheet showInView:self.view];
|
||||
|
|
@ -379,6 +377,7 @@ static void message_status(LinphoneChatMessage* msg,LinphoneChatMessageState sta
|
|||
forNinePatchNamed:@"chat_background"]];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Action Functions
|
||||
|
||||
- (IBAction)onBackClick:(id)event {
|
||||
|
|
@ -434,223 +433,92 @@ static void message_status(LinphoneChatMessage* msg,LinphoneChatMessageState sta
|
|||
}
|
||||
|
||||
- (IBAction)onTransferCancelClick:(id)event {
|
||||
if(uploadContext) {
|
||||
[uploadContext cancel];
|
||||
[self stopUpload];
|
||||
if(imageSharing) {
|
||||
[imageSharing cancel];
|
||||
}
|
||||
if(downloadContext) {
|
||||
[downloadContext cancel];
|
||||
[self stopDownload];
|
||||
}
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"File transfer interrupted by user"];
|
||||
}
|
||||
|
||||
- (void)enableTransferView:(BOOL)isTranfer {
|
||||
if (isTranfer) {
|
||||
[imageTransferProgressBar setProgress:0.0];
|
||||
} else {
|
||||
//[uploadContext cancel];
|
||||
}
|
||||
[footerView setHidden:isTranfer];
|
||||
[transferView setHidden:!isTranfer];
|
||||
[imageTransferProgressBar setHidden:!isTranfer];
|
||||
[cancelTransferButton setHidden:!isTranfer];
|
||||
[sendButton setEnabled:!isTranfer];
|
||||
}
|
||||
|
||||
- (void)startUpload {
|
||||
[self enableTransferView:TRUE];
|
||||
}
|
||||
|
||||
- (void)stopUpload {
|
||||
[self enableTransferView:FALSE];
|
||||
}
|
||||
|
||||
- (void)startDownload {
|
||||
[self enableTransferView:TRUE];
|
||||
}
|
||||
|
||||
- (void)stopDownload {
|
||||
[self enableTransferView:FALSE];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - NSURLConnectionDelegate
|
||||
#pragma mark ImageSharingDelegate
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
|
||||
if (connection == uploadContext) {
|
||||
[self stopUpload];
|
||||
NSString *serverUrl = [[LinphoneManager instance] lpConfigStringForKey:@"file_upload_url"];
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Cannot upload file to server [%@] because [%@]", serverUrl, [error localizedDescription]];
|
||||
UIAlertView* errorAlert = [UIAlertView alloc];
|
||||
- (void)imageSharingProgress:(ImageSharing*)aimageSharing progress:(float)progress {
|
||||
[imageTransferProgressBar setProgress:progress animated:FALSE];
|
||||
}
|
||||
|
||||
- (void)imageSharingAborted:(ImageSharing*)aimageSharing {
|
||||
[footerView setHidden:FALSE];
|
||||
[transferView setHidden:TRUE];
|
||||
imageSharing = NULL;
|
||||
}
|
||||
|
||||
- (void)imageSharingError:(ImageSharing*)aimageSharing error:(NSError *)error {
|
||||
[footerView setHidden:FALSE];
|
||||
[transferView setHidden:TRUE];
|
||||
NSString *url = [aimageSharing.connection.currentRequest.URL absoluteString];
|
||||
if (aimageSharing.upload) {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Cannot upload file to server [%@] because [%@]", url, [error localizedDescription]];
|
||||
UIAlertView* errorAlert = [UIAlertView alloc];
|
||||
[errorAlert initWithTitle:NSLocalizedString(@"Transfer error", nil)
|
||||
message:NSLocalizedString(@"Cannot transfer file to remote contact", nil)
|
||||
delegate:nil
|
||||
cancelButtonTitle:NSLocalizedString(@"Ok",nil)
|
||||
message:NSLocalizedString(@"Cannot transfer file to remote contact", nil)
|
||||
delegate:nil
|
||||
cancelButtonTitle:NSLocalizedString(@"Ok",nil)
|
||||
otherButtonTitles:nil ,nil];
|
||||
[errorAlert show];
|
||||
[errorAlert release];
|
||||
}else if (connection == downloadContext) {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Cannot dowanlod file from [%@] because [%@]", [connection.currentRequest.URL absoluteString], [error localizedDescription]];
|
||||
UIAlertView* errorAlert = [UIAlertView alloc];
|
||||
} else {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Cannot dowanlod file from [%@] because [%@]", url, [error localizedDescription]];
|
||||
UIAlertView* errorAlert = [UIAlertView alloc];
|
||||
[errorAlert initWithTitle:NSLocalizedString(@"Transfer error", nil)
|
||||
message:NSLocalizedString(@"Cannot transfer file from remote contact", nil)
|
||||
delegate:nil
|
||||
cancelButtonTitle:NSLocalizedString(@"Continue", nil)
|
||||
otherButtonTitles:nil, nil];
|
||||
delegate:nil
|
||||
cancelButtonTitle:NSLocalizedString(@"Continue", nil)
|
||||
otherButtonTitles:nil, nil];
|
||||
[errorAlert show];
|
||||
[errorAlert release];
|
||||
} else {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Invalid file transfer connection", connection];
|
||||
}
|
||||
imageSharing = NULL;
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
|
||||
[imageTransferProgressBar setProgress:(float)((float)totalBytesWritten/(float)totalBytesExpectedToWrite) animated:FALSE];
|
||||
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
|
||||
if (connection == uploadContext) {
|
||||
NSString* imageRemoteUrl=[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"File can be downloaded from [%@]", imageRemoteUrl];
|
||||
[self sendMessage:NSLocalizedString(@"Image sent",nil) withExterlBodyUrl:imageRemoteUrl];
|
||||
} else if (connection == downloadContext) {
|
||||
if (downloadedData == nil) downloadedData = [[NSMutableData alloc] initWithCapacity:[data length]];
|
||||
[downloadedData appendData:data];
|
||||
[imageTransferProgressBar setProgress:((float)downloadedData.length/(float)totalBytesExpectedToRead) animated:FALSE];
|
||||
} else {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Invalid file transfer connection", connection];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
||||
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
|
||||
int statusCode = httpResponse.statusCode;;
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"File transfer status code [%i]",statusCode];
|
||||
- (void)imageSharingUploadDone:(ImageSharing*)aimageSharing url:(NSURL*)url{
|
||||
[self sendMessage:NSLocalizedString(@"Image sent", nil) withExterlBodyUrl:[url absoluteString]];
|
||||
|
||||
if (connection == uploadContext) {
|
||||
if (statusCode == 200) {
|
||||
//nop
|
||||
} else if (statusCode >= 400) {
|
||||
UIAlertView* errorAlert = [UIAlertView alloc];
|
||||
[errorAlert initWithTitle:NSLocalizedString(@"Transfer error",nil)
|
||||
message:NSLocalizedString(@"Cannot transfer file to remote contact",nil)
|
||||
delegate:nil
|
||||
cancelButtonTitle:NSLocalizedString(@"Continue",nil)
|
||||
otherButtonTitles:nil ,nil];
|
||||
[errorAlert show];
|
||||
[errorAlert release];
|
||||
}
|
||||
} else if (connection == downloadContext) {
|
||||
if (statusCode == 200) {
|
||||
totalBytesExpectedToRead = [response expectedContentLength];
|
||||
} else if (statusCode >= 400) {
|
||||
UIAlertView* errorAlert = [UIAlertView alloc];
|
||||
[errorAlert initWithTitle:NSLocalizedString(@"Transfer error",nil)
|
||||
message:NSLocalizedString(@"Cannot transfer file from remote contact",nil)
|
||||
delegate:nil
|
||||
cancelButtonTitle:NSLocalizedString(@"Continue",nil)
|
||||
otherButtonTitles:nil ,nil];
|
||||
[errorAlert show];
|
||||
[errorAlert release];
|
||||
}
|
||||
} else {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Invalid file transfer connection", connection];
|
||||
}
|
||||
[footerView setHidden:FALSE];
|
||||
[transferView setHidden:TRUE];
|
||||
imageSharing = NULL;
|
||||
}
|
||||
|
||||
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
|
||||
if (connection == uploadContext) {
|
||||
[self stopUpload];
|
||||
uploadContext = nil;
|
||||
} else if (connection == downloadContext) {
|
||||
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
|
||||
[library writeImageDataToSavedPhotosAlbum:downloadedData
|
||||
metadata:nil
|
||||
completionBlock:^(NSURL *assetURL, NSError *error){
|
||||
if (error) {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Cannot save image data downloaded [%@]",[error localizedDescription]];
|
||||
} else {
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"Image saved to [%@]",[assetURL absoluteString]];
|
||||
}
|
||||
ImageViewController *controller = DYNAMIC_CAST([[PhoneMainView instance] changeCurrentView:[ImageViewController compositeViewDescription] push:TRUE], ImageViewController);
|
||||
if(controller != nil) {
|
||||
[controller setImage:[UIImage imageWithData:downloadedData]];
|
||||
}
|
||||
[downloadedData release];
|
||||
downloadedData = nil;
|
||||
}];
|
||||
|
||||
|
||||
[library release];
|
||||
[self stopDownload];
|
||||
downloadContext = nil;
|
||||
} else {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Invalid file transfer connection", connection];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSURLConnection*)downloadImageFrom:(NSString*)address {
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"downloading [%@]", address];
|
||||
NSURL* url = [NSURL URLWithString: address ];
|
||||
NSURLRequest* request = [NSURLRequest requestWithURL:url
|
||||
cachePolicy:NSURLRequestUseProtocolCachePolicy
|
||||
timeoutInterval:60.0];
|
||||
|
||||
return [[NSURLConnection alloc] initWithRequest:request delegate: self];
|
||||
- (void)imageSharingDownloadDone:(ImageSharing*)aimageSharing image:(UIImage *)image {
|
||||
[footerView setHidden:FALSE];
|
||||
[transferView setHidden:TRUE];
|
||||
|
||||
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
|
||||
[library writeImageToSavedPhotosAlbum:(CGImageRef)image
|
||||
metadata:nil
|
||||
completionBlock:^(NSURL *assetURL, NSError *error){
|
||||
if (error) {
|
||||
[LinphoneLogger log:LinphoneLoggerError format:@"Cannot save image data downloaded [%@]",[error localizedDescription]];
|
||||
} else {
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"Image saved to [%@]",[assetURL absoluteString]];
|
||||
}
|
||||
ImageViewController *controller = DYNAMIC_CAST([[PhoneMainView instance] changeCurrentView:[ImageViewController compositeViewDescription] push:TRUE], ImageViewController);
|
||||
if(controller != nil) {
|
||||
[controller setImage:image];
|
||||
}
|
||||
}];
|
||||
|
||||
|
||||
[library release];
|
||||
imageSharing = NULL;
|
||||
}
|
||||
|
||||
|
||||
- (NSURLConnection*)uploadImage:(UIImage*)image Named:(NSString*)name {
|
||||
/*
|
||||
turning the image into a NSData object
|
||||
getting the image back out of the UIImageView
|
||||
setting the quality to 90
|
||||
*/
|
||||
NSData *imageData = UIImageJPEGRepresentation(image, 80);
|
||||
// setting up the URL to post to
|
||||
NSString *urlString = [[LinphoneManager instance] lpConfigStringForKey:@"file_upload_url_preference"];
|
||||
|
||||
// setting up the request object now
|
||||
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
|
||||
[request setURL:[NSURL URLWithString:urlString]];
|
||||
[request setHTTPMethod:@"POST"];
|
||||
|
||||
/*
|
||||
add some header info now
|
||||
we always need a boundary when we post a file
|
||||
also we need to set the content type
|
||||
|
||||
You might want to generate a random boundary.. this is just the same
|
||||
as my output from wireshark on a valid html post
|
||||
*/
|
||||
NSString *boundary = @"---------------------------14737809831466499882746641449";
|
||||
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
|
||||
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
|
||||
|
||||
/*
|
||||
now lets create the body of the post
|
||||
*/
|
||||
NSMutableData *body = [NSMutableData data];
|
||||
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",name] dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
[body appendData:[NSData dataWithData:imageData]];
|
||||
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
// setting the body of the post to the reqeust
|
||||
[request setHTTPBody:body];
|
||||
|
||||
return [NSURLConnection connectionWithRequest:(NSURLRequest *)request delegate:self];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark ImpagePickerDelegate
|
||||
#pragma mark ImagePickerDelegate
|
||||
|
||||
- (void)imagePickerDelegateImage:(UIImage*)image {
|
||||
NSString *imageName = [NSString stringWithFormat:@"%i.jpg", [image hash]];
|
||||
uploadContext = [self uploadImage:image Named: imageName];
|
||||
[self startUpload];
|
||||
NSString *urlString = [[LinphoneManager instance] lpConfigStringForKey:@"file_upload_url_preference"];
|
||||
imageSharing = [ImageSharing imageSharingUpload:[NSURL URLWithString:urlString] image:image delegate:self];
|
||||
[footerView setHidden:TRUE];
|
||||
[transferView setHidden:FALSE];
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
/* ContactDetailsImagePickerController.h
|
||||
/* ImagePickerViewController.h
|
||||
*
|
||||
* Copyright (C) 2012 Belledonne Comunications, Grenoble, France
|
||||
*
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
/* ContactDetailsImagePickerController.m
|
||||
/* ImagePickerViewController.m
|
||||
*
|
||||
* Copyright (C) 2012 Belledonne Comunications, Grenoble, France
|
||||
*
|
||||
|
|
|
|||
50
Classes/ImageSharing.h
Normal file
50
Classes/ImageSharing.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
/* ImageSharing.h
|
||||
*
|
||||
* Copyright (C) 2012 Belledonne Comunications, Grenoble, France
|
||||
*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@class ImageSharing;
|
||||
|
||||
@protocol ImageSharingDelegate <NSObject>
|
||||
|
||||
- (void)imageSharingProgress:(ImageSharing*)imageSharing progress:(float)progress;
|
||||
- (void)imageSharingAborted:(ImageSharing*)imageSharing;
|
||||
- (void)imageSharingError:(ImageSharing*)imageSharing error:(NSError *)error;
|
||||
- (void)imageSharingUploadDone:(ImageSharing*)imageSharing url:(NSURL*)url;
|
||||
- (void)imageSharingDownloadDone:(ImageSharing*)imageSharing image:(UIImage *)image;
|
||||
|
||||
@end
|
||||
|
||||
@interface ImageSharing : NSObject<NSURLConnectionDataDelegate> {
|
||||
@private
|
||||
NSInteger totalBytesExpectedToRead;
|
||||
id<ImageSharingDelegate> delegate;
|
||||
int statusCode;
|
||||
}
|
||||
|
||||
+ (id)imageSharingUpload:(NSURL*)url image:(UIImage*)image delegate:(id<ImageSharingDelegate>)delegate;
|
||||
+ (id)imageSharingDownload:(NSURL*)url delegate:(id<ImageSharingDelegate>)delegate;
|
||||
|
||||
- (void)cancel;
|
||||
|
||||
@property (nonatomic, readonly) BOOL upload;
|
||||
@property (nonatomic, readonly) NSMutableData* data;
|
||||
@property (nonatomic, readonly) NSURLConnection* connection;
|
||||
|
||||
@end
|
||||
182
Classes/ImageSharing.m
Normal file
182
Classes/ImageSharing.m
Normal file
|
|
@ -0,0 +1,182 @@
|
|||
/* ImageSharing.m
|
||||
*
|
||||
* Copyright (C) 2012 Belledonne Comunications, Grenoble, France
|
||||
*
|
||||
* 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 2 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, write to the Free Software
|
||||
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||
*/
|
||||
|
||||
#import "ImageSharing.h"
|
||||
#import "Utils.h"
|
||||
#import "LinphoneManager.h"
|
||||
|
||||
@implementation ImageSharing
|
||||
|
||||
@synthesize connection;
|
||||
@synthesize data;
|
||||
@synthesize upload;
|
||||
|
||||
|
||||
#pragma mark - Lifecycle Functions
|
||||
|
||||
+ (id)imageSharingUpload:(NSURL*)url image:(UIImage*)image delegate:(id<ImageSharingDelegate>)delegate {
|
||||
ImageSharing *imgs = [[ImageSharing alloc] init];
|
||||
if(imgs != nil) {
|
||||
imgs->upload = TRUE;
|
||||
imgs->delegate = delegate;
|
||||
imgs->data = [[NSMutableData alloc] init];
|
||||
if(delegate) {
|
||||
[delegate imageSharingProgress:imgs progress:0];
|
||||
}
|
||||
[imgs uploadImageTo:url image:image];
|
||||
}
|
||||
return imgs;
|
||||
}
|
||||
|
||||
+ (id)imageSharingDownload:(NSURL*)url delegate:(id<ImageSharingDelegate>)delegate {
|
||||
ImageSharing *imgs = [[ImageSharing alloc] init];
|
||||
if(imgs != nil) {
|
||||
imgs->upload = FALSE;
|
||||
imgs->delegate = delegate;
|
||||
imgs->data = [[NSMutableData alloc] init];
|
||||
if(delegate) {
|
||||
[delegate imageSharingProgress:imgs progress:0];
|
||||
}
|
||||
[imgs downloadImageFrom:url];
|
||||
}
|
||||
return imgs;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
[connection release];
|
||||
[data release];
|
||||
[super dealloc];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark -
|
||||
|
||||
- (void)cancel {
|
||||
[connection cancel];
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"File transfer [%@] interrupted by user", [connection.currentRequest.URL absoluteString]];
|
||||
if(delegate) {
|
||||
[delegate imageSharingAborted:self];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (void)downloadImageFrom:(NSURL*)url {
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"downloading [%@]", [url absoluteString]];
|
||||
|
||||
NSURLRequest* request = [NSURLRequest requestWithURL:url
|
||||
cachePolicy:NSURLRequestUseProtocolCachePolicy
|
||||
timeoutInterval:60.0];
|
||||
|
||||
connection = [[NSURLConnection alloc] initWithRequest:request delegate: self];
|
||||
}
|
||||
|
||||
|
||||
- (void)uploadImageTo:(NSURL*)url image:(UIImage*)image {
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"downloading [%@]", [url absoluteString]];
|
||||
|
||||
// setting up the request object now
|
||||
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
|
||||
[request setURL:url];
|
||||
[request setHTTPMethod:@"POST"];
|
||||
|
||||
/*
|
||||
add some header info now
|
||||
we always need a boundary when we post a file
|
||||
also we need to set the content type
|
||||
|
||||
You might want to generate a random boundary.. this is just the same
|
||||
as my output from wireshark on a valid html post
|
||||
*/
|
||||
NSString *boundary = @"---------------------------14737809831466499882746641449";
|
||||
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
|
||||
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
|
||||
|
||||
/*
|
||||
now lets create the body of the post
|
||||
*/
|
||||
NSMutableData *body = [NSMutableData data];
|
||||
NSString *imageName = [NSString stringWithFormat:@"%i.jpg", [image hash]];
|
||||
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n",imageName] dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
[body appendData:[NSData dataWithData:UIImageJPEGRepresentation(image, 1.0)]];
|
||||
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
|
||||
[request setHTTPBody:body];
|
||||
|
||||
connection = [[NSURLConnection alloc] initWithRequest:(NSURLRequest *)request delegate:self];
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - NSURLConnectionDelegate
|
||||
|
||||
- (void)connection:(NSURLConnection *)aconnection didFailWithError:(NSError *)error {
|
||||
if(delegate) {
|
||||
[delegate imageSharingError:self error:error];
|
||||
}
|
||||
[self release];
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
|
||||
if(upload && delegate) {
|
||||
[delegate imageSharingProgress:self progress:(float)totalBytesWritten/(float)totalBytesExpectedToWrite];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)adata {
|
||||
[data appendData:adata];
|
||||
if(!upload && delegate) {
|
||||
[delegate imageSharingProgress:self progress:(float)data.length/(float)totalBytesExpectedToRead];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
|
||||
NSHTTPURLResponse * httpResponse = (NSHTTPURLResponse *) response;
|
||||
statusCode = httpResponse.statusCode;
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"File transfer status code [%i]",statusCode];
|
||||
|
||||
if (statusCode == 200 && !upload) {
|
||||
totalBytesExpectedToRead = [response expectedContentLength];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
|
||||
if(statusCode >= 400) {
|
||||
NSError *error = [NSError errorWithDomain:@"ImageSharing" code:statusCode userInfo:nil];
|
||||
if(delegate) {
|
||||
[delegate imageSharingError:self error:error];
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (upload) {
|
||||
NSString* imageRemoteUrl = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"File can be downloaded from [%@]", imageRemoteUrl];
|
||||
if(delegate) {
|
||||
[delegate imageSharingUploadDone:self url:[NSURL URLWithString:imageRemoteUrl]];
|
||||
}
|
||||
} else {
|
||||
UIImage* image = [UIImage imageWithData:data];
|
||||
[LinphoneLogger log:LinphoneLoggerLog format:@"File downloaded"];
|
||||
if(delegate) {
|
||||
[delegate imageSharingDownloadDone:self image:image];
|
||||
}
|
||||
}
|
||||
[self release];
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
@ -652,6 +652,8 @@
|
|||
D36FB2D51589EF7C0036F6F2 /* UIPauseButton.m in Sources */ = {isa = PBXBuildFile; fileRef = D36FB2D41589EF7C0036F6F2 /* UIPauseButton.m */; };
|
||||
D36FB2D61589EF7C0036F6F2 /* UIPauseButton.m in Sources */ = {isa = PBXBuildFile; fileRef = D36FB2D41589EF7C0036F6F2 /* UIPauseButton.m */; };
|
||||
D37295DB158B3C9600D2C0C7 /* video_off_disabled.png in Resources */ = {isa = PBXBuildFile; fileRef = D37295DA158B3C9600D2C0C7 /* video_off_disabled.png */; };
|
||||
D374D3FD16071762003D25FF /* ImageSharing.m in Sources */ = {isa = PBXBuildFile; fileRef = D374D3FC16071762003D25FF /* ImageSharing.m */; };
|
||||
D374D3FE16071762003D25FF /* ImageSharing.m in Sources */ = {isa = PBXBuildFile; fileRef = D374D3FC16071762003D25FF /* ImageSharing.m */; };
|
||||
D377BBFA15A19DA6002B696B /* video_on_disabled.png in Resources */ = {isa = PBXBuildFile; fileRef = D377BBF915A19DA6002B696B /* video_on_disabled.png */; };
|
||||
D378906515AC373B00BD776C /* ContactDetailsLabelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D378906315AC373B00BD776C /* ContactDetailsLabelViewController.m */; };
|
||||
D378906615AC373B00BD776C /* ContactDetailsLabelViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D378906315AC373B00BD776C /* ContactDetailsLabelViewController.m */; };
|
||||
|
|
@ -1704,6 +1706,8 @@
|
|||
D36FB2D31589EF7C0036F6F2 /* UIPauseButton.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIPauseButton.h; sourceTree = "<group>"; };
|
||||
D36FB2D41589EF7C0036F6F2 /* UIPauseButton.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIPauseButton.m; sourceTree = "<group>"; };
|
||||
D37295DA158B3C9600D2C0C7 /* video_off_disabled.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = video_off_disabled.png; path = Resources/video_off_disabled.png; sourceTree = "<group>"; };
|
||||
D374D3FB16071762003D25FF /* ImageSharing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageSharing.h; sourceTree = "<group>"; };
|
||||
D374D3FC16071762003D25FF /* ImageSharing.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageSharing.m; sourceTree = "<group>"; };
|
||||
D377BBF915A19DA6002B696B /* video_on_disabled.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; name = video_on_disabled.png; path = Resources/video_on_disabled.png; sourceTree = "<group>"; };
|
||||
D378906215AC373B00BD776C /* ContactDetailsLabelViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ContactDetailsLabelViewController.h; sourceTree = "<group>"; };
|
||||
D378906315AC373B00BD776C /* ContactDetailsLabelViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ContactDetailsLabelViewController.m; sourceTree = "<group>"; };
|
||||
|
|
@ -2266,6 +2270,8 @@
|
|||
D38187D415FE346B00C3EDCA /* HistoryViewController.xib */,
|
||||
D378AB2815DCDB480098505D /* ImagePickerViewController.h */,
|
||||
D378AB2915DCDB490098505D /* ImagePickerViewController.m */,
|
||||
D374D3FB16071762003D25FF /* ImageSharing.h */,
|
||||
D374D3FC16071762003D25FF /* ImageSharing.m */,
|
||||
22405EFD1601C19000B92522 /* ImageViewController.h */,
|
||||
22405EFE1601C19100B92522 /* ImageViewController.m */,
|
||||
D37EE11016035793003608A6 /* ImageViewController.xib */,
|
||||
|
|
@ -4463,6 +4469,7 @@
|
|||
D3ED40191602172200BF332B /* HPGrowingTextView.m in Sources */,
|
||||
D3ED401B1602172200BF332B /* HPTextViewInternal.m in Sources */,
|
||||
D37EE162160377D7003608A6 /* DTActionSheet.m in Sources */,
|
||||
D374D3FD16071762003D25FF /* ImageSharing.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
@ -4557,6 +4564,7 @@
|
|||
D3ED401A1602172200BF332B /* HPGrowingTextView.m in Sources */,
|
||||
D3ED401C1602172200BF332B /* HPTextViewInternal.m in Sources */,
|
||||
D37EE163160377D7003608A6 /* DTActionSheet.m in Sources */,
|
||||
D374D3FE16071762003D25FF /* ImageSharing.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue