B2G/gaia/apps/email/js/ext/mailapi/worker-bootstrap.js
/**
   * Save an attachment to device storage, making the filename unique if we
   * encounter a collision.
   */
Save an attachment to device storage, making the filename unique if we
encounter a collision.
Save an attachment to device storage making the filename unique if we encounter a collision .
function saveToStorage(blob, storage, filename, partInfo, isRetry) {
    pendingStorageWrites++;
    var callback = function(success, error, savedFilename) {
      if (success) {
        self._LOG.savedAttachment(storage, blob.type, blob.size);
        console.log('saved attachment to', storage, savedFilename, 'type:', blob.type);
        partInfo.file = [storage, savedFilename];
        if (--pendingStorageWrites === 0)
          done();
      } else {
        self._LOG.saveFailure(storage, blob.type, error, filename);
        console.warn('failed to save attachment to', storage, filename,
                     'type:', blob.type);
        pendingStorageWrites--;
        if (isRetry) {
          if (pendingStorageWrites === 0)
            done();
          return;
        }
        var idxLastPeriod = filename.lastIndexOf('.');
        if (idxLastPeriod === -1)
          idxLastPeriod = filename.length;
        filename = filename.substring(0, idxLastPeriod) + '-' + Date.now() +
                    filename.substring(idxLastPeriod);
        saveToStorage(blob, storage, filename, partInfo, true);
      }
    };
    sendMessage('save', [storage, blob, filename], callback);
  }
function saveToStorage ( blob , storage , filename , partInfo , isRetry ) { pendingStorageWrites ++ ; var callback = function ( success , error , savedFilename ) { if ( success ) { self . _LOG . savedAttachment ( storage , blob . type , blob . size ) ; console . log ( 'saved attachment to' , storage , savedFilename , 'type:' , blob . type ) ; partInfo . file = [ storage , savedFilename ] ; if ( -- pendingStorageWrites === 0 ) done ( ) ; } else { self . _LOG . saveFailure ( storage , blob . type , error , filename ) ; console . warn ( 'failed to save attachment to' , storage , filename , 'type:' , blob . type ) ; pendingStorageWrites -- ; if ( isRetry ) { if ( pendingStorageWrites === 0 ) done ( ) ; return ; } var idxLastPeriod = filename . lastIndexOf ( '.' ) ; if ( idxLastPeriod === - 1 ) idxLastPeriod = filename . length ; filename = filename . substring ( 0 , idxLastPeriod ) + '-' + Date . now ( ) + filename . substring ( idxLastPeriod ) ; saveToStorage ( blob , storage , filename , partInfo , true ) ; } } ; sendMessage ( 'save' , [ storage , blob , filename ] , callback ) ; }
Save an attachment to device storage, making the filename unique if we
 encounter a collision.
Save an attachment to device storage making the filename unique if we encounter a collision .
// if we failed to unique the file after appending junk, just give up // retry by appending a super huge timestamp to the file before its // extension.