2 Copyright (C) 2000-2003 SKYRIX Software AG
4 This file is part of OGo
6 OGo is free software; you can redistribute it and/or modify it under
7 the terms of the GNU Lesser General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 OGo is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
14 License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with OGo; see the file COPYING. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #import <NGMime/NGMime.h>
26 #import <NGHttp/NGHttp.h>
28 @interface WOFileUpload : WOInput
30 // WODynamicElement: extraAttributes
31 // WODynamicElement: otherTagString
34 // inherited: disabled
36 WOAssociation *filePath; // dispostion 'filename'
37 WOAssociation *data; // uploaded data
40 @end /* WOFileUpload */
42 @interface WORequest(UsedPrivates)
46 @implementation WOFileUpload
48 static NGMimeType *multipartFormData = nil;
51 static BOOL isInitialized = NO;
55 multipartFormData = [NGMimeType mimeType:@"multipart/form-data"];
56 multipartFormData = RETAIN(multipartFormData);
60 - (id)initWithName:(NSString *)_name
61 associations:(NSDictionary *)_config
62 template:(WOElement *)_root {
64 if ((self = [super initWithName:_name associations:_config template:_root])) {
65 self->filePath = OWGetProperty(_config, @"filePath");
66 self->data = OWGetProperty(_config, @"data");
71 #if !LIB_FOUNDATION_BOEHM_GC
73 RELEASE(self->filePath); self->filePath = nil;
74 RELEASE(self->data); self->data = nil;
79 // ******************** responder ********************
81 - (void)takeValuesFromRequest:(WORequest *)_request
82 inContext:(WOContext *)_ctx
84 if (![self->disabled boolValueInComponent:[_ctx component]]) {
88 currentId = OWFormElementName(self, _ctx);
90 formValue = [_request formValueForKey:currentId];
92 NGMimeType *contentType = [[_request httpRequest] contentType];
94 if (![contentType hasSameType:multipartFormData]) {
95 NSLog(@"WARNING: tried to apply file-upload value of %@ from "
96 @"a non multipart-form request (value=%@) !",
97 [_ctx elementID], formValue);
101 //NSLog(@"%@: value=%@ ..", [self elementID], formValue);
103 if ([self->data isValueSettable])
104 [self->data setValue:formValue inComponent:[_ctx component]];
106 if ([self->filePath isValueSettable]) {
107 NGMimeMultipartBody *body = [[_request httpRequest] body];
109 if ([body isKindOfClass:[NGMimeMultipartBody class]]) {
110 NSArray *parts = [body parts];
111 unsigned i, count = [parts count];
113 // search for part of current form element
115 for (i = 0; i < count; i++) {
117 id<NGMimePart> bodyPart;
119 bodyPart = [parts objectAtIndex:i];
121 [[bodyPart valuesOfHeaderFieldWithName:@"content-disposition"]
125 static Class DispClass = Nil;
128 if (DispClass == Nil)
129 DispClass = [NGMimeContentDispositionHeaderField class];
131 if (![disposition isKindOfClass:DispClass]) {
133 [[DispClass alloc] initWithString:[disposition stringValue]];
134 AUTORELEASE(disposition);
138 [(NGMimeContentDispositionHeaderField *)disposition name];
140 if ([formName isEqualToString:currentId]) {
142 setValue:[disposition filename]
143 inComponent:[_ctx component]];
154 - (void)appendToResponse:(WOResponse *)_response inContext:(WOContext *)_ctx {
155 if (![[_ctx request] isFromClientComponent]) {
156 NSString *v = [self->value stringValueInComponent:[_ctx component]];
158 WOResponse_AddCString(_response, "<input type=\"file\" name=\"");
159 [_response appendContentHTMLAttributeValue:OWFormElementName(self, _ctx)];
160 WOResponse_AddChar(_response, '"');
162 WOResponse_AddCString(_response, " value=\"");
163 [_response appendContentHTMLAttributeValue:v];
164 WOResponse_AddChar(_response, '"');
166 [self appendExtraAttributesToResponse:_response inContext:_ctx];
168 if (self->otherTagString) {
169 WOResponse_AddString(_response,
170 [self->otherTagString stringValueInComponent:
173 WOResponse_AddCString(_response, " />");
179 - (NSString *)associationDescription {
180 NSMutableString *str = [[NSMutableString alloc] init];
181 [str appendString:[super associationDescription]];
183 if (self->filePath) [str appendFormat:@" path=%@", self->filePath];
184 if (self->data) [str appendFormat:@" data=%@", self->data];
186 return AUTORELEASE(str);
189 @end /* WOFileUpload */