face-recognition.js/public_html/js/lib/permanent-storage.js

99 lines
2.5 KiB
JavaScript
Raw Normal View History

/**************************
* PermanentStorage *
* 02-11-16 *
***************************
* Designed & Developed by *
* xdrm-brackets *
* & *
* SeekDaSky *
***************************
* https://xdrm.io/ *
* http://seekdasky.ovh/ *
**************************/
var PermanentStorage = function(){};
PermanentStorage.prototype = {
/* STORES DATA TO REMOTE STORAGE
*
* @raw_data<String> Raw data to store
* @callback<Function> Callback function that'll take the @status as result
*
* @return status<Boolean> If no error
*
* @note: @callback will be called with `this` bound to `xhr.responseText`
*
*/
store: function(raw_data, callback){
/* (0) Initialization */
var fd, xhr;
{ /* (1) Checks @raw_data argument */
raw_data = raw_data || null;
raw_data = typeof raw_data === 'string' ? raw_data : null;
if( !raw_data )
return false;
}
{ /* (2) Checks @callback argument */
if( !(callback instanceof Function) )
return false;
}
{ /* (3) Format data and wrap it into FormData */
fd = new FormData();
2016-11-03 09:36:23 +00:00
fd.append('command', 'store');
fd.append('data', raw_data);
}
{ /* (4) Sends data to server */
xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttpRequest');
xhr.addEventListener('readystatechange', function(e){
2016-11-03 09:36:23 +00:00
!(xhr.readyState-4) && !!~[0,200].indexOf(xhr.status) && ( xhr.responseText == 'success' && callback() || console.warn('[PermanentStorage] storeError') );
}, false);
xhr.open( 'POST', './permanent-storage/', true );
xhr.send( fd );
}
2016-11-03 09:36:23 +00:00
return true;
},
/* FETCHES DATA FROM REMOTE STORAGE
*
* @callback<Function> Callback function that'll take the @fetched_data as result
*
* @return raw_data<String> Fetched data
*
*/
2016-11-03 09:36:23 +00:00
fetch: function(callback){
/* (0) Initialization */
2016-11-03 09:36:23 +00:00
var xhr, fd;
{ /* (1) Checks @callback argument */
if( !(callback instanceof Function) )
return false;
}
2016-11-03 09:36:23 +00:00
{ /* (2) Set FormData */
fd = new FormData();
fd.append('command', 'fetch');
}
{ /* (2) Sends data to server */
xhr = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHttpRequest');
xhr.addEventListener('readystatechange', function(e){
2016-11-03 09:36:23 +00:00
!(xhr.readyState-4) && !!~[0,200].indexOf(xhr.status) && ( xhr.responseText != 'error' && callback(xhr.responseText) || console.warn('[PermanentStorage] fetchError') );
}, false);
xhr.open( 'POST', './permanent-storage/', true );
2016-11-03 09:36:23 +00:00
xhr.send( fd );
}
}
};