Clean up remote avatar temporary files if we fail before saving them into avatars directory (OMB core, OStatus, WikiHowProfile, YammerImport)

This commit is contained in:
Brion Vibber 2010-10-13 11:04:41 -07:00
parent 2291d68e70
commit bca215563f
4 changed files with 65 additions and 45 deletions

View File

@ -328,13 +328,18 @@ class StatusNetOAuthDataStore extends OAuthDataStore
function add_avatar($profile, $url) function add_avatar($profile, $url)
{ {
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
copy($url, $temp_filename); try {
$imagefile = new ImageFile($profile->id, $temp_filename); copy($url, $temp_filename);
$filename = Avatar::filename($profile->id, $imagefile = new ImageFile($profile->id, $temp_filename);
image_type_to_extension($imagefile->type), $filename = Avatar::filename($profile->id,
null, image_type_to_extension($imagefile->type),
common_timestamp()); null,
rename($temp_filename, Avatar::path($filename)); common_timestamp());
rename($temp_filename, Avatar::path($filename));
} catch (Exception $e) {
unlink($temp_filename);
throw $e;
}
return $profile->setOriginal($filename); return $profile->setOriginal($filename);
} }

View File

@ -1053,22 +1053,27 @@ class Ostatus_profile extends Memcached_DataObject
// @fixme this should be better encapsulated // @fixme this should be better encapsulated
// ripped from oauthstore.php (for old OMB client) // ripped from oauthstore.php (for old OMB client)
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
if (!copy($url, $temp_filename)) { try {
throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url)); if (!copy($url, $temp_filename)) {
} throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url));
}
if ($this->isGroup()) { if ($this->isGroup()) {
$id = $this->group_id; $id = $this->group_id;
} else { } else {
$id = $this->profile_id; $id = $this->profile_id;
}
// @fixme should we be using different ids?
$imagefile = new ImageFile($id, $temp_filename);
$filename = Avatar::filename($id,
image_type_to_extension($imagefile->type),
null,
common_timestamp());
rename($temp_filename, Avatar::path($filename));
} catch (Exception $e) {
unlink($temp_filename);
throw $e;
} }
// @fixme should we be using different ids?
$imagefile = new ImageFile($id, $temp_filename);
$filename = Avatar::filename($id,
image_type_to_extension($imagefile->type),
null,
common_timestamp());
rename($temp_filename, Avatar::path($filename));
// @fixme hardcoded chmod is lame, but seems to be necessary to // @fixme hardcoded chmod is lame, but seems to be necessary to
// keep from accidentally saving images from command-line (queues) // keep from accidentally saving images from command-line (queues)
// that can't be read from web server, which causes hard-to-notice // that can't be read from web server, which causes hard-to-notice

View File

@ -174,20 +174,25 @@ class WikiHowProfilePlugin extends Plugin
// @fixme this should be better encapsulated // @fixme this should be better encapsulated
// ripped from OStatus via oauthstore.php (for old OMB client) // ripped from OStatus via oauthstore.php (for old OMB client)
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
if (!copy($url, $temp_filename)) { try {
throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url)); if (!copy($url, $temp_filename)) {
throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url));
}
$profile = $user->getProfile();
$id = $profile->id;
// @fixme should we be using different ids?
$imagefile = new ImageFile($id, $temp_filename);
$filename = Avatar::filename($id,
image_type_to_extension($imagefile->type),
null,
common_timestamp());
rename($temp_filename, Avatar::path($filename));
} catch (Exception $e) {
unlink($temp_filename);
throw $e;
} }
$profile = $user->getProfile();
$id = $profile->id;
// @fixme should we be using different ids?
$imagefile = new ImageFile($id, $temp_filename);
$filename = Avatar::filename($id,
image_type_to_extension($imagefile->type),
null,
common_timestamp());
rename($temp_filename, Avatar::path($filename));
$profile->setOriginal($filename); $profile->setOriginal($filename);
} }
} }

View File

@ -436,18 +436,23 @@ class YammerImporter
// @fixme this should be better encapsulated // @fixme this should be better encapsulated
// ripped from oauthstore.php (for old OMB client) // ripped from oauthstore.php (for old OMB client)
$temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar'); $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
if (!copy($url, $temp_filename)) { try {
throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url)); if (!copy($url, $temp_filename)) {
} throw new ServerException(sprintf(_m("Unable to fetch avatar from %s."), $url));
}
$id = $dest->id; $id = $dest->id;
// @fixme should we be using different ids? // @fixme should we be using different ids?
$imagefile = new ImageFile($id, $temp_filename); $imagefile = new ImageFile($id, $temp_filename);
$filename = Avatar::filename($id, $filename = Avatar::filename($id,
image_type_to_extension($imagefile->type), image_type_to_extension($imagefile->type),
null, null,
common_timestamp()); common_timestamp());
rename($temp_filename, Avatar::path($filename)); rename($temp_filename, Avatar::path($filename));
} catch (Exception $e) {
unlink($temp_filename);
throw $e;
}
// @fixme hardcoded chmod is lame, but seems to be necessary to // @fixme hardcoded chmod is lame, but seems to be necessary to
// keep from accidentally saving images from command-line (queues) // keep from accidentally saving images from command-line (queues)
// that can't be read from web server, which causes hard-to-notice // that can't be read from web server, which causes hard-to-notice