B2G/gaia/apps/browser/js/browser.js
// Saves a media file to device storage.
Saves a media file to device storage.
Saves a media file to device storage .
function browser_saveMedia(url, type) {
    function displayMessage(message) {
      var status = document.getElementById('save-media-status');
      status.firstElementChild.textContent = message;
      status.classList.add('visible');
      window.setTimeout(function() {
        status.classList.remove('visible');
      }, 3000);
    }
    function storeBlob(blob, name, retryCount) {
      var storageTypeMap = {
        'image': 'pictures',
        'video': 'videos',
        'audio': 'music'
      };
      var blobType = blob.type.split('/')[0];
      var storageType = storageTypeMap[blobType];
      if (!storageType) {
        displayMessage(_('error-saving-' + type));
        return;
      }
      var storage = navigator.getDeviceStorage(storageType);
      var addreq = storage.addNamed(blob, name);
      addreq.onsuccess = function() {
        displayMessage(_(type + '-saved'));
      };
      addreq.onerror = function() {
        if (addreq.error.name === 'NoModificationAllowedError' &&
            retryCount !== Browser.MAX_SAVING_RETRIES) {
          name = Date.now() + '-' + name;
          storeBlob(blob, name, retryCount + 1);
        } else {
          displayMessage(_('error-saving-' + type));
        }
      };
    }
    var xhr = new XMLHttpRequest({mozSystem: true});
    xhr.open('GET', url, true);
    xhr.responseType = 'blob';
    xhr.onload = function browser_mediaDataListener() {
      if (xhr.status !== 200 || !xhr.response) {
        displayMessage(_('error-saving-' + type));
        return;
      }
      var name = url.split('/').reverse()[0].toLowerCase().split(/[&?#]/g)[0]
                    .replace(/[^a-z0-9\.]/g, '_');
      var ext = MimeMapper.guessExtensionFromType(xhr.response.type);
      if (ext && name.indexOf(ext) === -1) {
        name += '.' + ext;
      }
      storeBlob(xhr.response, name, 0);
    };
    xhr.onerror = function getDefaultDataError() {
      displayMessage(_('error-saving-' + type));
    };
    xhr.send();
  }
function browser_saveMedia ( url , type ) { function displayMessage ( message ) { var status = document . getElementById ( 'save-media-status' ) ; status . firstElementChild . textContent = message ; status . classList . add ( 'visible' ) ; window . setTimeout ( function ( ) { status . classList . remove ( 'visible' ) ; } , 3000 ) ; } function storeBlob ( blob , name , retryCount ) { var storageTypeMap = { 'image' : 'pictures' , 'video' : 'videos' , 'audio' : 'music' } ; var blobType = blob . type . split ( '/' ) [ 0 ] ; var storageType = storageTypeMap [ blobType ] ; if ( ! storageType ) { displayMessage ( _ ( 'error-saving-' + type ) ) ; return ; } var storage = navigator . getDeviceStorage ( storageType ) ; var addreq = storage . addNamed ( blob , name ) ; addreq . onsuccess = function ( ) { displayMessage ( _ ( type + '-saved' ) ) ; } ; addreq . onerror = function ( ) { if ( addreq . error . name === 'NoModificationAllowedError' && retryCount !== Browser . MAX_SAVING_RETRIES ) { name = Date . now ( ) + '-' + name ; storeBlob ( blob , name , retryCount + 1 ) ; } else { displayMessage ( _ ( 'error-saving-' + type ) ) ; } } ; } var xhr = new XMLHttpRequest ( { mozSystem : true } ) ; xhr . open ( 'GET' , url , true ) ; xhr . responseType = 'blob' ; xhr . onload = function browser_mediaDataListener ( ) { if ( xhr . status !== 200 || ! xhr . response ) { displayMessage ( _ ( 'error-saving-' + type ) ) ; return ; } var name = url . split ( '/' ) . reverse ( ) [ 0 ] . toLowerCase ( ) . split ( / [&?#] / g ) [ 0 ] . replace ( / [^a-z0-9\.] / g , '_' ) ; var ext = MimeMapper . guessExtensionFromType ( xhr . response . type ) ; if ( ext && name . indexOf ( ext ) === - 1 ) { name += '.' + ext ; } storeBlob ( xhr . response , name , 0 ) ; } ; xhr . onerror = function getDefaultDataError ( ) { displayMessage ( _ ( 'error-saving-' + type ) ) ; } ; xhr . send ( ) ; }
Saves a media file to device storage.
Saves a media file to device storage .
/*
       * XXX: Bug 852864 - DeviceStorage addNamed failed with TypeMismatchError
       * 3gp and ogg types of audio files are returned as blobs of video type.
       * The workaround here is saving the blob to corresponding storage based
       * on the type of it instead of the type specified by users. Which allows
       * users able to save those types of audio files.
       */ // Prepend some always changing id and try to store again, but give up // after MAX_SAVING_RETRIES retries. // Save the blob to device storage. // Extract a filename from the URL, and to some sanitizing. // If we have no file extension, use the content-type header to // add one.