function imageinfo_cache_shutdown_async in Imageinfo Cache 6
Same name and namespace in other branches
- 6.2 imageinfo_cache.module \imageinfo_cache_shutdown_async()
Function that gets called right after a file has been uploaded.
Parameters
$file: object File info
$op: string insert or delete
3 calls to imageinfo_cache_shutdown_async()
- imageinfo_cache_file_delete in ./
imageinfo_cache.module - Implementation of hook_file_delete().
- imageinfo_cache_file_insert in ./
imageinfo_cache.module - Implementation of hook_file_insert().
- imageinfo_cache_nodeapi in ./
imageinfo_cache.module - Implementation of hook_nodeapi().
File
- ./
imageinfo_cache.module, line 148 - Cache image info for theme_imagecache & theme_imagefield_image.
Code
function imageinfo_cache_shutdown_async($file = NULL, $op = NULL) {
global $base_path;
static $files = array();
static $registered = FALSE;
static $array_counter = 0;
static $file_counter = 0;
// Record file and op in static array & register shutdown function if needed.
if (!empty($file) && !empty($op)) {
// Make sure file has the correct info and it is an image.
if (imageinfo_cache_check_file($file, $op) == FALSE) {
return;
}
// Only send if file is missing from any one of the caches
$missing = FALSE;
$cid = 'theme_imagefield_' . md5($file->filepath);
$cache = cache_get($cid, 'cache_imageinfo');
if (empty($cache)) {
$missing = TRUE;
}
// Do imagecache presets.
if (!$missing && module_exists('imagecache') && function_exists('imagecache_generate_image') && variable_get('imageinfo_cache_imagecache_pregenerate', IMAGEINFO_CACHE_IMAGECACHE_PREGENERATE)) {
// Clear the imagecache cache.
foreach (imagecache_presets() as $preset) {
// Remove the image.
$cid = 'imagecache_' . $preset['presetname'] . '_' . md5($file->filepath);
$cache = cache_get($cid, 'cache_imageinfo');
if (empty($cache)) {
$missing = TRUE;
break;
}
}
}
// All data for this image is cached for insert.
// None of the data for this image is cached for delete.
if (!$missing && $op == 'insert') {
return;
}
// Only send 5 files at a time by default.
$file_counter++;
if ($file_counter > variable_get('imageinfo_cache_async_max', IMAGEINFO_CACHE_ASYNC_MAX)) {
$file_counter = 0;
$array_counter++;
}
// Add info to static array.
$files[$array_counter][$file->fid] = array(
'fid' => $file->fid,
'filepath' => $file->filepath,
'timestamp' => $file->timestamp,
'op' => $op,
);
if (!empty($file->field['type_name'])) {
$files[$array_counter][$file->fid]['field']['type_name'] = $file->field['type_name'];
}
if (!empty($file->field['field_name'])) {
$files[$array_counter][$file->fid]['field']['field_name'] = $file->field['field_name'];
}
if (!$registered) {
register_shutdown_function(__FUNCTION__);
$registered = TRUE;
}
return;
}
// URL key.
$key = variable_get('imageinfo_cache_url_key', FALSE);
if ($key == FALSE) {
variable_set('imageinfo_cache_url_key', mt_rand());
$key = variable_get('imageinfo_cache_url_key', FALSE);
}
foreach ($files as $values) {
$query['files'] = $values;
$query['key'] = $key;
// If asynchronous image operations are diabled then block here and return.
if (variable_get('imageinfo_cache_async', IMAGEINFO_CACHE_ASYNC) == FALSE) {
imageinfo_cache_primer($query);
continue;
}
// Setup request URL and headers.
$query_string = http_build_query($query, '', '&');
$ip = variable_get('imageinfo_cache_server_addr', FALSE);
if (empty($ip)) {
$ip = $_SERVER['SERVER_ADDR'];
}
$url = 'http://' . $ip . $base_path . 'imageinfo_cache_generate.php';
$headers = array(
'Host' => $_SERVER['HTTP_HOST'],
'Content-Type' => 'application/x-www-form-urlencoded',
'Connection' => 'close',
);
// Generate imagecache presets async.
$socket_timeout = ini_set('default_socket_timeout', variable_get('imageinfo_cache_socket_timeout', IMAGEINFO_CACHE_SOCKET_TIMEOUT));
$results = drupal_http_request($url, $headers, 'POST', $query_string);
ini_set('default_socket_timeout', $socket_timeout);
// Check response.
$key_back = trim($results->data);
// If async failed; block here and generate images and caches.
if ($key_back != $key) {
watchdog('imageinfo_cache', 'Asynchronous imageinfo cache primer failed. Using Synchronous mode.' . $key_back . ' ' . $key);
// watchdog('debug', str_replace(' ', ' ', nl2br(htmlentities(print_r($results, TRUE)))));
imageinfo_cache_primer($query);
}
}
}