New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ticket/15276] Use storage in avatars #4894
Changes from 33 commits
4ed0de9
0417482
ef00bb4
41af01b
929e32e
ee094dd
5a25a6b
89f4e12
286b1bb
4c5114c
946f034
9e018e7
006990f
8a47fd4
8d7336e
400e663
090ed9b
0ff80fe
2afada5
3c60333
09856ae
c3e9aa1
87229e1
28060a8
da3c9b3
fe20aa0
354dda5
bb88666
b255209
5ff182c
9fcf30d
ed28219
a8ba4a9
443c503
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,7 @@ | |
$user->setup(); | ||
|
||
$echos = 0; | ||
|
||
if (!isset($config['avatar_salt'])) | ||
{ | ||
$cache->purge(); | ||
|
@@ -30,6 +30,11 @@ | |
die('database not up to date'); | ||
} | ||
|
||
if (!isset($config['storage\\avatar\\config\\path']) || $config['storage\\avatar\\config\\path'] !== 'phpbb\\storage\\provider\\local') | ||
{ | ||
die('use local provider'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this message should be improved as for someone not knowing the exact purpose of this might not be able to understand why it's causing a die(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think i can rewrite this to use storage, but i dont know under which circunstances this script is used, so i just prevent to use it after changing storage There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I think we can leave it like that for now. We should start working on the dev doc soon and include a link here |
||
} | ||
|
||
// let's start with the users using a group_avatar. | ||
$sql = 'SELECT group_id, group_avatar | ||
FROM ' . GROUPS_TABLE . ' | ||
|
@@ -46,16 +51,16 @@ | |
{ | ||
$new_avatar_name = adjust_avatar($row['group_avatar'], 'g' . $row['group_id']); | ||
$group_avatars[] = $new_avatar_name; | ||
|
||
// failure is probably due to the avatar name already being adjusted | ||
if ($new_avatar_name !== false) | ||
{ | ||
$sql = 'UPDATE ' . USERS_TABLE . " | ||
SET user_avatar = '" . $db->sql_escape($new_avatar_name) . "' | ||
WHERE user_avatar = '" . $db->sql_escape($row['group_avatar']) . "' | ||
WHERE user_avatar = '" . $db->sql_escape($row['group_avatar']) . "' | ||
AND user_avatar_type = " . AVATAR_UPLOAD; | ||
$db->sql_query($sql); | ||
|
||
$sql = 'UPDATE ' . GROUPS_TABLE . " | ||
SET group_avatar = '" . $db->sql_escape($new_avatar_name) . "' | ||
WHERE group_id = {$row['group_id']}"; | ||
|
@@ -80,8 +85,8 @@ | |
$db->sql_freeresult($result); | ||
|
||
$sql = 'SELECT user_id, username, user_avatar, user_avatar_type | ||
FROM ' . USERS_TABLE . ' | ||
WHERE user_avatar_type = ' . AVATAR_UPLOAD . ' | ||
FROM ' . USERS_TABLE . ' | ||
WHERE user_avatar_type = ' . AVATAR_UPLOAD . ' | ||
AND ' . $db->sql_in_set('user_avatar', $group_avatars, true, true); | ||
$result = $db->sql_query($sql); | ||
|
||
|
@@ -108,7 +113,7 @@ | |
$db->sql_query($sql); | ||
echo '<br /> Failed updating user ' . $row['user_id'] . "\n"; | ||
} | ||
|
||
if ($echos > 200) | ||
{ | ||
echo '<br />' . "\n"; | ||
|
@@ -131,8 +136,8 @@ | |
function adjust_avatar($old_name, $midfix) | ||
{ | ||
global $config, $phpbb_root_path; | ||
$avatar_path = $phpbb_root_path . $config['avatar_path']; | ||
|
||
$avatar_path = $phpbb_root_path . $config['storage\\avatar\\config\\path']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know, there never was a rule about not having (back-)slashes in config names, but without underscores this somehow looks weird to me and breaking the status quo of having configuration names. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually I asked him to do that on purpose. For me as there is a status quo of not using \ we can reserved it for internal usage like that and have a guarantee of 0 conflict ever. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Internal usage means that you're not having any intention to make the path customizable? Then why not mark it as a parameter in a yaml file? Otherwise I don't understand the term "internal usage" or have a different opinion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the path is customizable from a core page in the acp There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As personally don't care for this. It's a nice way to prevent conflicts for sure. As long as it doesn't mess up with some dbal There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, okay.. It just looks weird to me but other than that it doesn't do anything harmful, I guess. |
||
$extension = strtolower(substr(strrchr($old_name, '.'), 1)); | ||
$new_name = $config['avatar_salt'] . '_' . $midfix . '.' . $extension; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -502,26 +502,8 @@ function main($id, $mode) | |
|
||
$upload_dir_size = get_formatted_filesize($config['upload_dir_size']); | ||
|
||
$avatar_dir_size = 0; | ||
|
||
if ($avatar_dir = @opendir($phpbb_root_path . $config['avatar_path'])) | ||
{ | ||
while (($file = readdir($avatar_dir)) !== false) | ||
{ | ||
if ($file[0] != '.' && $file != 'CVS' && strpos($file, 'index.') === false) | ||
{ | ||
$avatar_dir_size += filesize($phpbb_root_path . $config['avatar_path'] . '/' . $file); | ||
} | ||
} | ||
closedir($avatar_dir); | ||
|
||
$avatar_dir_size = get_formatted_filesize($avatar_dir_size); | ||
} | ||
else | ||
{ | ||
// Couldn't open Avatar dir. | ||
$avatar_dir_size = $user->lang['NOT_AVAILABLE']; | ||
} | ||
// Couldn't open Avatar dir. | ||
$avatar_dir_size = $user->lang['NOT_AVAILABLE']; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need here another way to get the avatars total filesize There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is just not possible with most cloud providers. We can check if the local adapter is in use, but otherwise I think you may need to use your cloud provider web interface (and even if it is possible we can't display the avatars usage but only the global one, if you use the same S3 bucket for many things per example). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually it may be possible for some providers and I think it can still be useful (even if it displays the global usage). Example: (all numbers are in bytes) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but i cannot get the usage easily. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. well, it depends what we want to do. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont know, i still think that is better to do a class for each storage with methods to get the files or somehing like that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry but I don't understand what you have in mind. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, im going to do a concept, but i have to finish other things first There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please create a dedicated ticket for this issue |
||
|
||
if ($posts_per_day > $total_posts) | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2164,7 +2164,9 @@ function phpbb_style_is_active($style_id) | |
*/ | ||
function avatar_delete($mode, $row, $clean_db = false) | ||
{ | ||
global $phpbb_root_path, $config; | ||
global $config, $phpbb_container; | ||
|
||
$storage = $phpbb_container->get('storage.avatar'); | ||
|
||
// Check if the users avatar is actually *not* a group avatar | ||
if ($mode == 'user') | ||
|
@@ -2181,11 +2183,16 @@ function avatar_delete($mode, $row, $clean_db = false) | |
} | ||
$filename = get_avatar_filename($row[$mode . '_avatar']); | ||
|
||
if (file_exists($phpbb_root_path . $config['avatar_path'] . '/' . $filename)) | ||
try | ||
{ | ||
@unlink($phpbb_root_path . $config['avatar_path'] . '/' . $filename); | ||
$storage->delete($filename); | ||
|
||
return true; | ||
} | ||
catch (\phpbb\storage\exception\exception $e) | ||
{ | ||
// Fail is covered by return statement below | ||
} | ||
|
||
return false; | ||
} | ||
|
@@ -2505,22 +2512,29 @@ function group_create(&$group_id, $type, $name, $desc, $group_attributes, $allow | |
*/ | ||
function group_correct_avatar($group_id, $old_entry) | ||
{ | ||
global $config, $db, $phpbb_root_path; | ||
global $config, $db, $phpbb_container; | ||
|
||
$storage = $phpbb_container->get('storage.avatar'); | ||
|
||
$group_id = (int) $group_id; | ||
$ext = substr(strrchr($old_entry, '.'), 1); | ||
$old_filename = get_avatar_filename($old_entry); | ||
$new_filename = $config['avatar_salt'] . "_g$group_id.$ext"; | ||
$new_entry = 'g' . $group_id . '_' . substr(time(), -5) . ".$ext"; | ||
|
||
$avatar_path = $phpbb_root_path . $config['avatar_path']; | ||
if (@rename($avatar_path . '/'. $old_filename, $avatar_path . '/' . $new_filename)) | ||
try | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This modification is not exactly the same as what the code did before which is fine for now, but it may cause problems if this needs to be extended in the future. (meaning error handling could get difficult if there are multiple statements that could throw exceptions). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uhm, i think it is. |
||
{ | ||
$this->storage->rename($old_filename, $new_filename); | ||
|
||
$sql = 'UPDATE ' . GROUPS_TABLE . ' | ||
SET group_avatar = \'' . $db->sql_escape($new_entry) . "' | ||
WHERE group_id = $group_id"; | ||
$db->sql_query($sql); | ||
} | ||
catch (\phpbb\storage\exception\exception $e) | ||
{ | ||
// If rename fail, dont execute the query | ||
} | ||
} | ||
|
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should display some translateable error message here i think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I dont know what is the purpose of this folder, but none of the strings in this file are translatable