pixelfed/app/Jobs/AvatarPipeline/AvatarOptimize.php

105 lines
3 KiB
PHP
Raw Normal View History

2018-08-10 09:10:05 +00:00
<?php
namespace App\Jobs\AvatarPipeline;
2018-08-28 03:07:36 +00:00
use App\Avatar;
use App\Profile;
2024-03-12 07:03:33 +00:00
use Cache;
2018-08-28 03:07:36 +00:00
use Carbon\Carbon;
2018-08-10 09:10:05 +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;
2019-04-06 22:20:00 +00:00
use Illuminate\Support\Str;
2018-08-28 03:07:36 +00:00
use Image as Intervention;
2022-12-02 03:31:20 +00:00
use Storage;
2018-08-10 09:10:05 +00:00
class AvatarOptimize implements ShouldQueue
{
2024-03-12 07:03:33 +00:00
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $profile;
2018-08-10 09:10:05 +00:00
2024-03-12 07:03:33 +00:00
protected $current;
2018-08-10 09:10:05 +00:00
2024-03-12 07:03:33 +00:00
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
2019-01-12 22:24:20 +00:00
2024-03-12 07:03:33 +00:00
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Profile $profile, $current)
{
$this->profile = $profile;
$this->current = $current;
}
2018-08-10 09:10:05 +00:00
2024-03-12 07:03:33 +00:00
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$avatar = $this->profile->avatar;
$file = storage_path("app/$avatar->media_path");
2018-08-10 09:10:05 +00:00
2024-03-12 07:03:33 +00:00
try {
$img = Intervention::make($file)->orientate();
$img->fit(200, 200, function ($constraint) {
$constraint->upsize();
});
$quality = config_cache('pixelfed.image_quality');
$img->save($file, $quality);
2018-08-10 09:10:05 +00:00
2024-03-12 07:03:33 +00:00
$avatar = Avatar::whereProfileId($this->profile->id)->firstOrFail();
$avatar->change_count = ++$avatar->change_count;
$avatar->last_processed_at = Carbon::now();
$avatar->save();
Cache::forget('avatar:'.$avatar->profile_id);
$this->deleteOldAvatar($avatar->media_path, $this->current);
2022-12-02 03:31:20 +00:00
2024-03-12 07:03:33 +00:00
if ((bool) config_cache('pixelfed.cloud_storage') && (bool) config_cache('instance.avatar.local_to_cloud')) {
$this->uploadToCloud($avatar);
} else {
$avatar->cdn_url = null;
$avatar->save();
}
} catch (Exception $e) {
}
}
2018-08-10 09:10:05 +00:00
2024-03-12 07:03:33 +00:00
protected function deleteOldAvatar($new, $current)
{
if (storage_path('app/'.$new) == $current ||
Str::endsWith($current, 'avatars/default.png') ||
Str::endsWith($current, 'avatars/default.jpg')) {
return;
}
if (is_file($current)) {
@unlink($current);
}
}
2022-12-02 03:31:20 +00:00
2024-03-12 07:03:33 +00:00
protected function uploadToCloud($avatar)
{
$base = 'cache/avatars/'.$avatar->profile_id;
$disk = Storage::disk(config('filesystems.cloud'));
$disk->deleteDirectory($base);
$path = $base.'/'.'avatar_'.strtolower(Str::random(random_int(3, 6))).$avatar->change_count.'.'.pathinfo($avatar->media_path, PATHINFO_EXTENSION);
$url = $disk->put($path, Storage::get($avatar->media_path));
$avatar->media_path = $path;
$avatar->cdn_url = $disk->url($path);
$avatar->save();
Storage::delete($avatar->media_path);
Cache::forget('avatar:'.$avatar->profile_id);
}
2018-08-10 09:10:05 +00:00
}