Skip to content

Commit

Permalink
对头像CDN进行优化,以能够及时刷新
Browse files Browse the repository at this point in the history
  • Loading branch information
skywalker512 committed Jul 13, 2018
1 parent 2a58fd3 commit b5e6d0b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion addons/plugins/Gravatar/plugin.php
Expand Up @@ -35,7 +35,7 @@ function avatar($member = array(), $className = "")
// Construct the avatar path from the provided information.
if (!empty($member["memberId"]) and !empty($member["avatarFormat"])) {
$cdn = C("esoTalk.cdnURL");
$file = "uploads/avatars/{$member["memberId"]}.{$member["avatarFormat"]}";
$file = "uploads/avatars/{$member["memberId"]}_{$member["avatarTime"]}.{$member["avatarFormat"]}";
$url = getWebPath($file);
return "<img src='$cdn$url' alt='{$member["memberId"]}' class='avatar $className'/>";
} else {
Expand Down
2 changes: 1 addition & 1 deletion core/controllers/ETMemberController.class.php
Expand Up @@ -373,7 +373,7 @@ public function action_removeAvatar($memberId = "")
}

// Remove the avatar file.
@unlink(PATH_UPLOADS."/avatars/".$member["memberId"].".".$member["avatarFormat"]);
@unlink(PATH_UPLOADS."/avatars/".$member["memberId"]."_".$member["avatarTime"].".".$member["avatarFormat"]);

// Clear the member's avatar format field.
ET::memberModel()->updateById($member["memberId"], array("avatarFormat" => null));
Expand Down
11 changes: 9 additions & 2 deletions core/controllers/ETSettingsController.class.php
Expand Up @@ -173,7 +173,7 @@ public function action_general()
elseif ($form->validPostBack("removeAvatar")) {

// Delete the avatar file and set the member's avatarFormat to null.
@unlink(PATH_UPLOADS."/avatars/".$member["memberId"].".".$member["avatarFormat"]);
@unlink(PATH_UPLOADS."/avatars/".$member["memberId"]."_".$member["avatarTime"].".".$member["avatarFormat"]);
ET::memberModel()->updateById($member["memberId"], array("avatarFormat" => null));

$this->message(T("message.changesSaved"), "success autoDismiss");
Expand Down Expand Up @@ -367,16 +367,23 @@ public function saveAvatar($form, $key, &$preferences)
$uploader = ET::uploader();

try {
// remove previous img
$member = $this->profile("general");
@unlink(PATH_UPLOADS."/avatars/".$member["memberId"]."_".$member["avatarTime"].".".$member["avatarFormat"]);

// Validate and get the uploaded file from this field.
$file = $uploader->getUploadedFile($key);
$time = substr(md5(time()), 0, 4);

// Save it as an image, cropping it to the configured avatar size.
$avatar = $uploader->saveAsImage($file, PATH_UPLOADS."/avatars/".ET::$session->userId, C("esoTalk.avatars.width"), C("esoTalk.avatars.height"), "crop");
$avatar = $uploader->saveAsImage($file, PATH_UPLOADS."/avatars/".ET::$session->userId."_".$time, C("esoTalk.avatars.width"), C("esoTalk.avatars.height"), "crop");

// Update the member's avatarFormat field to the avatar file's extension.
ET::memberModel()->updateById(ET::$session->userId, array("avatarFormat" => pathinfo($avatar, PATHINFO_EXTENSION)));

// Update the member's avatarFormat field to the avatar file's extension.
ET::memberModel()->updateById(ET::$session->userId, array("avatarTime" => $time));

} catch (Exception $e) {

// If something went wrong up there, add the error message to the form.
Expand Down
2 changes: 1 addition & 1 deletion core/lib/functions.render.php
Expand Up @@ -174,7 +174,7 @@ function avatar($member = array(), $className = "")
// Construct the avatar path from the provided information.
if (!empty($member["memberId"]) and !empty($member["avatarFormat"])) {
$cdn = C("esoTalk.cdnURL");
$file = "uploads/avatars/{$member["memberId"]}.{$member["avatarFormat"]}";
$file = "uploads/avatars/{$member["memberId"]}_{$member["avatarTime"]}.{$member["avatarFormat"]}";
$url = getWebPath($file);
return "<img src='$cdn$url' alt='{$member["memberId"]}' class='avatar $className'/>";
}
Expand Down
1 change: 1 addition & 0 deletions core/models/ETUpgradeModel.class.php
Expand Up @@ -151,6 +151,7 @@ protected function structure($drop = false)
->column("lastActionTime", "int(11) unsigned")
->column("lastActionDetail", "tinyblob")
->column("avatarFormat", "enum('jpg','png','gif')")
->column("avatarTime", "char(4)")
->column("preferences", "mediumblob")
->column("countPosts", "int(11) unsigned", 0)
->column("countConversations", "int(11) unsigned", 0)
Expand Down

0 comments on commit b5e6d0b

Please sign in to comment.