|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Models; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Factories\HasFactory; |
| 6 | +use Illuminate\Database\Eloquent\Relations\BelongsTo; |
| 7 | + |
| 8 | +/** |
| 9 | + * @property int $user_id |
| 10 | + * @property int $server_id |
| 11 | + * @property string $path |
| 12 | + * @property string $type |
| 13 | + * @property string $server_user |
| 14 | + * @property string $name |
| 15 | + * @property int $size |
| 16 | + * @property int $links |
| 17 | + * @property string $owner |
| 18 | + * @property string $group |
| 19 | + * @property string $date |
| 20 | + * @property string $permissions |
| 21 | + * @property User $user |
| 22 | + * @property Server $server |
| 23 | + */ |
| 24 | +class File extends AbstractModel |
| 25 | +{ |
| 26 | + use HasFactory; |
| 27 | + |
| 28 | + protected $fillable = [ |
| 29 | + 'user_id', |
| 30 | + 'server_id', |
| 31 | + 'server_user', |
| 32 | + 'path', |
| 33 | + 'type', |
| 34 | + 'name', |
| 35 | + 'size', |
| 36 | + 'links', |
| 37 | + 'owner', |
| 38 | + 'group', |
| 39 | + 'date', |
| 40 | + 'permissions', |
| 41 | + ]; |
| 42 | + |
| 43 | + protected $casts = [ |
| 44 | + 'user_id' => 'integer', |
| 45 | + 'server_id' => 'integer', |
| 46 | + 'size' => 'integer', |
| 47 | + 'links' => 'integer', |
| 48 | + 'created_at' => 'datetime', |
| 49 | + 'updated_at' => 'datetime', |
| 50 | + ]; |
| 51 | + |
| 52 | + protected static function boot(): void |
| 53 | + { |
| 54 | + parent::boot(); |
| 55 | + |
| 56 | + static::deleting(function (File $file) { |
| 57 | + if ($file->name === '.' || $file->name === '..') { |
| 58 | + return false; |
| 59 | + } |
| 60 | + |
| 61 | + $file->server->os()->deleteFile($file->getFilePath(), $file->server_user); |
| 62 | + |
| 63 | + return true; |
| 64 | + }); |
| 65 | + } |
| 66 | + |
| 67 | + public function server(): BelongsTo |
| 68 | + { |
| 69 | + return $this->belongsTo(Server::class); |
| 70 | + } |
| 71 | + |
| 72 | + public function user(): BelongsTo |
| 73 | + { |
| 74 | + return $this->belongsTo(User::class); |
| 75 | + } |
| 76 | + |
| 77 | + public static function path(User $user, Server $server, string $serverUser): string |
| 78 | + { |
| 79 | + $file = self::query() |
| 80 | + ->where('user_id', $user->id) |
| 81 | + ->where('server_id', $server->id) |
| 82 | + ->where('server_user', $serverUser) |
| 83 | + ->first(); |
| 84 | + |
| 85 | + if ($file) { |
| 86 | + return $file->path; |
| 87 | + } |
| 88 | + |
| 89 | + return home_path($serverUser); |
| 90 | + } |
| 91 | + |
| 92 | + public static function parse(User $user, Server $server, string $path, string $serverUser, string $listOutput): void |
| 93 | + { |
| 94 | + self::query() |
| 95 | + ->where('user_id', $user->id) |
| 96 | + ->where('server_id', $server->id) |
| 97 | + ->delete(); |
| 98 | + |
| 99 | + // Split output by line |
| 100 | + $lines = explode("\n", trim($listOutput)); |
| 101 | + |
| 102 | + // Skip the first two lines (total count and . & .. directories) |
| 103 | + array_shift($lines); |
| 104 | + |
| 105 | + foreach ($lines as $line) { |
| 106 | + if (preg_match('/^([drwx\-]+)\s+(\d+)\s+(\w+)\s+(\w+)\s+(\d+)\s+([\w\s:\-]+)\s+(.+)$/', $line, $matches)) { |
| 107 | + $type = match ($matches[1][0]) { |
| 108 | + '-' => 'file', |
| 109 | + 'd' => 'directory', |
| 110 | + default => 'unknown', |
| 111 | + }; |
| 112 | + if ($type === 'unknown') { |
| 113 | + continue; |
| 114 | + } |
| 115 | + if ($matches[7] === '.') { |
| 116 | + continue; |
| 117 | + } |
| 118 | + self::create([ |
| 119 | + 'user_id' => $user->id, |
| 120 | + 'server_id' => $server->id, |
| 121 | + 'server_user' => $serverUser, |
| 122 | + 'path' => $path, |
| 123 | + 'type' => $type, |
| 124 | + 'name' => $matches[7], |
| 125 | + 'size' => (int) $matches[5], |
| 126 | + 'links' => (int) $matches[2], |
| 127 | + 'owner' => $matches[3], |
| 128 | + 'group' => $matches[4], |
| 129 | + 'date' => $matches[6], |
| 130 | + 'permissions' => $matches[1], |
| 131 | + ]); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + public function getFilePath(): string |
| 137 | + { |
| 138 | + return $this->path.'/'.$this->name; |
| 139 | + } |
| 140 | + |
| 141 | + public function isExtractable(): bool |
| 142 | + { |
| 143 | + $extension = pathinfo($this->name, PATHINFO_EXTENSION); |
| 144 | + |
| 145 | + return in_array($extension, ['zip', 'tar', 'tar.gz', 'bz2', 'tar.bz2']); |
| 146 | + } |
| 147 | +} |
0 commit comments