kostysh / File-uploading-component-for-Sencha-Touch

Ext.Button based component for uploading files in Sencha Touch apps without page refresh
51 stars 26 forks source link

Add the ability to sign a request before it is sent out, and also decode the response. #13

Closed travist closed 10 years ago

travist commented 10 years ago

I am working on interfacing this with Drupal file services. They just recently made a change to where every request must be signed before it can be sent to the server with an Authentication token to stop XSRF. When they made this change, the file uploads stopped working. I made a change to your library that allows a derived class to sign a request before it is sent out, and then properly decode the response when it comes back...

Ext.define('DrupalTouch.ux.DrupalFile', {
  extend: 'Ext.ux.Fileup',
  xtype: 'drupalfile',
  requires: [
    'Ext.ux.Fileup'
  ],
  file: null,
  signRequest: function(http, callback) {
    Ext.Ajax.request({
      url: '/services/session/token',
      method: 'GET',
      success: function(response) {
        http.setRequestHeader("X-CSRF-Token", response.responseText);
        callback(http);
      }
    });
  },
  decodeResponse: function(response) {
    if (response.responseText) {

      // Get the drupal file from the response text.
      this.file = Ext.JSON.decode(response.responseText);
      return {success: true};
    }
    else {
      return {
        success: false,
        message: 'File upload failed'
      }
    }
  }
});

This PR adds this ability. I appreciate this library.

kostysh commented 10 years ago

Thank you for this improvement. I think - it will be better to make this feature config-depended. Because in most cases request signing is not needed. I will add config for feature to the next version.

kostysh commented 10 years ago

Just pushed changes. Added request signing feature (but config-depended). For using this feature now you can set signRequestEnabled property to true value, and redefine singProvider method as you want (with Ajax request, for example).