1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-23 16:24:29 +00:00
pixelfed/app/Jobs/AvatarPipeline/CreateAvatar.php

108 lines
3.3 KiB
PHP
Raw Normal View History

2018-05-22 23:57:52 +00:00
<?php
namespace App\Jobs\AvatarPipeline;
2018-08-28 03:07:36 +00:00
use App\Avatar;
use App\Profile;
use App\Util\Identicon\Preprocessor\HashPreprocessor;
use Bitverse\Identicon\Color\Color;
use Bitverse\Identicon\Generator\RingsGenerator;
use Bitverse\Identicon\Identicon;
2018-05-22 23:57:52 +00:00
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
2018-08-28 03:07:36 +00:00
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
2018-05-22 23:57:52 +00:00
class CreateAvatar implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2018-05-31 22:04:15 +00:00
protected $profile;
2018-08-28 03:07:36 +00:00
2018-05-22 23:57:52 +00:00
/**
* Create a new job instance.
*
* @return void
*/
2018-05-31 22:04:15 +00:00
public function __construct(Profile $profile)
2018-05-22 23:57:52 +00:00
{
2018-05-31 22:04:15 +00:00
$this->profile = $profile;
2018-05-22 23:57:52 +00:00
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
2018-05-31 22:04:15 +00:00
$profile = $this->profile;
$username = $profile->username;
2018-05-22 23:57:52 +00:00
$generator = new RingsGenerator();
$generator->setBackgroundColor(Color::parseHex('#FFFFFF'));
2018-05-31 22:04:15 +00:00
$identicon = new Identicon(new HashPreprocessor('sha256'), $generator);
2018-05-22 23:57:52 +00:00
2018-08-28 03:07:36 +00:00
$hash = $username.str_random(12);
2018-05-22 23:57:52 +00:00
$icon = $identicon->getIcon($hash);
try {
2018-08-28 03:07:36 +00:00
$baseDir = storage_path('app/public/avatars');
if (!is_dir($baseDir)) {
mkdir($baseDir);
}
2018-06-01 04:07:29 +00:00
2018-08-28 03:07:36 +00:00
$prefix = $profile->id;
$padded = str_pad($prefix, 12, 0, STR_PAD_LEFT);
$parts = str_split($padded, 3);
foreach ($parts as $k => $part) {
if ($k == 0) {
$prefix = storage_path('app/public/avatars/'.$parts[0]);
if (!is_dir($prefix)) {
mkdir($prefix);
}
2018-05-22 23:57:52 +00:00
}
2018-08-28 03:07:36 +00:00
if ($k == 1) {
$prefix = storage_path('app/public/avatars/'.$parts[0].'/'.$parts[1]);
if (!is_dir($prefix)) {
mkdir($prefix);
}
2018-05-22 23:57:52 +00:00
}
2018-08-28 03:07:36 +00:00
if ($k == 2) {
$prefix = storage_path('app/public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2]);
if (!is_dir($prefix)) {
mkdir($prefix);
}
2018-05-22 23:57:52 +00:00
}
2018-08-28 03:07:36 +00:00
if ($k == 3) {
$avatarpath = 'public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3];
$prefix = storage_path('app/'.$avatarpath);
if (!is_dir($prefix)) {
mkdir($prefix);
}
2018-05-22 23:57:52 +00:00
}
}
2018-08-28 03:07:36 +00:00
$dir = storage_path('app/'.$avatarpath);
//$dir = storage_path('app/public/avatars/'.$prefix);
if (!is_dir($dir)) {
mkdir($dir);
}
//$path = 'public/avatars/' . $prefix . '/avatar.svg';
$path = $avatarpath.'/avatar.svg';
$basePath = storage_path('app/'.$path);
file_put_contents($basePath, $icon);
2018-05-22 23:57:52 +00:00
} catch (Exception $e) {
}
2018-08-28 03:07:36 +00:00
$avatar = new Avatar();
2018-05-31 22:04:15 +00:00
$avatar->profile_id = $profile->id;
2018-05-22 23:57:52 +00:00
$avatar->media_path = $path;
$avatar->thumb_path = $path;
$avatar->change_count = 0;
$avatar->last_processed_at = \Carbon\Carbon::now();
$avatar->save();
}
}