When site has enabled page cache and anonymous user tries to upload a file, he cannot see upload status progress. He see only message "Starting upload...". Bug was reported in June 2012, has a working patch and I have a question:
How to fix broken file.module function file_ajax_progress
without hacking the core and patching after every update? Should I do some function override by custom module?
This is all working code together:
/**
* Menu callback for upload progress.
*
* @param $key
* The unique key for this upload process.
*/
function file_ajax_progress($key) {
$progress = array(
'message' => t('Starting upload...'),
'percentage' => -1,
);
$implementation = file_progress_implementation();
if ($implementation == 'uploadprogress') {
$status = uploadprogress_get_info($key);
if (isset($status['bytes_uploaded']) && !empty($status['bytes_total'])) {
$progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['bytes_uploaded']), '@total' => format_size($status['bytes_total'])));
$progress['percentage'] = round(100 * $status['bytes_uploaded'] / $status['bytes_total']);
}
}
elseif ($implementation == 'apc') {
$status = apc_fetch('upload_' . $key);
if (isset($status['current']) && !empty($status['total'])) {
$progress['message'] = t('Uploading... (@current of @total)', array('@current' => format_size($status['current']), '@total' => format_size($status['total'])));
$progress['percentage'] = round(100 * $status['current'] / $status['total']);
}
}
// Mark this page as being uncacheable.
drupal_page_is_cacheable(FALSE);
drupal_json_output($progress);
}