The issue I am having is image cache is not updated when an image with the same name is uploaded. The module includes code to clear caches, but it is commented out in lines 107-128 of upload_replace.module.

Is there a reason why this code was commented out and if so is there a way to still provide that functionality?

Lines 107-128:
//Have to clear the cache because the revision data is cached somewhere
/*
* Find the nids where this file is used
$query = "SELECT DISTINCT nid FROM {files} WHERE fid=%d";
$result = db_query($query, $new_file->fid);
while($data = db_fetch_object($result)) {
cache_clear_all("content:$data->nid");
}
*/
//This is inefficent, but how can we determine what nodes use this file?
// cache_clear_all('*', 'cache_content', TRUE);
}

/**
* HOOK_file_delete, update the filepath in the file object before deleting as we may have altered it above
* @param object $new_file
*/
/*
function upload_replace_file_delete(&$file) {
$file->filepath = db_result(db_query("SELECT filepath FROM {files} WHERE fid = %d", $file->fid));
}
*/

Comments

capellic’s picture

Issue summary: View changes

Looks like the code was commented out during the port from Drupal 6 to Drupal 7 query code and call to cache_clear_all aren't correct for Drupal 7. It simply looks like the module maintainer never got around to it. I did some digging and I've come up with some code that is working on my end and even works fine with Acquia Purge and Cache Expiration on the Acquia hosting platform.

Right below the comment, "//Have to clear the cache because the revision data is cached somewhere" (line 107), add this:

$query = "SELECT DISTINCT uri FROM {file_managed} WHERE fid=:fid";
$uri = db_query($query, array(':fid' => $new_file->fid))->fetchField();
image_path_flush($uri);

Leave the code that is commented out in the module as is.

ladybug_3777’s picture

Adding an actual .patch file for this issue so the change above can be applied and rolled back out easier, as well as enabling it to be include in .make files.

elijah lynn’s picture

elijah lynn’s picture

Status: Active » Needs review
StatusFileSize
new907 bytes
new601 bytes

It appears this also needs to flush the cache of the file that was being replaced, the new _X.ext.

I have attached a patch that adds this although, maybe it could be more robust?

This is dependent on https://www.drupal.org/node/1115484#comment-11308321 being applied first.

elijah lynn’s picture

My patch is incorrect, it should be $old_destination, not $original_destination. I don't have time to re-roll right now but it is here if anyone needs it https://www.drupal.org/node/1115484#comment-11325069. If someone could re-roll that would be great!

elijah lynn’s picture

StatusFileSize
new1017 bytes

I just committed #57 from #1115484: Parameter 1 to upload_replace_file_update() expected to be a reference, value given in module_invoke_all() which includes #2 here. Here is latest patch re-rolled against HEAD with the improvements to flush out the image style of the replaced image.

  • Elijah Lynn committed aa02580 on 7.x-1.x
    Issue #2037295 by Elijah Lynn, ladybug_3777, capellic, jarrodch: Image...
elijah lynn’s picture

Status: Needs review » Fixed

Committed #7.

Status: Fixed » Needs work

The last submitted patch, 7: upload_replace-2037295-7.patch, failed testing.

The last submitted patch, 7: upload_replace-2037295-7.patch, failed testing.

elijah lynn’s picture

Lol, I turned on testing but the error that came back was "no valid tests specified". Looks like I will turn off testing for now.

elijah lynn’s picture

Status: Needs work » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.