How to limit upload file type/format in GCDWebUploader

448 Views Asked by At

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">&times;</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);
      }
    },
1

There are 1 best solutions below

0
On

Well, I found how to do this today. It is very simple, just define allowedFileExtensionsproperty, which is provided in GCDWebUploader. In my case as below code snip.

/ / / define this property in your own class, then this question was solved.

// create webUploader, access with your iPhone's IP from computer's browser.
    static func initWebUploader() {
        webUploader.start()
        webUploader.allowedFileExtensions = ["aac", "mp3"]
        if webUploader.serverURL != nil {
            print("Visit \(webUploader.serverURL!) in your web browser")
        } else {
            print("You are not connected with Wifi")
        }
    }