From 46c5ef426b3104a2df0c1b9428d79f31cd249b94 Mon Sep 17 00:00:00 2001 From: Jari vetoniemi Date: Wed, 15 May 2013 10:35:16 +0300 Subject: [PATCH] Include DAVPutStreamRequest --- Sources/Core/DAVRequests.h | 12 ++++++++++++ Sources/Core/DAVRequests.m | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/Sources/Core/DAVRequests.h b/Sources/Core/DAVRequests.h index 602c410..f7891da 100644 --- a/Sources/Core/DAVRequests.h +++ b/Sources/Core/DAVRequests.h @@ -50,3 +50,15 @@ @property (copy) NSString *dataMIMEType; // defaults to application/octet-stream @end + +@interface DAVPutStreamRequest : DAVRequest { + @private + NSString *_pfilepath; + NSString *_MIMEtype; +} + +// Pass - Path to local file +@property (strong) NSString *filepath; +@property (copy) NSString *dataMIMEType; // defaults to application/octet-stream + +@end diff --git a/Sources/Core/DAVRequests.m b/Sources/Core/DAVRequests.m index 5492607..33cbb01 100644 --- a/Sources/Core/DAVRequests.m +++ b/Sources/Core/DAVRequests.m @@ -156,3 +156,39 @@ - (NSURLRequest *)request { } @end + +@implementation DAVPutStreamRequest + +- (id)initWithPath:(NSString *)aPath { + if ((self = [super initWithPath:aPath])) { + self.dataMIMEType = @"application/octet-stream"; + } + return self; +} + +@synthesize filepath = _pfilepath; +@synthesize dataMIMEType = _MIMEType; + +- (NSInputStream *)connection:(NSURLConnection *)connection needNewBodyStream:(NSURLRequest *)req { + + return [NSInputStream inputStreamWithFileAtPath:_pfilepath]; +} + +- (NSURLRequest *)request { + NSParameterAssert(_pfilepath != nil); + + NSError *error = nil; + NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:_pfilepath + error:&error]; + if (!attrs) return nil; + + NSString *len = [NSString stringWithFormat:@"%lld", attrs.fileSize]; + NSMutableURLRequest *req = [self newRequestWithPath:self.path method:@"PUT"]; + [req setValue:[self dataMIMEType] forHTTPHeaderField:@"Content-Type"]; + [req setValue:len forHTTPHeaderField:@"Content-Length"]; + [req setHTTPBodyStream:[NSInputStream inputStreamWithFileAtPath:_pfilepath]]; + + return req; +} + +@end