Plugins/Image Uploading Plugin/AIGenericMultipartImageUploader.m
author Evan Schoenberg
Thu, 14 Nov 2019 21:09:06 -0500
branchadium-1.5.11
changeset 6016 325e2ab3406f
parent 3678 4dbda7e6999b
permissions -rw-r--r--
Fixed a bunch of warnings
3678
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     1
/* 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     2
 * Adium is the legal property of its developers, whose names are listed in the copyright file included
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     3
 * with this source distribution.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     4
 * 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     6
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     7
 * or (at your option) any later version.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     8
 * 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
     9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
    10
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
    11
 * Public License for more details.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
    12
 * 
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
    13
 * You should have received a copy of the GNU General Public License along with this program; if not,
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
    14
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
4dbda7e6999b Bring consistency to file headings, this is not exhaustive. Zac and Adam version.
Frank Dowsett <wixardy@adium.im>
parents: 3430
diff changeset
    15
 */
2534
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    16
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    17
#import "AIGenericMultipartImageUploader.h"
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    18
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    19
#import <Adium/AIChat.h>
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    20
#import <Adium/AIInterfaceControllerProtocol.h>
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    21
#import <AIUtilities/AIStringAdditions.h>
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    22
#import <AIUtilities/AIProgressDataUploader.h>
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    23
#import <AIUtilities/AIImageAdditions.h>
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    24
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    25
#define MULTIPART_FORM_BOUNDARY	@"bf5faadd239c17e35f91e6dafe1d2f96"
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    26
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    27
@interface AIGenericMultipartImageUploader()
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    28
- (id)initWithImage:(NSImage *)inImage
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    29
		   uploader:(AIImageUploaderPlugin *)inUploader
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    30
			   chat:(AIChat *)inChat;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    31
- (void)uploadImage;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    32
@end
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    33
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    34
@implementation AIGenericMultipartImageUploader
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    35
+ (id)uploadImage:(NSImage *)image forUploader:(AIImageUploaderPlugin *)uploader inChat:(AIChat *)chat;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    36
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    37
	return [[[self alloc] initWithImage:image uploader:uploader chat:chat] autorelease];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    38
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    39
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    40
+ (NSString *)serviceName
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    41
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    42
	NSAssert1(NO, @"Implementation of %@ lacks serviceName", NSStringFromClass(self));
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    43
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    44
	return nil;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    45
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    46
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    47
- (NSString *)uploadURL
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    48
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    49
	NSAssert1(NO, @"Implementation of %@ lacks uploadURL", self);
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    50
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    51
	return nil;	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    52
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    53
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    54
- (NSString *)fieldName
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    55
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    56
	NSAssert1(NO, @"Implementation of %@ lacks fieldName", self);
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    57
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    58
	return nil;	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    59
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    60
3430
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
    61
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
    62
- (NSArray *)additionalFields
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
    63
{	
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
    64
	return nil;
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
    65
}
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
    66
2534
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    67
- (NSUInteger)maximumSize
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    68
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    69
	NSAssert1(NO, @"Implementation of %@ lacks maximumSize", self);
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    70
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    71
	return 0;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    72
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    73
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    74
- (void)parseResponse:(NSData *)date
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    75
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    76
	NSAssert1(NO, @"Implementation of %@ lacks parseResponse:", self);	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    77
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    78
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    79
- (id)initWithImage:(NSImage *)inImage
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    80
		   uploader:(AIImageUploaderPlugin *)inUploader
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    81
			   chat:(AIChat *)inChat
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    82
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    83
	if ((self = [super init])) {
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    84
		image = [inImage retain];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    85
		uploader = inUploader;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    86
		chat = inChat;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    87
		
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    88
		[self uploadImage];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    89
	}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    90
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    91
	return self;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    92
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    93
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    94
- (void)dealloc
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    95
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    96
	[dataUploader release]; dataUploader = nil;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    97
	[image release]; image = nil;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    98
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
    99
	[super dealloc];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   100
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   101
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   102
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   103
#pragma mark Data uploader delegate
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   104
- (void)updateUploadProgress:(NSUInteger)uploaded total:(NSUInteger)total context:(id)context
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   105
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   106
	[uploader updateProgress:uploaded total:total forChat:chat];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   107
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   108
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   109
- (void)uploadCompleted:(id)context result:(NSData *)result
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   110
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   111
	if (result.length) {
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   112
		[self parseResponse:result];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   113
	} else {
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   114
		[uploader errorWithMessage:AILocalizedString(@"Unable to upload", nil) forChat:chat];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   115
	}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   116
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   117
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   118
- (void)uploadFailed:(id)context
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   119
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   120
	[uploader errorWithMessage:AILocalizedString(@"Unable to upload", nil) forChat:chat];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   121
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   122
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   123
#pragma mark Image upload
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   124
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   125
- (void)uploadImage
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   126
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   127
	NSMutableData *body = [NSMutableData data];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   128
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   129
	NSBitmapImageFileType bestType;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   130
	
6016
325e2ab3406f Fixed a bunch of warnings
Evan Schoenberg
parents: 3678
diff changeset
   131
    NSData *pngRepresentation = [[image largestBitmapImageRep] representationUsingType:NSPNGFileType properties:@{}];
325e2ab3406f Fixed a bunch of warnings
Evan Schoenberg
parents: 3678
diff changeset
   132
	NSData *jpgRepresentation = [[image largestBitmapImageRep] representationUsingType:NSJPEGFileType properties:@{}];
2534
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   133
	NSData *imageRepresentation;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   134
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   135
	if (pngRepresentation.length > jpgRepresentation.length) {
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   136
		bestType = NSJPEGFileType;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   137
		imageRepresentation = jpgRepresentation;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   138
	} else {
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   139
		bestType = NSPNGFileType;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   140
		imageRepresentation = pngRepresentation;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   141
	}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   142
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   143
	if (imageRepresentation.length > self.maximumSize) {
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   144
		imageRepresentation = [image representationWithFileType:bestType maximumFileSize:self.maximumSize];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   145
	}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   146
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   147
	if (!imageRepresentation) {
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   148
		[uploader errorWithMessage:AILocalizedString(@"Unable to upload", nil) forChat:chat];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   149
		return;
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   150
	}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   151
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   152
	[body appendData:[[NSString stringWithFormat:@"--%@\r\n", MULTIPART_FORM_BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   153
	[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image\"\r\n", self.fieldName] dataUsingEncoding:NSUTF8StringEncoding]];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   154
	[body appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n", (bestType == NSJPEGFileType) ? @"image/jpeg" : @"image/png"] dataUsingEncoding:NSUTF8StringEncoding]];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   155
	[body appendData:imageRepresentation];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   156
	[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", MULTIPART_FORM_BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
3430
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
   157
	for (NSDictionary *field in [self additionalFields]) {
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
   158
		[body appendData:[[NSString stringWithFormat:@"--%@\r\n", MULTIPART_FORM_BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
   159
		[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name= \"%@\"\r\n\r\n", [field objectForKey:@"name"]] dataUsingEncoding:NSUTF8StringEncoding]];
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
   160
		[body appendData:[[NSString stringWithFormat:@"%@", [field objectForKey:@"value"]] dataUsingEncoding:NSUTF8StringEncoding]];
6f998f0fe450 Patch from shaynesweeney to add Imgur as an image uploader. Fixes #13186
Frank Dowsett <wixardy@adium.im>
parents: 2534
diff changeset
   161
	}
2534
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   162
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   163
	NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   164
							 [NSString stringWithFormat:@"multipart/form-data; boundary=%@", MULTIPART_FORM_BOUNDARY], @"Content-type", nil];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   165
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   166
	dataUploader = [[AIProgressDataUploader dataUploaderWithData:body
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   167
															 URL:[NSURL URLWithString:self.uploadURL]
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   168
														 headers:headers
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   169
														delegate:self
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   170
														 context:nil] retain];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   171
	
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   172
	[dataUploader upload];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   173
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   174
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   175
- (void)cancel
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   176
{
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   177
	[dataUploader cancel];
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   178
}
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   179
1306b41fd66b Abstract multipart image uploading a bit and add an imageshack.us service for image uploading.
Zachary West <zacw@adium.im>
parents:
diff changeset
   180
@end