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

99 lines
2.5 KiB
JavaScript

/**************************
* 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();
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){
!(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 );
}
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
*
*/
fetch: function(callback){
/* (0) Initialization */
var xhr, fd;
{ /* (1) Checks @callback argument */
if( !(callback instanceof Function) )
return false;
}
{ /* (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){
!(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 );
xhr.send( fd );
}
}
};