My app involves GCDWebUploader for audio files uploading to iPhone's sandbox. It is working well but I want to limit the uploading file type/format that user could upload, e.g. xxx.doc, xxx.jpg are forbidden for uploading. Currently I want to allow only .MP3 and .AAC format.
I found this uploadFile
method in GCDWebUploader.m, maybe this is what I need, to override this method in another class. But it is in objective-C, how could I use it in Swift? Also what I need to change accordingly in the html
and js
file. Any help and hint is welcomed.
/ / / uploadFile
method in GCDWebUploader.m
- (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 firstFileForControlName:@"files[]"];
if ((!_allowHiddenItems && [file.fileName hasPrefix:@"."]) || ![self _checkFileExtension:file.fileName]) {
return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_Forbidden message:@"Uploaded file name \"%@\" is not allowed", file.fileName];
}
NSString* relativePath = [[request firstArgumentForControlName:@"path"] string];
NSString* absolutePath = [self _uniquePathForPath:[[_uploadDirectory stringByAppendingPathComponent:GCDWebServerNormalizePath(relativePath)] stringByAppendingPathComponent:file.fileName]];
if (![self shouldUploadFileAtPath:absolutePath withTemporaryFile:file.temporaryPath]) {
return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_Forbidden message:@"Uploading file \"%@\" to \"%@\" is not permitted", file.fileName, relativePath];
}
/ / / Alert in index.html, it may use for alert of uploading warning.
<script type="text/x-tmpl" id="template-alert">
<div class="alert alert-{%=o.level%} alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{%=o.title%}</strong>{%=o.description%}
</div>
</script>
/ / / code in index.js
fail: function(e, data) {
var file = data.files[0];
if (data.errorThrown != "abort") {
_showError("Failed uploading \"" + file.name + "\" to \"" + _path + "\"", data.textStatus, data.errorThrown);
}
},
Well, I found how to do this today. It is very simple, just define
allowedFileExtensions
property, which is provided in GCDWebUploader. In my case as below code snip./ / / define this property in your own class, then this question was solved.