2023-09-22 21:31:47 +02:00
< ? php
namespace App\Models ;
2024-08-05 20:00:57 +02:00
use App\Events\FileStorageChanged ;
2023-09-22 21:31:47 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory ;
class LocalFileVolume extends BaseModel
{
use HasFactory ;
2024-06-10 22:43:34 +02:00
2023-09-22 21:31:47 +02:00
protected $guarded = [];
2024-02-14 10:21:53 +01:00
protected static function booted ()
{
static :: created ( function ( LocalFileVolume $fileVolume ) {
2024-02-14 15:00:24 +01:00
$fileVolume -> load ([ 'service' ]);
2024-03-13 18:26:30 +01:00
dispatch ( new \App\Jobs\ServerStorageSaveJob ( $fileVolume ));
2024-02-14 10:21:53 +01:00
});
}
2024-06-10 22:43:34 +02:00
2023-09-22 21:31:47 +02:00
public function service ()
{
2023-09-25 12:49:55 +02:00
return $this -> morphTo ( 'resource' );
2023-09-22 21:31:47 +02:00
}
2024-06-10 22:43:34 +02:00
2024-08-12 16:06:24 +02:00
public function loadStorageOnServer ()
{
$this -> load ([ 'service' ]);
$isService = data_get ( $this -> resource , 'service' );
if ( $isService ) {
$workdir = $this -> resource -> service -> workdir ();
$server = $this -> resource -> service -> server ;
} else {
$workdir = $this -> resource -> workdir ();
$server = $this -> resource -> destination -> server ;
}
$commands = collect ([]);
$path = data_get_str ( $this , 'fs_path' );
if ( $path -> startsWith ( '.' )) {
$path = $path -> after ( '.' );
$path = $workdir . $path ;
}
$isFile = instant_remote_process ([ " test -f $path && echo OK || echo NOK " ], $server );
if ( $isFile === 'OK' ) {
$content = instant_remote_process ([ " cat $path " ], $server , false );
$this -> content = $content ;
$this -> is_directory = false ;
$this -> save ();
}
}
2024-04-15 19:47:17 +02:00
public function deleteStorageOnServer ()
{
2024-08-21 14:31:17 +02:00
$this -> load ([ 'service' ]);
2024-04-15 19:47:17 +02:00
$isService = data_get ( $this -> resource , 'service' );
if ( $isService ) {
$workdir = $this -> resource -> service -> workdir ();
$server = $this -> resource -> service -> server ;
} else {
$workdir = $this -> resource -> workdir ();
$server = $this -> resource -> destination -> server ;
}
2024-08-05 20:00:57 +02:00
$commands = collect ([]);
2024-08-12 16:06:24 +02:00
$path = data_get_str ( $this , 'fs_path' );
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 ( $path && $path != '/' && $path != '.' && $path != '..' ) {
2024-08-05 20:00:57 +02:00
if ( $isFile === 'OK' ) {
2024-08-12 16:06:24 +02:00
$commands -> push ( " rm -rf $path > /dev/null 2>&1 || true " );
2024-06-10 22:43:34 +02:00
2024-08-05 20:00:57 +02:00
} elseif ( $isDir === 'OK' ) {
2024-08-12 16:06:24 +02:00
$commands -> push ( " rm -rf $path > /dev/null 2>&1 || true " );
$commands -> push ( " rmdir $path > /dev/null 2>&1 || true " );
2024-08-05 20:00:57 +02:00
}
}
if ( $commands -> count () > 0 ) {
return instant_remote_process ( $commands , $server );
}
2024-04-15 19:47:17 +02:00
}
2024-06-10 22:43:34 +02:00
2024-02-14 15:00:24 +01:00
public function saveStorageOnServer ()
2023-09-29 21:38:11 +02:00
{
2024-08-12 16:06:24 +02:00
$this -> load ([ 'service' ]);
2024-04-10 15:00:46 +02:00
$isService = data_get ( $this -> resource , 'service' );
if ( $isService ) {
$workdir = $this -> resource -> service -> workdir ();
$server = $this -> resource -> service -> server ;
} else {
$workdir = $this -> resource -> workdir ();
$server = $this -> resource -> destination -> server ;
}
2024-08-05 20:00:57 +02:00
$commands = collect ([]);
if ( $this -> is_directory ) {
2024-02-14 15:00:24 +01:00
$commands -> push ( " mkdir -p $this->fs_path > /dev/null 2>&1 || true " );
2024-08-05 20:00:57 +02:00
$commands -> push ( " cd $workdir " );
2024-02-14 15:00:24 +01:00
}
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 " );
}
}
2024-08-12 16:06:24 +02:00
$path = data_get_str ( $this , 'fs_path' );
$content = data_get ( $this , 'content' );
2023-09-29 21:38:11 +02:00
if ( $path -> startsWith ( '.' )) {
$path = $path -> after ( '.' );
2024-06-10 22:43:34 +02:00
$path = $workdir . $path ;
2023-09-29 21:38:11 +02:00
}
$isFile = instant_remote_process ([ " test -f $path && echo OK || echo NOK " ], $server );
$isDir = instant_remote_process ([ " test -d $path && echo OK || echo NOK " ], $server );
2024-08-12 16:06:24 +02:00
if ( $isFile == 'OK' && $this -> is_directory ) {
2024-08-05 20:00:57 +02:00
$content = instant_remote_process ([ " cat $path " ], $server , false );
2024-08-12 16:06:24 +02:00
$this -> is_directory = false ;
$this -> content = $content ;
$this -> save ();
2024-08-05 20:00:57 +02:00
FileStorageChanged :: dispatch ( data_get ( $server , 'team_id' ));
2024-06-10 22:43:34 +02: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.' );
2024-08-12 16:06:24 +02:00
} elseif ( $isDir == 'OK' && ! $this -> is_directory ) {
if ( $path == '/' || $path == '.' || $path == '..' || $path == '' || str ( $path ) -> isEmpty () || is_null ( $path )) {
$this -> is_directory = true ;
$this -> save ();
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.' );
}
instant_remote_process ([
" rm -fr $path " ,
" touch $path " ,
], $server , false );
FileStorageChanged :: dispatch ( data_get ( $server , 'team_id' ));
2023-09-29 21:38:11 +02:00
}
2024-08-12 16:06:24 +02:00
if ( $isDir == 'NOK' && ! $this -> is_directory ) {
$chmod = data_get ( $this , 'chmod' );
$chown = data_get ( $this , 'chown' );
2024-02-14 15:00:24 +01:00
if ( $content ) {
$content = base64_encode ( $content );
2024-04-17 10:49:34 +02:00
$commands -> push ( " echo ' $content ' | base64 -d | tee $path > /dev/null " );
2024-07-22 09:18:15 +02:00
} else {
$commands -> push ( " touch $path " );
}
$commands -> push ( " chmod +x $path " );
if ( $chown ) {
$commands -> push ( " chown $chown $path " );
}
if ( $chmod ) {
$commands -> push ( " chmod $chmod $path " );
2024-02-14 15:00:24 +01:00
}
2024-08-12 16:06:24 +02:00
} elseif ( $isDir == 'NOK' && $this -> is_directory ) {
2023-09-29 21:38:11 +02:00
$commands -> push ( " mkdir -p $path > /dev/null 2>&1 || true " );
}
2024-06-10 22:43:34 +02:00
2023-09-29 21:38:11 +02:00
return instant_remote_process ( $commands , $server );
}
2023-09-22 21:31:47 +02:00
}