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-08 09:51:36 +00:00
|
|
|
|
2023-08-07 13:31:42 +00:00
|
|
|
protected $guarded = [];
|
2024-06-10 20:43:34 +00:00
|
|
|
|
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
|
|
|
|
2024-06-10 20:43:34 +00:00
|
|
|
public static function ownedByCurrentTeam(array $select = ['*'])
|
2023-08-07 13:31:42 +00:00
|
|
|
{
|
|
|
|
$selectArray = collect($select)->concat(['id']);
|
2024-06-10 20:43:34 +00:00
|
|
|
|
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
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-10-10 11:10:43 +00:00
|
|
|
public function isUsable()
|
|
|
|
{
|
|
|
|
return $this->is_usable;
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-10-10 11:10:43 +00:00
|
|
|
public function team()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Team::class);
|
|
|
|
}
|
2024-06-10 20:43:34 +00:00
|
|
|
|
2023-08-08 09:51:36 +00:00
|
|
|
public function awsUrl()
|
|
|
|
{
|
2023-08-07 13:31:42 +00:00
|
|
|
return "{$this->endpoint}/{$this->bucket}";
|
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
|
2023-11-15 08:34:27 +00:00
|
|
|
public function testConnection(bool $shouldSave = false)
|
2023-08-08 09:51:36 +00:00
|
|
|
{
|
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;
|
2023-10-10 12:14:41 +00:00
|
|
|
if ($this->unusable_email_sent === false && is_transactional_emails_active()) {
|
2024-07-25 20:04:05 +00:00
|
|
|
$mail = new MailMessage();
|
|
|
|
$mail->subject('Last Hour: S3 Storage Connection Error');
|
|
|
|
$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 {
|
2023-11-15 08:34:27 +00:00
|
|
|
if ($shouldSave) {
|
|
|
|
$this->save();
|
|
|
|
}
|
2023-10-10 11:10:43 +00:00
|
|
|
}
|
2023-08-07 13:31:42 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|