2023-09-22 19:31:47 +00:00
< ? php
namespace App\Models ;
use Illuminate\Database\Eloquent\Factories\HasFactory ;
class LocalFileVolume extends BaseModel
{
use HasFactory ;
protected $guarded = [];
2024-02-14 09:21:53 +00:00
protected static function booted ()
{
static :: created ( function ( LocalFileVolume $fileVolume ) {
2024-02-14 14:00:24 +00:00
$fileVolume -> load ([ 'service' ]);
$fileVolume -> saveStorageOnServer ();
2024-02-14 09:21:53 +00:00
});
}
2023-09-22 19:31:47 +00:00
public function service ()
{
2023-09-25 10:49:55 +00:00
return $this -> morphTo ( 'resource' );
2023-09-22 19:31:47 +00:00
}
2024-02-14 14:00:24 +00:00
public function saveStorageOnServer ()
2023-09-29 19:38:11 +00:00
{
2024-02-14 14:00:24 +00:00
$workdir = $this -> resource -> service -> workdir ();
$server = $this -> resource -> service -> server ;
2023-09-29 19:38:11 +00:00
$commands = collect ([
" mkdir -p $workdir > /dev/null 2>&1 || true " ,
" cd $workdir "
]);
2024-02-14 14:00:24 +00:00
$is_directory = $this -> is_directory ;
if ( $is_directory ) {
$commands -> push ( " mkdir -p $this->fs_path > /dev/null 2>&1 || true " );
}
if ( str ( $this -> fs_path ) -> startsWith ( '.' ) || str ( $this -> fs_path ) -> startsWith ( '/' ) || str ( $this -> fs_path ) -> startsWith ( '~' )) {
$parent_dir = str ( $this -> fs_path ) -> beforeLast ( '/' );
if ( $parent_dir != '' ) {
$commands -> push ( " mkdir -p $parent_dir > /dev/null 2>&1 || true " );
}
}
2023-09-29 19:38:11 +00:00
$fileVolume = $this ;
2024-02-14 14:00:24 +00:00
$path = str ( data_get ( $fileVolume , 'fs_path' ));
2023-09-29 19:38:11 +00:00
$content = data_get ( $fileVolume , 'content' );
if ( $path -> startsWith ( '.' )) {
$path = $path -> after ( '.' );
$path = $workdir . $path ;
}
$isFile = instant_remote_process ([ " test -f $path && echo OK || echo NOK " ], $server );
$isDir = instant_remote_process ([ " test -d $path && echo OK || echo NOK " ], $server );
if ( $isFile == 'OK' && $fileVolume -> is_directory ) {
2024-02-14 14:00:24 +00:00
throw new \Exception ( " The following file is a file on the server, but you are trying to mark it as a directory. Please delete the file on the server or mark it as directory. " );
2023-09-29 19:38:11 +00:00
} else if ( $isDir == 'OK' && ! $fileVolume -> is_directory ) {
2024-02-14 14:00:24 +00:00
throw new \Exception ( " The following file is a directory on the server, but you are trying to mark it as a file. <br><br>Please delete the directory on the server or mark it as directory. " );
2023-09-29 19:38:11 +00:00
}
2023-10-04 07:58:39 +00:00
if ( ! $fileVolume -> is_directory && $isDir == 'NOK' ) {
2024-02-14 14:00:24 +00:00
if ( $content ) {
$content = base64_encode ( $content );
$commands -> push ( " echo ' $content ' | base64 -d > $path " );
2024-02-21 10:21:11 +00:00
$commands -> push ( " chmod +x $path " );
2024-02-14 14:00:24 +00:00
}
2024-02-14 09:13:49 +00:00
} else if ( $isDir == 'NOK' && $fileVolume -> is_directory ) {
2023-09-29 19:38:11 +00:00
$commands -> push ( " mkdir -p $path > /dev/null 2>&1 || true " );
}
return instant_remote_process ( $commands , $server );
}
2023-09-22 19:31:47 +00:00
}