2018-06-01 02:36:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2018-08-10 09:10:05 +00:00
|
|
|
use App\Avatar;
|
|
|
|
use App\Jobs\AvatarPipeline\AvatarOptimize;
|
2018-08-28 03:07:36 +00:00
|
|
|
use Auth;
|
|
|
|
use Cache;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Storage;
|
2018-06-01 02:36:58 +00:00
|
|
|
|
|
|
|
class AvatarController extends Controller
|
|
|
|
{
|
2018-08-10 09:10:05 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
return $this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
|
|
|
$this->validate($request, [
|
2022-02-06 02:32:10 +00:00
|
|
|
'avatar' => 'required|mimetypes:jpeg,png|max:'.config('pixelfed.max_avatar_size'),
|
2018-08-10 09:10:05 +00:00
|
|
|
]);
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-08-10 09:10:05 +00:00
|
|
|
try {
|
2018-08-28 03:07:36 +00:00
|
|
|
$user = Auth::user();
|
|
|
|
$profile = $user->profile;
|
|
|
|
$file = $request->file('avatar');
|
|
|
|
$path = $this->getPath($user, $file);
|
|
|
|
$dir = $path['root'];
|
|
|
|
$name = $path['name'];
|
|
|
|
$public = $path['storage'];
|
|
|
|
$loc = $request->file('avatar')->storeAs($public, $name);
|
2018-08-10 09:10:05 +00:00
|
|
|
|
2019-03-01 18:54:05 +00:00
|
|
|
$avatar = Avatar::firstOrNew(['profile_id' => $profile->id]);
|
2019-03-01 21:02:05 +00:00
|
|
|
$currentAvatar = $avatar->recentlyCreated ? null : storage_path('app/'.$profile->avatar->media_path);
|
2018-08-28 03:07:36 +00:00
|
|
|
$avatar->media_path = "$public/$name";
|
|
|
|
$avatar->change_count = ++$avatar->change_count;
|
|
|
|
$avatar->last_processed_at = null;
|
|
|
|
$avatar->save();
|
2018-08-10 09:10:05 +00:00
|
|
|
|
2018-08-28 03:07:36 +00:00
|
|
|
Cache::forget("avatar:{$profile->id}");
|
2019-04-30 02:37:27 +00:00
|
|
|
Cache::forget('user:account:id:'.$user->id);
|
2018-08-28 03:07:36 +00:00
|
|
|
AvatarOptimize::dispatch($user->profile, $currentAvatar);
|
2018-08-10 09:10:05 +00:00
|
|
|
} catch (Exception $e) {
|
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-08-10 09:10:05 +00:00
|
|
|
return redirect()->back()->with('status', 'Avatar updated successfully. It may take a few minutes to update across the site.');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getPath($user, $file)
|
|
|
|
{
|
|
|
|
$basePath = storage_path('app/public/avatars');
|
|
|
|
$this->checkDir($basePath);
|
|
|
|
|
|
|
|
$id = $user->profile->id;
|
|
|
|
$path = $this->buildPath($id);
|
|
|
|
$dir = storage_path('app/'.$path);
|
|
|
|
$this->checkDir($dir);
|
2018-11-27 06:53:26 +00:00
|
|
|
$name = str_random(20).'_avatar.'.$file->guessExtension();
|
2018-08-28 03:07:36 +00:00
|
|
|
$res = ['root' => 'storage/app/'.$path, 'name' => $name, 'storage' => $path];
|
2018-08-10 09:10:05 +00:00
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function checkDir($path)
|
|
|
|
{
|
2018-08-28 03:07:36 +00:00
|
|
|
if (!is_dir($path)) {
|
2018-08-10 09:10:05 +00:00
|
|
|
mkdir($path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function buildPath($id)
|
|
|
|
{
|
2019-07-20 05:07:33 +00:00
|
|
|
$padded = str_pad($id, 19, 0, STR_PAD_LEFT);
|
2018-08-10 09:10:05 +00:00
|
|
|
$parts = str_split($padded, 3);
|
2018-08-28 03:07:36 +00:00
|
|
|
foreach ($parts as $k => $part) {
|
|
|
|
if ($k == 0) {
|
|
|
|
$prefix = storage_path('app/public/avatars/'.$parts[0]);
|
|
|
|
$this->checkDir($prefix);
|
|
|
|
}
|
|
|
|
if ($k == 1) {
|
|
|
|
$prefix = storage_path('app/public/avatars/'.$parts[0].'/'.$parts[1]);
|
|
|
|
$this->checkDir($prefix);
|
|
|
|
}
|
|
|
|
if ($k == 2) {
|
|
|
|
$prefix = storage_path('app/public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2]);
|
|
|
|
$this->checkDir($prefix);
|
|
|
|
}
|
|
|
|
if ($k == 3) {
|
|
|
|
$avatarpath = 'public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3];
|
|
|
|
$prefix = storage_path('app/'.$avatarpath);
|
2019-07-20 05:07:33 +00:00
|
|
|
$this->checkDir($prefix);
|
|
|
|
}
|
|
|
|
if ($k == 4) {
|
|
|
|
$avatarpath = 'public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3].'/'.$parts[4];
|
|
|
|
$prefix = storage_path('app/'.$avatarpath);
|
|
|
|
$this->checkDir($prefix);
|
|
|
|
}
|
|
|
|
if ($k == 5) {
|
|
|
|
$avatarpath = 'public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3].'/'.$parts[4].'/'.$parts[5];
|
|
|
|
$prefix = storage_path('app/'.$avatarpath);
|
|
|
|
$this->checkDir($prefix);
|
|
|
|
}
|
|
|
|
if ($k == 6) {
|
|
|
|
$avatarpath = 'public/avatars/'.$parts[0].'/'.$parts[1].'/'.$parts[2].'/'.$parts[3].'/'.$parts[4].'/'.$parts[5].'/'.$parts[6];
|
|
|
|
$prefix = storage_path('app/'.$avatarpath);
|
2018-08-28 03:07:36 +00:00
|
|
|
$this->checkDir($prefix);
|
|
|
|
}
|
2018-08-10 09:10:05 +00:00
|
|
|
}
|
2018-08-28 03:07:36 +00:00
|
|
|
|
2018-08-10 09:10:05 +00:00
|
|
|
return $avatarpath;
|
|
|
|
}
|
2019-04-06 22:45:53 +00:00
|
|
|
|
|
|
|
public function deleteAvatar(Request $request)
|
|
|
|
{
|
|
|
|
$user = Auth::user();
|
|
|
|
$profile = $user->profile;
|
|
|
|
|
|
|
|
$avatar = $profile->avatar;
|
|
|
|
|
2020-12-15 06:54:22 +00:00
|
|
|
if( $avatar->media_path == 'public/avatars/default.png' ||
|
2021-01-25 01:22:13 +00:00
|
|
|
$avatar->media_path == 'public/avatars/default.jpg'
|
2020-12-15 06:54:22 +00:00
|
|
|
) {
|
2019-04-06 22:45:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(is_file(storage_path('app/' . $avatar->media_path))) {
|
|
|
|
@unlink(storage_path('app/' . $avatar->media_path));
|
|
|
|
}
|
|
|
|
|
2020-12-15 06:54:22 +00:00
|
|
|
$avatar->media_path = 'public/avatars/default.jpg';
|
2019-04-06 22:45:53 +00:00
|
|
|
$avatar->change_count = $avatar->change_count + 1;
|
|
|
|
$avatar->save();
|
|
|
|
|
|
|
|
Cache::forget('avatar:' . $avatar->profile_id);
|
|
|
|
|
|
|
|
return response()->json(200);
|
|
|
|
}
|
2018-06-01 02:36:58 +00:00
|
|
|
}
|