TfeServer

Blog de tfe, Perl, Web et Libre...?

Override a file in FirefoxOS/DeviceStorage

PubliƩ le 22/07/2015 dans FirefoxOS

Apaprently, there is no way to directly override a file in DeviceStorage API.

So, the easiest way to override a file is to remove it, then created a new one:


/* Parameters: blob content, and file destination(string) */
function override_file(blob,file)
{
 return new Promise(function(ok, reject)
    {
 var sdcard = navigator.getDeviceStorage('sdcard');

        // Check if existing file exists
        request = sdcard.delete(file);

        // file existed
        request.onsuccess = function() {
            var request = sdcard.addNamed(blob, file);
            request.onsuccess = ok;
            request.onerror = reject
        };

        // file was not existing, but creating a new one anyway
        request.onerror = function() {
            console.log('creating new file! file');
            var request = sdcard.addNamed(blob, file);
            request.onsuccess = ok;
            request.onerror = reject;
        };
   });
}