Updated API

This commit is contained in:
Pierre-Olivier Latour 2014-04-01 15:45:51 -07:00
parent 74f99167da
commit 0bad724f08
2 changed files with 61 additions and 22 deletions

View File

@ -30,6 +30,8 @@
@class GCDWebUploader;
@protocol GCDWebUploaderDelegate <NSObject>
@optional
- (void)webUploader:(GCDWebUploader*)uploader didDownloadFileAtPath:(NSString*)path;
- (void)webUploader:(GCDWebUploader*)uploader didUploadFileAtPath:(NSString*)path;
- (void)webUploader:(GCDWebUploader*)uploader didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath;
- (void)webUploader:(GCDWebUploader*)uploader didDeleteItemAtPath:(NSString*)path;
@ -46,3 +48,8 @@
@property(nonatomic, copy) NSString* footer; // Default is application name and version (text must be HTML escaped)
- (id)initWithUploadDirectory:(NSString*)path;
@end
@interface GCDWebUploader (Subclassing)
- (BOOL)shouldUploadFileAtPath:(NSString*)path withTemporaryFile:(NSString*)tempPath; // Default implementation returns YES
- (BOOL)shouldMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath; // Default implementation returns YES
@end

View File

@ -180,6 +180,11 @@
if (isDirectory) {
return [GCDWebServerResponse responseWithStatusCode:400];
} else {
if ([uploader.delegate respondsToSelector:@selector(webUploader:didDownloadFileAtPath: )]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didDownloadFileAtPath:absolutePath];
});
}
return [GCDWebServerFileResponse responseWithFile:absolutePath isAttachment:YES];
}
} else {
@ -196,18 +201,23 @@
NSString* contentType = (range.location != NSNotFound ? @"application/json" : @"text/plain; charset=utf-8");
GCDWebServerMultiPartFile* file = [[(GCDWebServerMultiPartFormRequest*)request files] objectForKey:@"files[]"];
NSString* fileName = file.fileName;
if ((![fileName hasPrefix:@"."] || uploader.showHiddenFiles) && [uploader _checkFileExtension:fileName]) {
if ((![file.fileName hasPrefix:@"."] || uploader.showHiddenFiles) && [uploader _checkFileExtension:file.fileName]) {
NSString* relativePath = [(GCDWebServerMultiPartArgument*)[[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"path"] string];
NSString* absolutePath = [uploader _uniquePathForPath:[[uploader.uploadDirectory stringByAppendingPathComponent:relativePath] stringByAppendingPathComponent:fileName]];
NSError* error = nil;
if ([[NSFileManager defaultManager] moveItemAtPath:file.temporaryPath toPath:absolutePath error:&error]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didUploadFileAtPath:absolutePath];
});
return [GCDWebServerDataResponse responseWithJSONObject:@{} contentType:contentType];
NSString* absolutePath = [uploader _uniquePathForPath:[[uploader.uploadDirectory stringByAppendingPathComponent:relativePath] stringByAppendingPathComponent:file.fileName]];
if ([uploader shouldUploadFileAtPath:absolutePath withTemporaryFile:file.temporaryPath]) {
NSError* error = nil;
if ([[NSFileManager defaultManager] moveItemAtPath:file.temporaryPath toPath:absolutePath error:&error]) {
if ([uploader.delegate respondsToSelector:@selector(webUploader:didUploadFileAtPath:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didUploadFileAtPath:absolutePath];
});
}
return [GCDWebServerDataResponse responseWithJSONObject:@{} contentType:contentType];
} else {
return [GCDWebServerResponse responseWithStatusCode:500];
}
} else {
return [GCDWebServerResponse responseWithStatusCode:500];
return [GCDWebServerResponse responseWithStatusCode:403];
}
} else {
return [GCDWebServerResponse responseWithStatusCode:400];
@ -234,13 +244,19 @@
return [GCDWebServerResponse responseWithStatusCode:400];
}
NSString* newAbsolutePath = [uploader _uniquePathForPath:[uploader.uploadDirectory stringByAppendingPathComponent:newRelativePath]];
if ([[NSFileManager defaultManager] moveItemAtPath:oldAbsolutePath toPath:newAbsolutePath error:NULL]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didMoveItemFromPath:oldAbsolutePath toPath:newAbsolutePath];
});
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
if ([uploader shouldMoveItemFromPath:oldAbsolutePath toPath:newAbsolutePath]) {
if ([[NSFileManager defaultManager] moveItemAtPath:oldAbsolutePath toPath:newAbsolutePath error:NULL]) {
if ([uploader.delegate respondsToSelector:@selector(webUploader:didMoveItemFromPath:toPath:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didMoveItemFromPath:oldAbsolutePath toPath:newAbsolutePath];
});
}
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
} else {
return [GCDWebServerResponse responseWithStatusCode:500];
}
} else {
return [GCDWebServerResponse responseWithStatusCode:500];
return [GCDWebServerResponse responseWithStatusCode:403];
}
} else {
return [GCDWebServerResponse responseWithStatusCode:404];
@ -255,9 +271,11 @@
NSString* absolutePath = [uploader.uploadDirectory stringByAppendingPathComponent:relativePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:absolutePath]) {
if ([[NSFileManager defaultManager] removeItemAtPath:absolutePath error:NULL]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didDeleteItemAtPath:absolutePath];
});
if ([uploader.delegate respondsToSelector:@selector(webUploader:didDeleteItemAtPath:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didDeleteItemAtPath:absolutePath];
});
}
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
} else {
return [GCDWebServerResponse responseWithStatusCode:500];
@ -281,9 +299,11 @@
}
NSString* absolutePath = [uploader _uniquePathForPath:[uploader.uploadDirectory stringByAppendingPathComponent:relativePath]];
if ([[NSFileManager defaultManager] createDirectoryAtPath:absolutePath withIntermediateDirectories:YES attributes:nil error:NULL]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didCreateDirectoryAtPath:absolutePath];
});
if ([uploader.delegate respondsToSelector:@selector(webUploader:didCreateDirectoryAtPath:)]) {
dispatch_async(dispatch_get_main_queue(), ^{
[uploader.delegate webUploader:uploader didCreateDirectoryAtPath:absolutePath];
});
}
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
} else {
return [GCDWebServerResponse responseWithStatusCode:500];
@ -310,3 +330,15 @@
#endif
@end
@implementation GCDWebUploader (Subclassing)
- (BOOL)shouldUploadFileAtPath:(NSString*)path withTemporaryFile:(NSString*)tempPath {
return YES;
}
- (BOOL)shouldMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
return YES;
}
@end