lasthourcloud/app/Models/S3Storage.php

73 lines
2.1 KiB
PHP
Raw Normal View History

2023-08-07 13:31:42 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
2023-10-10 11:10:43 +00:00
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Storage;
2023-08-07 13:31:42 +00:00
class S3Storage extends BaseModel
{
use HasFactory;
2023-08-07 13:31:42 +00:00
protected $guarded = [];
2023-08-07 16:46:40 +00:00
protected $casts = [
2023-10-10 11:10:43 +00:00
'is_usable' => 'boolean',
2023-08-07 16:46:40 +00:00
'key' => 'encrypted',
'secret' => 'encrypted',
];
2023-08-07 13:31:42 +00:00
static public function ownedByCurrentTeam(array $select = ['*'])
{
$selectArray = collect($select)->concat(['id']);
2023-08-22 15:44:49 +00:00
return S3Storage::whereTeamId(currentTeam()->id)->select($selectArray->all())->orderBy('name');
2023-08-07 13:31:42 +00:00
}
2023-10-10 11:10:43 +00:00
public function isUsable()
{
return $this->is_usable;
}
2023-10-10 11:10:43 +00:00
public function team()
{
return $this->belongsTo(Team::class);
}
public function awsUrl()
{
2023-08-07 13:31:42 +00:00
return "{$this->endpoint}/{$this->bucket}";
}
public function testConnection(bool $shouldSave = false)
{
2023-10-10 11:10:43 +00:00
try {
set_s3_target($this);
Storage::disk('custom-s3')->files();
$this->unusable_email_sent = false;
$this->is_usable = true;
} catch (\Throwable $e) {
$this->is_usable = false;
if ($this->unusable_email_sent === false && is_transactional_emails_active()) {
2023-10-10 11:10:43 +00:00
$mail = new MailMessage();
2024-02-21 23:21:08 +00:00
$mail->subject('Last Hour: S3 Storage Connection Error');
2024-01-07 15:23:41 +00:00
$mail->view('emails.s3-connection-error', ['name' => $this->name, 'reason' => $e->getMessage(), 'url' => route('team.storage.show', ['storage_uuid' => $this->uuid])]);
2023-10-10 11:10:43 +00:00
$users = collect([]);
$members = $this->team->members()->get();
foreach ($members as $user) {
if ($user->isAdmin()) {
$users->push($user);
}
}
foreach ($users as $user) {
send_user_an_email($mail, $user->email);
}
$this->unusable_email_sent = true;
}
throw $e;
} finally {
if ($shouldSave) {
$this->save();
}
2023-10-10 11:10:43 +00:00
}
2023-08-07 13:31:42 +00:00
}
}