From ebfbb97e66ef5d4c3dbe6b2eb23d19cba9ab74bd Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Wed, 8 Jul 2015 15:23:01 +0200 Subject: Fix parsing of sharetime as string In some cases the ajax/share.php will return the share time as string. If this is the case it would get parsed completely wrong and cause the share dropdown to not work anymore. This change will properly cast the string to an interger and also fallback if this is not possible. --- core/js/share.js | 17 +++++++++++++++++ core/js/tests/specs/shareSpec.js | 14 ++++++++++++++ 2 files changed, 31 insertions(+) (limited to 'core/js') diff --git a/core/js/share.js b/core/js/share.js index d730d3bbf6e..f767da18da0 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -819,6 +819,21 @@ OC.Share={ dirname:function(path) { return path.replace(/\\/g,'/').replace(/\/[^\/]*$/, ''); }, + /** + * Parses a string to an valid integer (unix timestamp) + * @param time + * @returns {*} + * @internal Only used to work around a bug in the backend + */ + _parseTime: function(time) { + if (_.isString(time)) { + time = parseInt(time, 10); + if(isNaN(time)) { + time = null; + } + } + return time; + }, /** * Displays the expiration date field * @@ -834,6 +849,8 @@ OC.Share={ minDate: minDate, maxDate: null }; + // TODO: hack: backend returns string instead of integer + shareTime = OC.Share._parseTime(shareTime); if (_.isNumber(shareTime)) { shareTime = new Date(shareTime * 1000); } diff --git a/core/js/tests/specs/shareSpec.js b/core/js/tests/specs/shareSpec.js index 4e12f3bb0cf..a8beb807ccc 100644 --- a/core/js/tests/specs/shareSpec.js +++ b/core/js/tests/specs/shareSpec.js @@ -1316,5 +1316,19 @@ describe('OC.Share tests', function() { }); }); }); + describe('OC.Share utils', function() { + it('parseTime should properly parse strings', function() { + + _.each([ + [ '123456', 123456], + [ 123456 , 123456], + ['0123456', 123456], + ['abcdefg', null], + ], function(value) { + expect(OC.Share._parseTime(value[0])).toEqual(value[1]); + }); + + }); + }); }); -- cgit v1.2.3 From 5a0d41048868eddd709c6e9bb71a8c5243d239c0 Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Tue, 28 Jul 2015 14:20:55 +0200 Subject: tests for _parseTime with hex and empty strings --- core/js/share.js | 4 ++++ core/js/tests/specs/shareSpec.js | 2 ++ 2 files changed, 6 insertions(+) (limited to 'core/js') diff --git a/core/js/share.js b/core/js/share.js index f767da18da0..99fd08c6411 100644 --- a/core/js/share.js +++ b/core/js/share.js @@ -827,6 +827,10 @@ OC.Share={ */ _parseTime: function(time) { if (_.isString(time)) { + // skip empty strings and hex values + if (time === '' || (time.length > 1 && time[0] === '0' && time[1] === 'x')) { + return null; + } time = parseInt(time, 10); if(isNaN(time)) { time = null; diff --git a/core/js/tests/specs/shareSpec.js b/core/js/tests/specs/shareSpec.js index a8beb807ccc..3dc25134f59 100644 --- a/core/js/tests/specs/shareSpec.js +++ b/core/js/tests/specs/shareSpec.js @@ -1324,6 +1324,8 @@ describe('OC.Share tests', function() { [ 123456 , 123456], ['0123456', 123456], ['abcdefg', null], + ['0x12345', null], + [ '', null], ], function(value) { expect(OC.Share._parseTime(value[0])).toEqual(value[1]); }); -- cgit v1.2.3