lasthourcloud/app/Models/LocalFileVolume.php

113 lines
4.2 KiB
PHP
Raw Permalink Normal View History

2023-09-22 19:31:47 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class LocalFileVolume extends BaseModel
{
use HasFactory;
2024-06-10 20:43:34 +00:00
2023-09-22 19:31:47 +00:00
protected $guarded = [];
2024-02-14 09:21:53 +00:00
protected static function booted()
{
static::created(function (LocalFileVolume $fileVolume) {
$fileVolume->load(['service']);
2024-03-13 17:26:30 +00:00
dispatch(new \App\Jobs\ServerStorageSaveJob($fileVolume));
2024-02-14 09:21:53 +00:00
});
}
2024-06-10 20:43:34 +00:00
2023-09-22 19:31:47 +00:00
public function service()
{
return $this->morphTo('resource');
2023-09-22 19:31:47 +00:00
}
2024-06-10 20:43:34 +00:00
public function deleteStorageOnServer()
{
$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([
2024-06-10 20:43:34 +00:00
"cd $workdir",
]);
$fs_path = data_get($this, 'fs_path');
if ($fs_path && $fs_path != '/' && $fs_path != '.' && $fs_path != '..') {
$commands->push("rm -rf $fs_path");
}
ray($commands);
2024-06-10 20:43:34 +00:00
return instant_remote_process($commands, $server);
}
2024-06-10 20:43:34 +00:00
public function saveStorageOnServer()
{
2024-04-10 13:00:46 +00: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;
}
$commands = collect([
"mkdir -p $workdir > /dev/null 2>&1 || true",
2024-06-10 20:43:34 +00:00
"cd $workdir",
]);
$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");
}
}
$fileVolume = $this;
$path = str(data_get($fileVolume, 'fs_path'));
$content = data_get($fileVolume, 'content');
if ($path->startsWith('.')) {
$path = $path->after('.');
2024-06-10 20:43:34 +00:00
$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) {
$fileVolume->is_directory = false;
$fileVolume->save();
2024-06-10 20:43:34 +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.');
} elseif ($isDir == 'OK' && ! $fileVolume->is_directory) {
$fileVolume->is_directory = true;
$fileVolume->save();
2024-06-10 20:43:34 +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.');
}
if ($isDir == 'NOK' && ! $fileVolume->is_directory) {
$chmod = data_get($fileVolume, 'chmod');
$chown = data_get($fileVolume, 'chown');
if ($content) {
$content = base64_encode($content);
$commands->push("echo '$content' | base64 -d | tee $path > /dev/null");
} 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-06-10 20:43:34 +00:00
} elseif ($isDir == 'NOK' && $fileVolume->is_directory) {
$commands->push("mkdir -p $path > /dev/null 2>&1 || true");
}
2024-06-10 20:43:34 +00:00
return instant_remote_process($commands, $server);
}
2023-09-22 19:31:47 +00:00
}