Updated API
This commit is contained in:
@ -30,6 +30,8 @@
|
|||||||
@class GCDWebUploader;
|
@class GCDWebUploader;
|
||||||
|
|
||||||
@protocol GCDWebUploaderDelegate <NSObject>
|
@protocol GCDWebUploaderDelegate <NSObject>
|
||||||
|
@optional
|
||||||
|
- (void)webUploader:(GCDWebUploader*)uploader didDownloadFileAtPath:(NSString*)path;
|
||||||
- (void)webUploader:(GCDWebUploader*)uploader didUploadFileAtPath:(NSString*)path;
|
- (void)webUploader:(GCDWebUploader*)uploader didUploadFileAtPath:(NSString*)path;
|
||||||
- (void)webUploader:(GCDWebUploader*)uploader didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath;
|
- (void)webUploader:(GCDWebUploader*)uploader didMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath;
|
||||||
- (void)webUploader:(GCDWebUploader*)uploader didDeleteItemAtPath:(NSString*)path;
|
- (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)
|
@property(nonatomic, copy) NSString* footer; // Default is application name and version (text must be HTML escaped)
|
||||||
- (id)initWithUploadDirectory:(NSString*)path;
|
- (id)initWithUploadDirectory:(NSString*)path;
|
||||||
@end
|
@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
|
||||||
|
|||||||
@ -180,6 +180,11 @@
|
|||||||
if (isDirectory) {
|
if (isDirectory) {
|
||||||
return [GCDWebServerResponse responseWithStatusCode:400];
|
return [GCDWebServerResponse responseWithStatusCode:400];
|
||||||
} else {
|
} 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];
|
return [GCDWebServerFileResponse responseWithFile:absolutePath isAttachment:YES];
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -196,19 +201,24 @@
|
|||||||
NSString* contentType = (range.location != NSNotFound ? @"application/json" : @"text/plain; charset=utf-8");
|
NSString* contentType = (range.location != NSNotFound ? @"application/json" : @"text/plain; charset=utf-8");
|
||||||
|
|
||||||
GCDWebServerMultiPartFile* file = [[(GCDWebServerMultiPartFormRequest*)request files] objectForKey:@"files[]"];
|
GCDWebServerMultiPartFile* file = [[(GCDWebServerMultiPartFormRequest*)request files] objectForKey:@"files[]"];
|
||||||
NSString* fileName = file.fileName;
|
if ((![file.fileName hasPrefix:@"."] || uploader.showHiddenFiles) && [uploader _checkFileExtension:file.fileName]) {
|
||||||
if ((![fileName hasPrefix:@"."] || uploader.showHiddenFiles) && [uploader _checkFileExtension:fileName]) {
|
|
||||||
NSString* relativePath = [(GCDWebServerMultiPartArgument*)[[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"path"] string];
|
NSString* relativePath = [(GCDWebServerMultiPartArgument*)[[(GCDWebServerURLEncodedFormRequest*)request arguments] objectForKey:@"path"] string];
|
||||||
NSString* absolutePath = [uploader _uniquePathForPath:[[uploader.uploadDirectory stringByAppendingPathComponent:relativePath] stringByAppendingPathComponent:fileName]];
|
NSString* absolutePath = [uploader _uniquePathForPath:[[uploader.uploadDirectory stringByAppendingPathComponent:relativePath] stringByAppendingPathComponent:file.fileName]];
|
||||||
|
if ([uploader shouldUploadFileAtPath:absolutePath withTemporaryFile:file.temporaryPath]) {
|
||||||
NSError* error = nil;
|
NSError* error = nil;
|
||||||
if ([[NSFileManager defaultManager] moveItemAtPath:file.temporaryPath toPath:absolutePath error:&error]) {
|
if ([[NSFileManager defaultManager] moveItemAtPath:file.temporaryPath toPath:absolutePath error:&error]) {
|
||||||
|
if ([uploader.delegate respondsToSelector:@selector(webUploader:didUploadFileAtPath:)]) {
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[uploader.delegate webUploader:uploader didUploadFileAtPath:absolutePath];
|
[uploader.delegate webUploader:uploader didUploadFileAtPath:absolutePath];
|
||||||
});
|
});
|
||||||
|
}
|
||||||
return [GCDWebServerDataResponse responseWithJSONObject:@{} contentType:contentType];
|
return [GCDWebServerDataResponse responseWithJSONObject:@{} contentType:contentType];
|
||||||
} else {
|
} else {
|
||||||
return [GCDWebServerResponse responseWithStatusCode:500];
|
return [GCDWebServerResponse responseWithStatusCode:500];
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return [GCDWebServerResponse responseWithStatusCode:403];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return [GCDWebServerResponse responseWithStatusCode:400];
|
return [GCDWebServerResponse responseWithStatusCode:400];
|
||||||
}
|
}
|
||||||
@ -234,14 +244,20 @@
|
|||||||
return [GCDWebServerResponse responseWithStatusCode:400];
|
return [GCDWebServerResponse responseWithStatusCode:400];
|
||||||
}
|
}
|
||||||
NSString* newAbsolutePath = [uploader _uniquePathForPath:[uploader.uploadDirectory stringByAppendingPathComponent:newRelativePath]];
|
NSString* newAbsolutePath = [uploader _uniquePathForPath:[uploader.uploadDirectory stringByAppendingPathComponent:newRelativePath]];
|
||||||
|
if ([uploader shouldMoveItemFromPath:oldAbsolutePath toPath:newAbsolutePath]) {
|
||||||
if ([[NSFileManager defaultManager] moveItemAtPath:oldAbsolutePath toPath:newAbsolutePath error:NULL]) {
|
if ([[NSFileManager defaultManager] moveItemAtPath:oldAbsolutePath toPath:newAbsolutePath error:NULL]) {
|
||||||
|
if ([uploader.delegate respondsToSelector:@selector(webUploader:didMoveItemFromPath:toPath:)]) {
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[uploader.delegate webUploader:uploader didMoveItemFromPath:oldAbsolutePath toPath:newAbsolutePath];
|
[uploader.delegate webUploader:uploader didMoveItemFromPath:oldAbsolutePath toPath:newAbsolutePath];
|
||||||
});
|
});
|
||||||
|
}
|
||||||
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
|
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
|
||||||
} else {
|
} else {
|
||||||
return [GCDWebServerResponse responseWithStatusCode:500];
|
return [GCDWebServerResponse responseWithStatusCode:500];
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
return [GCDWebServerResponse responseWithStatusCode:403];
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return [GCDWebServerResponse responseWithStatusCode:404];
|
return [GCDWebServerResponse responseWithStatusCode:404];
|
||||||
}
|
}
|
||||||
@ -255,9 +271,11 @@
|
|||||||
NSString* absolutePath = [uploader.uploadDirectory stringByAppendingPathComponent:relativePath];
|
NSString* absolutePath = [uploader.uploadDirectory stringByAppendingPathComponent:relativePath];
|
||||||
if ([[NSFileManager defaultManager] fileExistsAtPath:absolutePath]) {
|
if ([[NSFileManager defaultManager] fileExistsAtPath:absolutePath]) {
|
||||||
if ([[NSFileManager defaultManager] removeItemAtPath:absolutePath error:NULL]) {
|
if ([[NSFileManager defaultManager] removeItemAtPath:absolutePath error:NULL]) {
|
||||||
|
if ([uploader.delegate respondsToSelector:@selector(webUploader:didDeleteItemAtPath:)]) {
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[uploader.delegate webUploader:uploader didDeleteItemAtPath:absolutePath];
|
[uploader.delegate webUploader:uploader didDeleteItemAtPath:absolutePath];
|
||||||
});
|
});
|
||||||
|
}
|
||||||
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
|
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
|
||||||
} else {
|
} else {
|
||||||
return [GCDWebServerResponse responseWithStatusCode:500];
|
return [GCDWebServerResponse responseWithStatusCode:500];
|
||||||
@ -281,9 +299,11 @@
|
|||||||
}
|
}
|
||||||
NSString* absolutePath = [uploader _uniquePathForPath:[uploader.uploadDirectory stringByAppendingPathComponent:relativePath]];
|
NSString* absolutePath = [uploader _uniquePathForPath:[uploader.uploadDirectory stringByAppendingPathComponent:relativePath]];
|
||||||
if ([[NSFileManager defaultManager] createDirectoryAtPath:absolutePath withIntermediateDirectories:YES attributes:nil error:NULL]) {
|
if ([[NSFileManager defaultManager] createDirectoryAtPath:absolutePath withIntermediateDirectories:YES attributes:nil error:NULL]) {
|
||||||
|
if ([uploader.delegate respondsToSelector:@selector(webUploader:didCreateDirectoryAtPath:)]) {
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[uploader.delegate webUploader:uploader didCreateDirectoryAtPath:absolutePath];
|
[uploader.delegate webUploader:uploader didCreateDirectoryAtPath:absolutePath];
|
||||||
});
|
});
|
||||||
|
}
|
||||||
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
|
return [GCDWebServerDataResponse responseWithJSONObject:@{}];
|
||||||
} else {
|
} else {
|
||||||
return [GCDWebServerResponse responseWithStatusCode:500];
|
return [GCDWebServerResponse responseWithStatusCode:500];
|
||||||
@ -310,3 +330,15 @@
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
@implementation GCDWebUploader (Subclassing)
|
||||||
|
|
||||||
|
- (BOOL)shouldUploadFileAtPath:(NSString*)path withTemporaryFile:(NSString*)tempPath {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)shouldMoveItemFromPath:(NSString*)fromPath toPath:(NSString*)toPath {
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
|
@end
|
||||||
|
|||||||
Reference in New Issue
Block a user