You are here

function advagg_file_saver in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg.module \advagg_file_saver()

Save a string to the specified destination. Verify that file size is not zero.

Parameters

$data: A string containing the contents of the file.

$dest: A string containing the destination location.

Return value

Boolean indicating if the file save was successful.

1 call to advagg_file_saver()
advagg_js_compress_file_saver in advagg_js_compress/advagg_js_compress.module
Save a string to the specified destination. Verify that file size is not zero.
1 string reference to 'advagg_file_saver'
advagg.module in ./advagg.module
Advanced CSS/JS aggregation module

File

./advagg.module, line 1220
Advanced CSS/JS aggregation module

Code

function advagg_file_saver($data, $dest, $force, $type) {

  // Make sure the tmp folder is ready for use
  $tmp = file_directory_temp();
  file_check_directory($tmp, FILE_CREATE_DIRECTORY);

  // Create the JS file.
  $file_save_data = 'file_save_data';
  $custom_path = variable_get('advagg_custom_files_dir', ADVAGG_CUSTOM_FILES_DIR);
  if (!empty($custom_path)) {
    $file_save_data = 'advagg_file_save_data';
  }
  if (!$file_save_data($data, $dest, FILE_EXISTS_REPLACE)) {
    return FALSE;
  }

  // Make sure filesize is not zero.
  advagg_clearstatcache(TRUE, $dest);
  if (@filesize($dest) == 0 && !empty($data)) {
    if (!$file_save_data($data, $dest, FILE_EXISTS_REPLACE)) {
      return FALSE;
    }
    advagg_clearstatcache(TRUE, $dest);
    if (@filesize($dest) == 0 && !empty($data)) {

      // Filename is bad, create a new one next time.
      file_delete($dest);
      return FALSE;
    }
  }
  if (variable_get('advagg_gzip_compression', ADVAGG_GZIP_COMPRESSION) && extension_loaded('zlib')) {
    $gzip_dest = $dest . '.gz';
    advagg_clearstatcache(TRUE, $gzip_dest);
    if (!file_exists($gzip_dest) || $force) {
      $gzip_data = gzencode($data, 9, FORCE_GZIP);
      if (!$file_save_data($gzip_data, $gzip_dest, FILE_EXISTS_REPLACE)) {
        return FALSE;
      }

      // Make sure filesize is not zero.
      advagg_clearstatcache(TRUE, $gzip_dest);
      if (@filesize($gzip_dest) == 0 && !empty($gzip_data)) {
        if (!$file_save_data($gzip_data, $gzip_dest, FILE_EXISTS_REPLACE)) {
          return FALSE;
        }
        advagg_clearstatcache(TRUE, $gzip_dest);
        if (@filesize($gzip_dest) == 0 && !empty($gzip_data)) {

          // Filename is bad, create a new one next time.
          file_delete($gzip_dest);
          return FALSE;
        }
      }
    }
  }

  // Make sure .htaccess file exists.
  advagg_htaccess_check_generate($dest);
  cache_set($dest, time(), 'cache_advagg', CACHE_PERMANENT);
  return TRUE;
}