rev.19 Change for avatars

Add random names.
Save names to the database.
This commit is contained in:
Visman 2020-10-03 23:29:17 +07:00
parent 465f02b981
commit 29d9db3727
5 changed files with 67 additions and 19 deletions

View file

@ -884,6 +884,7 @@ class Install extends Admin
'email_normal' => ['VARCHAR(190)', false, ''],
'email_confirmed' => ['TINYINT(1)', false, 0],
'title' => ['VARCHAR(50)', false, ''],
'avatar' => ['VARCHAR(30)', false, ''],
'realname' => ['VARCHAR(40)', false, ''],
'url' => ['VARCHAR(100)', false, ''],
'jabber' => ['VARCHAR(80)', false, ''],

View file

@ -17,7 +17,7 @@ class Update extends Admin
{
const PHP_MIN = '7.3.0';
const LATEST_REV_WITH_DB_CHANGES = 16;
const LATEST_REV_WITH_DB_CHANGES = 19;
const LOCK_NAME = 'lock_update';
const LOCk_TTL = 1800;
@ -842,4 +842,45 @@ class Update extends Admin
return null;
}
/**
* rev.18 to rev.19
*/
protected function stageNumber18(array $args): ?int
{
$this->c->DB->addField('users', 'avatar', 'VARCHAR(30)', false, '', 'title');
$dir = $this->c->DIR_PUBLIC . $this->c->config->o_avatars_dir . '/';
$avatars = [];
if (
\is_dir($dir)
&& false !== ($dh = \opendir($dir))
) {
while (false !== ($entry = \readdir($dh))) {
if (
\preg_match('%^([1-9]\d*)\.(jpg|gif|png)$%D', $entry, $matches)
&& \is_file($dir . $entry)
) {
$avatars[$matches[2]][] = (int) $matches[1];
}
}
\closedir($dh);
}
$query = 'UPDATE ::users
SET avatar=CONCAT(id, \'.\', ?s:ext)
WHERE id IN (?ai:ids)';
foreach ($avatars as $ext => $ids) {
$vars = [
':ext' => $ext,
':ids' => $ids,
];
$this->c->DB->exec($query, $vars);
}
return null;
}
}

View file

@ -119,11 +119,17 @@ class Edit extends Profile
}
if ($v->upload_avatar instanceof Image) {
$v->upload_avatar
->rename(false)
->rewrite(true)
$name = $this->c->Secury->randomPass(8);
$path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$name}.(jpg|png|gif)";
$result = $v->upload_avatar
->rename(true)
->rewrite(false)
->resize((int) $this->c->config->o_avatars_width, (int) $this->c->config->o_avatars_height)
->toFile($this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$this->curUser->id}.(jpg|png|gif)");
->toFile($path);
if (true === $result) {
$this->curUser->avatar = $v->upload_avatar->name() . '.' . $v->upload_avatar->ext();
}
}
$this->curUser->replAttrs($data, true);

View file

@ -24,7 +24,7 @@ class Model extends DataModel
$this->zDepend = [
'group_id' => ['isUnverified', 'isGuest', 'isAdmin', 'isAdmMod', 'link', 'viewUsers', 'showPostCount', 'searchUsers'],
'id' => ['isGuest', 'link', 'avatar', 'online'],
'id' => ['isGuest', 'link', 'online'],
'last_visit' => ['currentVisit'],
'show_sig' => ['showSignature'],
'show_avatars' => ['showAvatar'],
@ -162,14 +162,13 @@ class Model extends DataModel
*/
protected function getavatar(): ?string
{
foreach ($this->avatarTypes as $type) {
$path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$this->id}.{$type}";
$file = $this->getAttr('avatar');
if (
\is_file($path)
&& \getimagesize($path)
) {
return $this->c->PUBLIC_URL . "{$this->c->config->o_avatars_dir}/{$this->id}.{$type}";
if (! empty($file)) {
$path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$file}";
if (\is_file($path)) {
return $this->c->PUBLIC_URL . "{$this->c->config->o_avatars_dir}/{$file}";
}
}
@ -181,13 +180,14 @@ class Model extends DataModel
*/
public function deleteAvatar(): void
{
foreach ($this->avatarTypes as $type) {
$path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$this->id}.{$type}";
$file = $this->getAttr('avatar');
$path = $this->c->DIR_PUBLIC . "{$this->c->config->o_avatars_dir}/{$file}";
if (\is_file($path)) {
@\unlink($path);
}
if (\is_file($path)) {
@\unlink($path);
}
$this->avatar = '';
}
/**

View file

@ -42,7 +42,7 @@ if (
}
$c->PUBLIC_URL = $c->BASE_URL . $forkPublicPrefix;
$c->FORK_REVISION = 18;
$c->FORK_REVISION = 19;
$c->START = $forkStart;
$c->DIR_APP = __DIR__;
$c->DIR_PUBLIC = $forkPublic;