2024-05-13 15:23:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
use Sushi\Sushi;
|
|
|
|
|
|
|
|
class FileItem extends Model
|
|
|
|
{
|
|
|
|
use Sushi;
|
|
|
|
|
2024-05-13 17:07:04 +00:00
|
|
|
protected static string $rootPath;
|
2024-05-13 15:23:58 +00:00
|
|
|
|
|
|
|
protected static string $path;
|
|
|
|
|
|
|
|
protected array $schema = [
|
|
|
|
'name' => 'string',
|
|
|
|
'dateModified' => 'datetime',
|
|
|
|
'size' => 'integer',
|
|
|
|
'type' => 'string',
|
|
|
|
];
|
|
|
|
|
2024-05-13 17:07:04 +00:00
|
|
|
public static function queryForDiskAndPath(string $rootPath = 'public', string $path = ''): Builder
|
2024-05-13 15:23:58 +00:00
|
|
|
{
|
2024-05-13 17:07:04 +00:00
|
|
|
static::$rootPath = $rootPath;
|
2024-05-13 15:23:58 +00:00
|
|
|
static::$path = $path;
|
|
|
|
|
|
|
|
return static::query();
|
|
|
|
}
|
|
|
|
|
2024-05-13 15:43:21 +00:00
|
|
|
public function storageInstance()
|
|
|
|
{
|
|
|
|
return Storage::build([
|
|
|
|
'driver' => 'local',
|
|
|
|
'throw' => false,
|
2024-05-13 17:07:04 +00:00
|
|
|
'root' => static::$rootPath,
|
2024-05-13 15:43:21 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2024-05-13 15:23:58 +00:00
|
|
|
public function isFolder(): bool
|
|
|
|
{
|
|
|
|
return $this->type === 'Folder'
|
2024-05-13 15:43:21 +00:00
|
|
|
&& is_dir($this->storageInstance()->path($this->path));
|
2024-05-13 15:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isPreviousPath(): bool
|
|
|
|
{
|
|
|
|
return $this->name === '..';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function delete(): bool
|
|
|
|
{
|
|
|
|
if ($this->isFolder()) {
|
2024-05-13 15:43:21 +00:00
|
|
|
return $this->storageInstance()->deleteDirectory($this->path);
|
2024-05-13 15:23:58 +00:00
|
|
|
}
|
|
|
|
|
2024-05-13 15:43:21 +00:00
|
|
|
return $this->storageInstance()->delete($this->path);
|
2024-05-13 15:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function canOpen(): bool
|
|
|
|
{
|
|
|
|
return $this->type !== 'Folder'
|
2024-05-13 15:43:21 +00:00
|
|
|
&& $this->storageInstance()->exists($this->path)
|
|
|
|
&& $this->storageInstance()->getVisibility($this->path) === FilesystemContract::VISIBILITY_PUBLIC;
|
2024-05-13 15:23:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRows(): array
|
|
|
|
{
|
|
|
|
$backPath = [];
|
|
|
|
if (self::$path) {
|
|
|
|
$path = Str::of(self::$path)->explode('/');
|
|
|
|
|
|
|
|
$backPath = [
|
|
|
|
[
|
|
|
|
'name' => '..',
|
|
|
|
'dateModified' => null,
|
|
|
|
'size' => null,
|
|
|
|
'type' => 'Folder',
|
|
|
|
'path' => $path->count() > 1 ? $path->take($path->count() - 1)->join('/') : '',
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2024-05-13 15:43:21 +00:00
|
|
|
$storage = $this->storageInstance();
|
2024-05-13 15:23:58 +00:00
|
|
|
|
|
|
|
return collect($backPath)->push(
|
|
|
|
...collect($storage->directories(static::$path))
|
|
|
|
->sort()
|
|
|
|
->map(fn (string $directory): array => [
|
|
|
|
'name' => Str::remove(self::$path.'/', $directory),
|
|
|
|
'dateModified' => $storage->lastModified($directory),
|
|
|
|
'size' => null,
|
|
|
|
'type' => 'Folder',
|
|
|
|
'path' => $directory,
|
2024-05-13 16:57:25 +00:00
|
|
|
]),
|
2024-05-13 15:23:58 +00:00
|
|
|
...collect($storage->files(static::$path))
|
|
|
|
->sort()
|
|
|
|
->map(fn (string $file): array => [
|
|
|
|
'name' => Str::remove(self::$path.'/', $file),
|
|
|
|
'dateModified' => $storage->lastModified($file),
|
|
|
|
'size' => $storage->size($file),
|
2024-05-13 16:57:25 +00:00
|
|
|
'type' => $storage->mimeType($file) ?: 'File',
|
2024-05-13 15:23:58 +00:00
|
|
|
'path' => $file,
|
2024-05-13 17:07:04 +00:00
|
|
|
])
|
2024-05-13 15:23:58 +00:00
|
|
|
)->toArray();
|
2024-05-13 16:41:27 +00:00
|
|
|
|
2024-05-13 15:23:58 +00:00
|
|
|
}
|
|
|
|
}
|