2014-03-30 09:19:45 +08:00
/ *
Copyright ( c ) 2012 -2014 , Pierre - Olivier Latour
All rights reserved .
Redistribution and use in source and binary forms , with or without
modification , are permitted provided that the following conditions are met :
* Redistributions of source code must retain the above copyright
notice , this list of conditions and the following disclaimer .
* Redistributions in binary form must reproduce the above copyright
notice , this list of conditions and the following disclaimer in the
documentation and / or other materials provided with the distribution .
* The name of Pierre - Olivier Latour may not be used to endorse
or promote products derived from this software without specific
prior written permission .
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES , INCLUDING , BUT NOT LIMITED TO , THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED . IN NO EVENT SHALL PIERRE - OLIVIER LATOUR BE LIABLE FOR ANY
DIRECT , INDIRECT , INCIDENTAL , SPECIAL , EXEMPLARY , OR CONSEQUENTIAL DAMAGES
( INCLUDING , BUT NOT LIMITED TO , PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES ;
LOSS OF USE , DATA , OR PROFITS ; OR BUSINESS INTERRUPTION ) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY , WHETHER IN CONTRACT , STRICT LIABILITY , OR TORT
( INCLUDING NEGLIGENCE OR OTHERWISE ) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE , EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE .
* /
# import < TargetConditionals . h >
# if TARGET_OS _IPHONE
# import < UIKit / UIKit . h >
# else
# import < SystemConfiguration / SystemConfiguration . h >
# endif
# import "GCDWebUploader.h"
2014-04-08 14:03:08 +08:00
2014-04-07 02:32:07 +08:00
# import "GCDWebServerDataRequest.h"
# import "GCDWebServerMultiPartFormRequest.h"
# import "GCDWebServerURLEncodedFormRequest.h"
2014-04-08 14:03:08 +08:00
2014-04-07 02:32:07 +08:00
# import "GCDWebServerDataResponse.h"
2014-04-08 14:03:08 +08:00
# import "GCDWebServerErrorResponse.h"
2014-04-07 02:32:07 +08:00
# import "GCDWebServerFileResponse.h"
2014-03-30 09:19:45 +08:00
@ interface GCDWebUploader ( ) {
@ private
NSString * _uploadDirectory ;
NSArray * _allowedExtensions ;
2014-04-20 03:31:00 +08:00
BOOL _allowHidden ;
2014-03-30 09:19:45 +08:00
NSString * _title ;
NSString * _header ;
2014-04-02 07:06:44 +08:00
NSString * _prologue ;
NSString * _epilogue ;
2014-03-30 09:19:45 +08:00
NSString * _footer ;
}
@ end
2014-04-08 14:12:18 +08:00
@ implementation GCDWebUploader ( Methods )
2014-03-30 09:19:45 +08:00
- ( BOOL ) _checkFileExtension : ( NSString * ) fileName {
if ( _allowedExtensions && ! [ _allowedExtensions containsObject : [ [ fileName pathExtension ] lowercaseString ] ] ) {
return NO ;
}
return YES ;
}
2014-04-02 04:13:22 +08:00
- ( NSString * ) _uniquePathForPath : ( NSString * ) path {
if ( [ [ NSFileManager defaultManager ] fileExistsAtPath : path ] ) {
NSString * directory = [ path stringByDeletingLastPathComponent ] ;
NSString * file = [ path lastPathComponent ] ;
NSString * base = [ file stringByDeletingPathExtension ] ;
NSString * extension = [ file pathExtension ] ;
int retries = 0 ;
do {
if ( extension . length ) {
path = [ directory stringByAppendingPathComponent : [ [ base stringByAppendingFormat : @ " (%i)" , + + retries ] stringByAppendingPathExtension : extension ] ] ;
} else {
path = [ directory stringByAppendingPathComponent : [ base stringByAppendingFormat : @ " (%i)" , + + retries ] ] ;
}
} while ( [ [ NSFileManager defaultManager ] fileExistsAtPath : path ] ) ;
}
return path ;
}
2014-04-08 14:12:18 +08:00
- ( GCDWebServerResponse * ) listDirectory : ( GCDWebServerRequest * ) request {
NSString * relativePath = [ [ request query ] objectForKey : @ "path" ] ;
NSString * absolutePath = [ _uploadDirectory stringByAppendingPathComponent : relativePath ] ;
BOOL isDirectory ;
2014-04-08 14:17:57 +08:00
if ( ! [ [ NSFileManager defaultManager ] fileExistsAtPath : absolutePath isDirectory : & isDirectory ] ) {
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_NotFound message : @ "\" % @ \ " does not exist" , relativePath ] ;
}
if ( ! isDirectory ) {
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_BadRequest message : @ "\" % @ \ " is not a directory" , relativePath ] ;
}
2014-04-10 04:45:15 +08:00
NSString * directoryName = [ absolutePath lastPathComponent ] ;
2014-04-20 03:31:00 +08:00
if ( ! _allowHidden && [ directoryName hasPrefix : @ "." ] ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Listing directory name \" % @ \ " is not allowed" , directoryName ] ;
}
2014-04-08 14:17:57 +08:00
NSError * error = nil ;
NSArray * contents = [ [ NSFileManager defaultManager ] contentsOfDirectoryAtPath : absolutePath error : & error ] ;
if ( contents = = nil ) {
return [ GCDWebServerErrorResponse responseWithServerError : kGCDWebServerHTTPStatusCode_InternalServerError underlyingError : error message : @ "Failed listing directory \" % @ \ "" , relativePath ] ;
}
NSMutableArray * array = [ NSMutableArray array ] ;
for ( NSString * item in [ contents sortedArrayUsingSelector : @ selector ( localizedStandardCompare : ) ] ) {
2014-04-20 03:31:00 +08:00
if ( _allowHidden || ! [ item hasPrefix : @ "." ] ) {
2014-04-08 14:17:57 +08:00
NSDictionary * attributes = [ [ NSFileManager defaultManager ] attributesOfItemAtPath : [ absolutePath stringByAppendingPathComponent : item ] error : NULL ] ;
NSString * type = [ attributes objectForKey : NSFileType ] ;
if ( [ type isEqualToString : NSFileTypeRegular ] && [ self _checkFileExtension : item ] ) {
[ array addObject : @ {
@ "path" : [ relativePath stringByAppendingPathComponent : item ] ,
@ "name" : item ,
@ "size" : [ attributes objectForKey : NSFileSize ]
} ] ;
} else if ( [ type isEqualToString : NSFileTypeDirectory ] ) {
[ array addObject : @ {
@ "path" : [ [ relativePath stringByAppendingPathComponent : item ] stringByAppendingString : @ "/" ] ,
@ "name" : item
} ] ;
2014-04-08 14:12:18 +08:00
}
}
}
2014-04-08 14:17:57 +08:00
return [ GCDWebServerDataResponse responseWithJSONObject : array ] ;
2014-04-08 14:12:18 +08:00
}
- ( GCDWebServerResponse * ) downloadFile : ( GCDWebServerRequest * ) request {
NSString * relativePath = [ [ request query ] objectForKey : @ "path" ] ;
NSString * absolutePath = [ _uploadDirectory stringByAppendingPathComponent : relativePath ] ;
BOOL isDirectory ;
2014-04-08 14:17:57 +08:00
if ( ! [ [ NSFileManager defaultManager ] fileExistsAtPath : absolutePath isDirectory : & isDirectory ] ) {
2014-04-08 14:12:18 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_NotFound message : @ "\" % @ \ " does not exist" , relativePath ] ;
}
2014-04-08 14:17:57 +08:00
if ( isDirectory ) {
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_BadRequest message : @ "\" % @ \ " is a directory" , relativePath ] ;
}
2014-04-10 04:45:15 +08:00
NSString * fileName = [ absolutePath lastPathComponent ] ;
2014-04-20 03:31:00 +08:00
if ( ( [ fileName hasPrefix : @ "." ] && ! _allowHidden ) || ! [ self _checkFileExtension : fileName ] ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Downlading file name \" % @ \ " is not allowed" , fileName ] ;
}
2014-04-16 11:39:56 +08:00
if ( [ self . delegate respondsToSelector : @ selector ( webUploader : didDownloadFileAtPath : ) ] ) {
2014-04-08 14:17:57 +08:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
2014-04-16 11:39:56 +08:00
[ self . delegate webUploader : self didDownloadFileAtPath : absolutePath ] ;
2014-04-08 14:17:57 +08:00
} ) ;
}
return [ GCDWebServerFileResponse responseWithFile : absolutePath isAttachment : YES ] ;
2014-04-08 14:12:18 +08:00
}
- ( GCDWebServerResponse * ) uploadFile : ( GCDWebServerMultiPartFormRequest * ) request {
NSRange range = [ [ request . headers objectForKey : @ "Accept" ] rangeOfString : @ "application/json" options : NSCaseInsensitiveSearch ] ;
NSString * contentType = ( range . location ! = NSNotFound ? @ "application/json" : @ "text/plain; charset=utf-8" ) ; // Required when using iFrame transport ( see https : // github . com / blueimp / jQuery - File - Upload / wiki / Setup )
GCDWebServerMultiPartFile * file = [ request . files objectForKey : @ "files[]" ] ;
2014-04-20 03:31:00 +08:00
if ( ( ! _allowHidden && [ file . fileName hasPrefix : @ "." ] ) || ! [ self _checkFileExtension : file . fileName ] ) {
2014-04-08 14:12:18 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Uploaded file name \" % @ \ " is not allowed" , file . fileName ] ;
}
2014-04-08 14:17:57 +08:00
NSString * relativePath = [ ( GCDWebServerMultiPartArgument * ) [ request . arguments objectForKey : @ "path" ] string ] ;
NSString * absolutePath = [ self _uniquePathForPath : [ [ _uploadDirectory stringByAppendingPathComponent : relativePath ] stringByAppendingPathComponent : file . fileName ] ] ;
if ( ! [ self shouldUploadFileAtPath : absolutePath withTemporaryFile : file . temporaryPath ] ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Uploading file \" % @ \ " to \" % @ \ " is not permitted" , file . fileName , relativePath ] ;
2014-04-08 14:17:57 +08:00
}
NSError * error = nil ;
if ( ! [ [ NSFileManager defaultManager ] moveItemAtPath : file . temporaryPath toPath : absolutePath error : & error ] ) {
return [ GCDWebServerErrorResponse responseWithServerError : kGCDWebServerHTTPStatusCode_InternalServerError underlyingError : error message : @ "Failed moving uploaded file to \" % @ \ "" , relativePath ] ;
}
2014-04-16 11:39:56 +08:00
if ( [ self . delegate respondsToSelector : @ selector ( webUploader : didUploadFileAtPath : ) ] ) {
2014-04-08 14:17:57 +08:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
2014-04-16 11:39:56 +08:00
[ self . delegate webUploader : self didUploadFileAtPath : absolutePath ] ;
2014-04-08 14:17:57 +08:00
} ) ;
}
return [ GCDWebServerDataResponse responseWithJSONObject : @ { } contentType : contentType ] ;
2014-04-08 14:12:18 +08:00
}
- ( GCDWebServerResponse * ) moveItem : ( GCDWebServerURLEncodedFormRequest * ) request {
NSString * oldRelativePath = [ request . arguments objectForKey : @ "oldPath" ] ;
NSString * oldAbsolutePath = [ _uploadDirectory stringByAppendingPathComponent : oldRelativePath ] ;
BOOL isDirectory ;
2014-04-08 14:17:57 +08:00
if ( ! [ [ NSFileManager defaultManager ] fileExistsAtPath : oldAbsolutePath isDirectory : & isDirectory ] ) {
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_NotFound message : @ "\" % @ \ " does not exist" , oldRelativePath ] ;
}
NSString * newRelativePath = [ request . arguments objectForKey : @ "newPath" ] ;
NSString * newAbsolutePath = [ self _uniquePathForPath : [ _uploadDirectory stringByAppendingPathComponent : newRelativePath ] ] ;
2014-04-10 04:45:15 +08:00
NSString * itemName = [ newAbsolutePath lastPathComponent ] ;
2014-04-20 03:31:00 +08:00
if ( ( ! _allowHidden && [ itemName hasPrefix : @ "." ] ) || ( ! isDirectory && ! [ self _checkFileExtension : itemName ] ) ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Moving to item name \" % @ \ " is not allowed" , itemName ] ;
2014-04-08 14:17:57 +08:00
}
if ( ! [ self shouldMoveItemFromPath : oldAbsolutePath toPath : newAbsolutePath ] ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Moving \" % @ \ " to \" % @ \ " is not permitted" , oldRelativePath , newRelativePath ] ;
2014-04-08 14:17:57 +08:00
}
NSError * error = nil ;
if ( ! [ [ NSFileManager defaultManager ] moveItemAtPath : oldAbsolutePath toPath : newAbsolutePath error : & error ] ) {
return [ GCDWebServerErrorResponse responseWithServerError : kGCDWebServerHTTPStatusCode_InternalServerError underlyingError : error message : @ "Failed moving \" % @ \ " to \" % @ \ "" , oldRelativePath , newRelativePath ] ;
}
2014-04-16 11:39:56 +08:00
if ( [ self . delegate respondsToSelector : @ selector ( webUploader : didMoveItemFromPath : toPath : ) ] ) {
2014-04-08 14:17:57 +08:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
2014-04-16 11:39:56 +08:00
[ self . delegate webUploader : self didMoveItemFromPath : oldAbsolutePath toPath : newAbsolutePath ] ;
2014-04-08 14:17:57 +08:00
} ) ;
}
return [ GCDWebServerDataResponse responseWithJSONObject : @ { } ] ;
2014-04-08 14:12:18 +08:00
}
- ( GCDWebServerResponse * ) deleteItem : ( GCDWebServerURLEncodedFormRequest * ) request {
NSString * relativePath = [ request . arguments objectForKey : @ "path" ] ;
NSString * absolutePath = [ _uploadDirectory stringByAppendingPathComponent : relativePath ] ;
2014-04-10 04:45:15 +08:00
BOOL isDirectory = NO ;
if ( ! [ [ NSFileManager defaultManager ] fileExistsAtPath : absolutePath isDirectory : & isDirectory ] ) {
2014-04-08 14:12:18 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_NotFound message : @ "\" % @ \ " does not exist" , relativePath ] ;
}
2014-04-08 14:17:57 +08:00
2014-04-10 04:45:15 +08:00
NSString * itemName = [ absolutePath lastPathComponent ] ;
2014-04-20 03:31:00 +08:00
if ( ( [ itemName hasPrefix : @ "." ] && ! _allowHidden ) || ( ! isDirectory && ! [ self _checkFileExtension : itemName ] ) ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Deleting item name \" % @ \ " is not allowed" , itemName ] ;
}
2014-04-08 14:17:57 +08:00
if ( ! [ self shouldDeleteItemAtPath : absolutePath ] ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Deleting \" % @ \ " is not permitted" , relativePath ] ;
2014-04-08 14:17:57 +08:00
}
NSError * error = nil ;
if ( ! [ [ NSFileManager defaultManager ] removeItemAtPath : absolutePath error : & error ] ) {
return [ GCDWebServerErrorResponse responseWithServerError : kGCDWebServerHTTPStatusCode_InternalServerError underlyingError : error message : @ "Failed deleting \" % @ \ "" , relativePath ] ;
}
2014-04-16 11:39:56 +08:00
if ( [ self . delegate respondsToSelector : @ selector ( webUploader : didDeleteItemAtPath : ) ] ) {
2014-04-08 14:17:57 +08:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
2014-04-16 11:39:56 +08:00
[ self . delegate webUploader : self didDeleteItemAtPath : absolutePath ] ;
2014-04-08 14:17:57 +08:00
} ) ;
}
return [ GCDWebServerDataResponse responseWithJSONObject : @ { } ] ;
2014-04-08 14:12:18 +08:00
}
- ( GCDWebServerResponse * ) createDirectory : ( GCDWebServerURLEncodedFormRequest * ) request {
NSString * relativePath = [ request . arguments objectForKey : @ "path" ] ;
2014-04-08 14:17:57 +08:00
NSString * absolutePath = [ self _uniquePathForPath : [ _uploadDirectory stringByAppendingPathComponent : relativePath ] ] ;
2014-04-10 04:45:15 +08:00
NSString * directoryName = [ absolutePath lastPathComponent ] ;
2014-04-20 03:31:00 +08:00
if ( ! _allowHidden && [ directoryName hasPrefix : @ "." ] ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Creating directory name \" % @ \ " is not allowed" , directoryName ] ;
2014-04-08 14:12:18 +08:00
}
2014-04-08 14:17:57 +08:00
if ( ! [ self shouldCreateDirectoryAtPath : absolutePath ] ) {
2014-04-10 04:45:15 +08:00
return [ GCDWebServerErrorResponse responseWithClientError : kGCDWebServerHTTPStatusCode_Forbidden message : @ "Creating directory \" % @ \ " is not permitted" , relativePath ] ;
2014-04-08 14:12:18 +08:00
}
2014-04-08 14:17:57 +08:00
NSError * error = nil ;
2014-04-10 04:45:15 +08:00
if ( ! [ [ NSFileManager defaultManager ] createDirectoryAtPath : absolutePath withIntermediateDirectories : NO attributes : nil error : & error ] ) {
2014-04-08 14:17:57 +08:00
return [ GCDWebServerErrorResponse responseWithServerError : kGCDWebServerHTTPStatusCode_InternalServerError underlyingError : error message : @ "Failed creating directory \" % @ \ "" , relativePath ] ;
}
2014-04-16 11:39:56 +08:00
if ( [ self . delegate respondsToSelector : @ selector ( webUploader : didCreateDirectoryAtPath : ) ] ) {
2014-04-08 14:17:57 +08:00
dispatch_async ( dispatch_get _main _queue ( ) , ^ {
2014-04-16 11:39:56 +08:00
[ self . delegate webUploader : self didCreateDirectoryAtPath : absolutePath ] ;
2014-04-08 14:17:57 +08:00
} ) ;
}
return [ GCDWebServerDataResponse responseWithJSONObject : @ { } ] ;
2014-04-08 14:12:18 +08:00
}
@ end
@ implementation GCDWebUploader
2014-04-20 03:31:00 +08:00
@ synthesize uploadDirectory = _uploadDirectory , allowedFileExtensions = _allowedExtensions , allowHiddenItems = _allowHidden ,
2014-04-08 14:12:18 +08:00
title = _title , header = _header , prologue = _prologue , epilogue = _epilogue , footer = _footer ;
2014-04-08 14:45:33 +08:00
- ( instancetype ) initWithUploadDirectory : ( NSString * ) path {
2014-03-30 09:19:45 +08:00
if ( ( self = [ super init ] ) ) {
2014-04-02 04:16:58 +08:00
NSBundle * siteBundle = [ NSBundle bundleWithPath : [ [ NSBundle mainBundle ] pathForResource : @ "GCDWebUploader" ofType : @ "bundle" ] ] ;
2014-03-30 09:19:45 +08:00
if ( siteBundle = = nil ) {
# if ! __has _feature ( objc_arc )
[ self release ] ;
# endif
return nil ;
}
_uploadDirectory = [ [ path stringByStandardizingPath ] copy ] ;
2014-04-10 02:10:45 +08:00
# if __has _feature ( objc_arc )
2014-04-08 14:03:08 +08:00
GCDWebUploader * __unsafe _unretained server = self ;
2014-04-10 02:10:45 +08:00
# else
__block GCDWebUploader * server = self ;
# endif
2014-03-30 09:19:45 +08:00
// Resource files
[ self addGETHandlerForBasePath : @ "/" directoryPath : [ siteBundle resourcePath ] indexFilename : nil cacheAge : 3600 allowRangeRequests : NO ] ;
// Web page
[ self addHandlerForMethod : @ "GET" path : @ "/" requestClass : [ GCDWebServerRequest class ] processBlock : ^ GCDWebServerResponse * ( GCDWebServerRequest * request ) {
# if TARGET_OS _IPHONE
2014-04-02 04:13:22 +08:00
NSString * device = [ [ UIDevice currentDevice ] name ] ;
2014-03-30 09:19:45 +08:00
# else
2014-04-02 04:13:22 +08:00
# if __has _feature ( objc_arc )
NSString * device = CFBridgingRelease ( SCDynamicStoreCopyComputerName ( NULL , NULL ) ) ;
# else
NSString * device = [ ( id ) SCDynamicStoreCopyComputerName ( NULL , NULL ) autorelease ] ;
# endif
2014-03-30 09:19:45 +08:00
# endif
2014-04-08 14:03:08 +08:00
NSString * title = server . title ;
2014-04-02 07:06:44 +08:00
if ( title = = nil ) {
title = [ [ NSBundle mainBundle ] objectForInfoDictionaryKey : @ "CFBundleDisplayName" ] ;
2014-04-03 00:02:43 +08:00
# if ! TARGET_OS _IPHONE
if ( title = = nil ) {
title = [ [ NSProcessInfo processInfo ] processName ] ;
}
# endif
2014-04-02 07:06:44 +08:00
}
2014-04-08 14:03:08 +08:00
NSString * header = server . header ;
2014-04-02 07:06:44 +08:00
if ( header = = nil ) {
header = title ;
}
2014-04-08 14:03:08 +08:00
NSString * prologue = server . prologue ;
2014-04-02 07:06:44 +08:00
if ( prologue = = nil ) {
prologue = [ siteBundle localizedStringForKey : @ "PROLOGUE" value : @ "" table : nil ] ;
}
2014-04-08 14:03:08 +08:00
NSString * epilogue = server . epilogue ;
2014-04-02 07:06:44 +08:00
if ( epilogue = = nil ) {
epilogue = [ siteBundle localizedStringForKey : @ "EPILOGUE" value : @ "" table : nil ] ;
}
2014-04-08 14:03:08 +08:00
NSString * footer = server . footer ;
2014-04-02 04:13:22 +08:00
if ( footer = = nil ) {
2014-04-03 00:02:43 +08:00
NSString * name = [ [ NSBundle mainBundle ] objectForInfoDictionaryKey : @ "CFBundleDisplayName" ] ;
NSString * version = [ [ NSBundle mainBundle ] objectForInfoDictionaryKey : @ "CFBundleShortVersionString" ] ;
# if ! TARGET_OS _IPHONE
if ( ! name && ! version ) {
name = @ "OS X" ;
version = [ [ NSProcessInfo processInfo ] operatingSystemVersionString ] ;
}
# endif
footer = [ NSString stringWithFormat : [ siteBundle localizedStringForKey : @ "FOOTER_FORMAT" value : @ "" table : nil ] , name , version ] ;
2014-03-30 09:19:45 +08:00
}
return [ GCDWebServerDataResponse responseWithHTMLTemplate : [ siteBundle pathForResource : @ "index" ofType : @ "html" ]
variables : @ {
2014-04-02 07:06:44 +08:00
@ "device" : device ,
2014-03-30 09:19:45 +08:00
@ "title" : title ,
@ "header" : header ,
2014-04-02 07:06:44 +08:00
@ "prologue" : prologue ,
@ "epilogue" : epilogue ,
2014-03-30 09:19:45 +08:00
@ "footer" : footer
} ] ;
} ] ;
// File listing
[ self addHandlerForMethod : @ "GET" path : @ "/list" requestClass : [ GCDWebServerRequest class ] processBlock : ^ GCDWebServerResponse * ( GCDWebServerRequest * request ) {
2014-04-08 14:12:18 +08:00
return [ server listDirectory : request ] ;
2014-03-30 09:19:45 +08:00
} ] ;
// File download
[ self addHandlerForMethod : @ "GET" path : @ "/download" requestClass : [ GCDWebServerRequest class ] processBlock : ^ GCDWebServerResponse * ( GCDWebServerRequest * request ) {
2014-04-08 14:12:18 +08:00
return [ server downloadFile : request ] ;
2014-03-30 09:19:45 +08:00
} ] ;
// File upload
[ self addHandlerForMethod : @ "POST" path : @ "/upload" requestClass : [ GCDWebServerMultiPartFormRequest class ] processBlock : ^ GCDWebServerResponse * ( GCDWebServerRequest * request ) {
2014-04-08 14:12:18 +08:00
return [ server uploadFile : ( GCDWebServerMultiPartFormRequest * ) request ] ;
2014-04-02 04:13:22 +08:00
} ] ;
// File and folder moving
[ self addHandlerForMethod : @ "POST" path : @ "/move" requestClass : [ GCDWebServerURLEncodedFormRequest class ] processBlock : ^ GCDWebServerResponse * ( GCDWebServerRequest * request ) {
2014-04-08 14:12:18 +08:00
return [ server moveItem : ( GCDWebServerURLEncodedFormRequest * ) request ] ;
2014-03-30 09:19:45 +08:00
} ] ;
2014-04-08 14:12:18 +08:00
// File and folder deletion
2014-04-02 04:13:22 +08:00
[ self addHandlerForMethod : @ "POST" path : @ "/delete" requestClass : [ GCDWebServerURLEncodedFormRequest class ] processBlock : ^ GCDWebServerResponse * ( GCDWebServerRequest * request ) {
2014-04-08 14:12:18 +08:00
return [ server deleteItem : ( GCDWebServerURLEncodedFormRequest * ) request ] ;
2014-03-30 09:19:45 +08:00
} ] ;
2014-04-02 04:13:22 +08:00
// Directory creation
[ self addHandlerForMethod : @ "POST" path : @ "/create" requestClass : [ GCDWebServerURLEncodedFormRequest class ] processBlock : ^ GCDWebServerResponse * ( GCDWebServerRequest * request ) {
2014-04-08 14:12:18 +08:00
return [ server createDirectory : ( GCDWebServerURLEncodedFormRequest * ) request ] ;
2014-04-02 04:13:22 +08:00
} ] ;
2014-03-30 09:19:45 +08:00
}
return self ;
}
# if ! __has _feature ( objc_arc )
- ( void ) dealloc {
[ _uploadDirectory release ] ;
[ _allowedExtensions release ] ;
[ _title release ] ;
[ _header release ] ;
2014-04-02 07:06:44 +08:00
[ _prologue release ] ;
[ _epilogue release ] ;
2014-03-30 09:19:45 +08:00
[ _footer release ] ;
[ super dealloc ] ;
}
# endif
@ end
2014-04-02 06:45:51 +08:00
@ implementation GCDWebUploader ( Subclassing )
- ( BOOL ) shouldUploadFileAtPath : ( NSString * ) path withTemporaryFile : ( NSString * ) tempPath {
return YES ;
}
- ( BOOL ) shouldMoveItemFromPath : ( NSString * ) fromPath toPath : ( NSString * ) toPath {
return YES ;
}
2014-04-08 14:03:08 +08:00
- ( BOOL ) shouldDeleteItemAtPath : ( NSString * ) path {
return YES ;
}
- ( BOOL ) shouldCreateDirectoryAtPath : ( NSString * ) path {
return YES ;
}
2014-04-02 06:45:51 +08:00
@ end