Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit 3692b3a745b1c8b2b4659e723623e61609d467bb @nrj committed Dec 15, 2009
@@ -0,0 +1,18 @@
+//
+// CurlFTP.h
+// objective-curl
+//
+// Created by nrj on 12/14/09.
+// Copyright 2009. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "CurlObject.h"
+
+@interface CurlFTP : CurlObject {
+
+}
+
+- (void)uploadFile:(NSString *)filePath toLocation:(NSString *)hostname withCredentials:(NSString *)credentials;
+
+@end
@@ -0,0 +1,43 @@
+//
+// CurlFTP.m
+// objective-curl
+//
+// Created by nrj on 12/14/09.
+// Copyright 2009. All rights reserved.
+//
+
+#import "CurlFTP.h"
+
+
+@implementation CurlFTP
+
+- (void)uploadFile:(NSString *)filePath toLocation:(NSString *)hostname withCredentials:(NSString *)credentials
+{
+ CURLcode result;
+ FILE *fh;
+ struct stat file_info;
+ curl_off_t fsize;
+
+ if(stat([filePath UTF8String], &file_info)) {
+ printf("Couldnt open '%s': %s\n", [filePath UTF8String], strerror(errno));
+ return;
+ }
+
+ fsize = (curl_off_t)file_info.st_size;
+
+ fh = fopen([filePath UTF8String], "rb");
+
+ curl_easy_setopt(handle, CURLOPT_UPLOAD, 1L);
+ curl_easy_setopt(handle, CURLOPT_URL, [hostname UTF8String]);
+ curl_easy_setopt(handle, CURLOPT_USERPWD, [credentials UTF8String]);
+ curl_easy_setopt(handle, CURLOPT_READDATA, fh);
+ curl_easy_setopt(handle, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize);
+
+ result = curl_easy_perform(handle);
+
+ curl_easy_cleanup(fh);
+
+ fclose(fh);
+}
+
+@end
@@ -0,0 +1,47 @@
+//
+// CurlObject.h
+// objective-curl
+//
+// Created by nrj on 12/14/09.
+// Copyright 2009. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+
+#import "UploadDelegate.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <curl/curl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+@interface CurlObject : NSObject <UploadDelegate>
+{
+ id delegate;
+ CURL *handle;
+
+ BOOL verbose;
+ BOOL showProgress;
+
+ NSString *authUsername;
+ NSString *authPassword;
+
+ id <TransferRecord> currentTransfer;
+}
+
+@property(readwrite, assign) id delegate;
+@property(readwrite, retain) id <TransferRecord> currentTransfer;
+
+static int handleClientProgress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow);
+
+- (BOOL)verbose;
+- (void)setVerbose:(BOOL)value;
+
+- (BOOL)showProgress;
+- (void)setShowProgress:(BOOL)value;
+
+@end
@@ -0,0 +1,152 @@
+//
+// CurlObject.m
+// objective-curl
+//
+// Created by nrj on 12/14/09.
+// Copyright 2009. All rights reserved.
+//
+
+#import "CurlObject.h"
+
+
+
+@implementation CurlObject
+
+@synthesize delegate;
+@synthesize currentTransfer;
+
+- (id)init
+{
+ if (self = [super init])
+ {
+ handle = curl_easy_init();
+
+ if (!handle)
+ {
+ @throw [NSException exceptionWithName:@"Initialization Error"
+ reason:@"Unable to initialize libcurl."
+ userInfo:nil];
+ }
+
+ curl_easy_setopt(handle, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
+ }
+
+ return self;
+}
+
+
+- (void)dealloc
+{
+ curl_global_cleanup();
+
+ [super dealloc];
+}
+
+
+- (void)setVerbose:(BOOL)value
+{
+ if (!handle) return;
+
+ curl_easy_setopt(handle, CURLOPT_VERBOSE, value);
+
+ verbose = value;
+}
+
+
+- (BOOL)verbose
+{
+ return verbose;
+}
+
+
+- (void)setShowProgress:(BOOL)value
+{
+ if (!handle) return;
+
+ if (value) {
+ curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0);
+ curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, self);
+ curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, handleClientProgress);
+ } else {
+ curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 1);
+ curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, NULL);
+ curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, NULL);
+ }
+
+ showProgress = value;
+}
+
+
+- (BOOL)showProgress
+{
+ return showProgress;
+}
+
+
+#pragma mark Upload Delegate Methods
+
+
+- (void)uploadRequiresAuthentication:(id <TransferRecord>)aRecord
+{
+ if (delegate && [delegate respondsToSelector:@selector(uploadRequiresAuthentication:)])
+ {
+ [delegate uploadRequiresAuthentication:aRecord];
+ }
+}
+
+
+- (void)uploadDidBegin:(id <TransferRecord>)aRecord
+{
+ if (delegate && [delegate respondsToSelector:@selector(uploadDidBegin:)])
+ {
+ [delegate uploadDidBegin:aRecord];
+ }
+}
+
+
+- (void)uploadDidProgress:(id <TransferRecord>)aRecord toPercent:(int)aPercent
+{
+ printf("uploadDidProgress:toPercent:%d\n", aPercent);
+ if (delegate && [delegate respondsToSelector:@selector(uploadDidProgress:toPercent:)])
+ {
+ [delegate uploadDidProgress:aRecord toPercent:aPercent];
+ }
+}
+
+
+- (void)uploadDidFinish:(id <TransferRecord>)aRecord
+{
+ if (delegate && [delegate respondsToSelector:@selector(uploadDidFinish:)])
+ {
+ [delegate uploadDidFinish:aRecord];
+ }
+}
+
+
+@end
+
+
+#pragma mark Private Methods
+
+
+@implementation CurlObject (Private)
+
+
+static int handleClientProgress(void *clientp, double dltotal, double dlnow, double ultotal, double ulnow)
+{
+// CurlObject *client = (CurlObject *)clientp;
+// id <TransferRecord>transfer = [client currentTransfer];
+
+ int uploadProgress = (ulnow * 100 / ultotal);
+
+// [transfer setProgress:uploadProgress];
+
+// [client uploadDidProgress:transfer toPercent:uploadProgress];
+
+ printf("handleClientProgress %d\n", uploadProgress);
+
+ return 0;
+}
+
+
+@end
Binary file not shown.
Oops, something went wrong.

0 comments on commit 3692b3a

Please sign in to comment.