You are here

function advagg_js_compress_file_saver in Advanced CSS/JS Aggregation 6

Same name and namespace in other branches
  1. 7 advagg_js_compress/advagg_js_compress.module \advagg_js_compress_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 string reference to 'advagg_js_compress_file_saver'
advagg_js_compress_init in advagg_js_compress/advagg_js_compress.module
Implement hook_init.

File

advagg_js_compress/advagg_js_compress.module, line 512
Advanced CSS/JS aggregation js compression module.

Code

function advagg_js_compress_file_saver($data, $dest, $force, $type) {
  if ($type == 'css') {
    return advagg_file_saver($data, $dest, $force, $type);
  }
  if (!variable_get('advagg_gzip_compression', ADVAGG_GZIP_COMPRESSION) || !extension_loaded('zlib')) {
    return advagg_file_saver($data, $dest, $force, $type);
  }

  // Get file save function
  $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';
  }

  // Gzip first.
  $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;
      }
    }
  }

  // Use packer on JS data.
  advagg_js_compress_jspacker($data);

  // Write File.
  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;
    }
  }

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