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 = [];
|
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-08-08 09:51:36 +00:00
|
|
|
|
2023-10-10 11:10:43 +00:00
|
|
|
public function team()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Team::class);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
public function testConnection()
|
|
|
|
{
|
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;
|
|
|
|
return;
|
|
|
|
} 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()) {
|
2023-10-10 11:10:43 +00:00
|
|
|
$mail = new MailMessage();
|
|
|
|
$mail->subject('Coolify: S3 Storage Connection Error');
|
|
|
|
$mail->view('emails.s3-connection-error', ['name' => $this->name, 'reason' => $e->getMessage(), 'url' => route('team.storages.show', ['storage_uuid' => $this->uuid])]);
|
|
|
|
$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 {
|
|
|
|
$this->save();
|
|
|
|
}
|
2023-08-07 13:31:42 +00:00
|
|
|
}
|
2023-08-08 09:51:36 +00:00
|
|
|
}
|