Skip to content

Commit

Permalink
Utils: Added atobUtf() btoaUtf() atobUrlsafe() btoaUrlsafe()
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed Nov 14, 2014
1 parent 19a5465 commit cb2fcb9
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
26 changes: 24 additions & 2 deletions src/javascript/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,42 @@
return (myNav.indexOf('msie') !== -1) ? parseInt(myNav.split('msie')[1], 10) : false;
};

OSjs.Utils.urlsafe_b64encode = function(str) {
OSjs.Utils.btoaUrlsafe = function(str) { // Encode
return (!str || !str.length) ? '' : btoa(str)
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/=+$/, '');
};
OSjs.Utils.urlsafe_b64decode = function(str) {
OSjs.Utils.atobUrlsafe = function(str) { // Decode
if ( str && str.length ) {
str = (str + '===').slice(0, str.length + (str.length % 4));
return atob(str.replace(/-/g, '+').replace(/_/g, '/'));
}
return '';
};

OSjs.Utils.btoaUtf = function(str) { // Encode
var _unescape = window.unescape || function(s) {
function d(x, n) {
return String.fromCharCode(parseInt(n, 16));
}
return s.replace(/%([0-9A-F]{2})/i, d);
};
return btoa(_unescape(encodeURIComponent(str)));
};

OSjs.Utils.atobUtf = function(str) { // Decode
var _escape = window.escape || function(s) {
function q(c) {
c = c.charCodeAt();
return '%' + (c<16 ? '0' : '') + c.toString(16).toUpperCase();
}
return s.replace(/[\x00-),:-?[-^`{-\xFF]/g, q);
};

return decodeURIComponent(_escape(atob(str)));
};

OSjs.Utils.getUserLocale = function() {
var loc = (window.navigator.userLanguage || window.navigator.language) || 'en-EN';
var map = {
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/vfs/googledrive.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
title: file.filename,
mimeType: contentType
};
var base64Data = btoa(unescape(encodeURIComponent(result)));
var base64Data = Utils.btoaUtf(result);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/vfs/osjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
if ( error ) {
return callback(error);
}
callback(false, dataSource ? result : decodeURIComponent(escape(atob(result))));
callback(false, dataSource ? result : Utils.atobUtf(result));
});
};
OSjsStorage.copy = function(src, dest, callback) {
Expand Down
2 changes: 1 addition & 1 deletion src/javascript/vfs/public.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
if ( error ) {
return callback(error);
}
callback(false, dataSource ? result : decodeURIComponent(escape(atob(result))));
callback(false, dataSource ? result : Utils.atobUtf(result));
});
};
PublicStorage.copy = function(src, dest, callback) {
Expand Down

0 comments on commit cb2fcb9

Please sign in to comment.