Implements hook_s3fs_upload_params_alter().

Set headers for advagg files.

Related topics

File

./advagg.module, line 1316

Code

function advagg_s3fs_upload_params_alter(&$upload_params) {
    // Get advagg dir.
    list($css_path, $js_path) = advagg_get_root_files_dir();
    $scheme_css = file_uri_scheme($css_path[1]);
    if ($scheme_css) {
        $css_path_dir = str_replace("{$scheme_css}://", '', $css_path[1]);
    }
    else {
        $css_path_dir = ltrim($css_path[1], '/');
    }
    $scheme_js = file_uri_scheme($js_path[1]);
    if ($scheme_js) {
        $js_path_dir = str_replace("{$scheme_js}://", '', $js_path[1]);
    }
    else {
        $js_path_dir = ltrim($js_path[1], '/');
    }
    // Get file type in advagg dir, css or js.
    $type = '';
    if (strpos($upload_params['Bucket'] . '/' . $upload_params['Key'], $css_path_dir) !== FALSE) {
        $type = 'css';
    }
    if (strpos($upload_params['Bucket'] . '/' . $upload_params['Key'], $js_path_dir) !== FALSE) {
        $type = 'js';
    }
    if ($js_path_dir === $css_path_dir && !empty($type)) {
        $pathinfo = pathinfo($upload_params['Key']);
        if ($pathinfo['extension'] === 'gz') {
            $pathinfo = pathinfo($pathinfo['filename']);
        }
        $type = $pathinfo['extension'];
    }
    if (empty($type)) {
        // Only change advagg files.
        return;
    }
    // Cache control is 52 weeeks.
    if (variable_get('advagg_resource_hints_use_immutable', ADVAGG_RESOURCE_HINTS_USE_IMMUTABLE)) {
        $upload_params['CacheControl'] = 'max-age=31449600, public, immutable';
    }
    else {
        $upload_params['CacheControl'] = 'max-age=31449600, public';
    }
    // Expires in 365 days.
    $upload_params['Expires'] = gmdate('D, d M Y H:i:s \\G\\M\\T', REQUEST_TIME + 365 * 24 * 60 * 60);
    // The extension is .css or .js.
    $pathinfo = pathinfo($upload_params['Key']);
    if ($pathinfo['extension'] === $type) {
        if (variable_get('advagg_gzip', ADVAGG_GZIP)) {
            // Set gzip.
            $upload_params['ContentEncoding'] = 'gzip';
        }
        elseif (variable_get('advagg_brotli', ADVAGG_BROTLI)) {
            // Set br.
            $upload_params['ContentEncoding'] = 'br';
        }
    }
}