Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
changed: refactored maximum-submission-size determination logic,
Browse files Browse the repository at this point in the history
closes kobotoolbox#341

- never store default max size in db (offline-capable views)
- always attempt to obtain max size from server if not stored in db (offline-capable views)
- only set default max size at 1 point in code (instead of 3)
- do not make submission/max-size request during previews without enketo ID
- log error to console if OpenRosa server does not return X-OpenRosa-Accept-Content-Length header
  • Loading branch information
MartijnR committed Feb 23, 2016
1 parent 4a8c940 commit 4c6be1f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion app/lib/communicator/communicator.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function getMaxSize( survey ) {

return _request( options )
.then( function( response ) {
return response.headers[ 'x-openrosa-accept-content-length' ] || 5 * 1024 * 1024;
return response.headers[ 'x-openrosa-accept-content-length' ];
} );
}

Expand Down
11 changes: 8 additions & 3 deletions public/js/src/main-webform-edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@ translator.init( survey )
_init( formParts );
} )
.then( connection.getMaximumSubmissionSize )
.then( function( maxSize ) {
settings.maxSize = maxSize;
} );
.then( _updateMaxSizeSetting );
} else {
throw new Error( t( 'error.unknown' ) );
}
} ).catch( _showErrorOrAuthenticate );

function _updateMaxSizeSetting( maxSize ) {
if ( maxSize ) {
// overwrite default max size
settings.maxSize = maxSize;
}
}

function _showErrorOrAuthenticate( error ) {
$loader.addClass( 'fail' );
if ( error.status === 401 ) {
Expand Down
13 changes: 9 additions & 4 deletions public/js/src/main-webform.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ if ( settings.offline ) {
.then( formCache.updateMaxSubmissionSize )
.then( formCache.updateMedia )
.then( function( s ) {
settings.maxSize = s.maxSize;
_updateMaxSizeSetting( s.maxSize );
_setFormCacheEventHandlers();
_setAppCacheEventHandlers();
appCache.init();
Expand All @@ -54,12 +54,17 @@ if ( settings.offline ) {
.then( _swapTheme )
.then( _init )
.then( connection.getMaximumSubmissionSize )
.then( function( maxSize ) {
settings.maxSize = maxSize;
} )
.then( _updateMaxSizeSetting )
.catch( _showErrorOrAuthenticate );
}

function _updateMaxSizeSetting( maxSize ) {
if ( maxSize ) {
// overwrite default max size
settings.maxSize = maxSize;
}
}

function _showErrorOrAuthenticate( error ) {
error = ( typeof error === 'string' ) ? new Error( error ) : error;
console.error( error, error.stack );
Expand Down
11 changes: 6 additions & 5 deletions public/js/src/module/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ var TRANSFORM_HASH_URL = '/transform/xform/hash';
var EXPORT_URL = '/export/get-url';
var INSTANCE_URL = ( settings.enketoId ) ? '/submission/' + settings.enketoIdPrefix + settings.enketoId : null;
var MAX_SIZE_URL = ( settings.enketoId ) ? '/submission/max-size/' + settings.enketoIdPrefix + settings.enketoId : null;
var DEFAULT_MAX_SIZE = 5 * 1024 * 1024;
var ABSOLUTE_MAX_SIZE = 100 * 1024 * 1024;

/**
Expand Down Expand Up @@ -310,7 +309,8 @@ function getMaximumSubmissionSize() {
var maxSubmissionSize;

return new Promise( function( resolve ) {
if ( MAX_SIZE_URL ) {

if ( MAX_SIZE_URL && settings.type !== 'preview' && settings.type !== 'app' ) {
$.ajax( MAX_SIZE_URL, {
type: 'GET',
timeout: 5 * 1000,
Expand All @@ -321,15 +321,16 @@ function getMaximumSubmissionSize() {
maxSubmissionSize = ( Number( response.maxSize ) > ABSOLUTE_MAX_SIZE ) ? ABSOLUTE_MAX_SIZE : Number( response.maxSize );
resolve( maxSubmissionSize );
} else {
console.error( 'Error retrieving maximum submission size. Unexpected response: ', response );
// Note that in /previews the MAX_SIZE_URL is null, which will immediately call this handler
resolve( DEFAULT_MAX_SIZE );
resolve( null );
}
} )
.fail( function() {
resolve( DEFAULT_MAX_SIZE );
resolve( null );
} );
} else {
resolve( DEFAULT_MAX_SIZE );
resolve( null );
}
} );
}
Expand Down
4 changes: 2 additions & 2 deletions public/js/src/module/form-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@ function updateMaxSubmissionSize( survey ) {
return connection.getMaximumSubmissionSize()
.then( function( maxSize ) {
survey.maxSize = maxSize;
return survey;
return store.survey.update( survey );
} );
} else {
return Promise.resolve( survey );
}
}

/**
* Loads survey resources either from the store or via HTTP (and stores them)
* Loads survey resources either from the store or via HTTP (and stores them).
*
* @param {[type]} survey [description]
* @return {Promise} [description]
Expand Down
7 changes: 7 additions & 0 deletions public/js/src/module/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var config = require( 'enketo-config' );
var queryParams = _getAllQueryParams();
var settings = {};
var DEFAULT_MAX_SIZE = 5 * 1024 * 1024;
var settingsMap = [ {
q: 'return',
s: 'returnUrl'
Expand Down Expand Up @@ -90,6 +91,12 @@ if ( settings.submissionParameter && settings.submissionParameter.name ) {
settings.submissionParameter.value = queryParams[ settings.submissionParameter.name ];
}

// set default maxSubmissionSize
settings.maxSize = DEFAULT_MAX_SIZE;

// add type
settings.type = ( window.location.pathname.indexOf( '/preview' ) === 0 ) ? 'preview' : 'other';

// add enketoId
settings.enketoIdPrefix = '::';
settings.enketoId = _getEnketoId( '\/' + settings.enketoIdPrefix, window.location.pathname ) || _getEnketoId( '#', window.location.hash );
Expand Down

0 comments on commit 4c6be1f

Please sign in to comment.