Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions app/Livewire/Maven/DirectoryIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Livewire\Maven;

use App\Maven\MavenFile;
use DirectoryIterator;
use Illuminate\Support\Facades\Storage;
use Livewire\Component;

class DirectoryIndex extends Component
{
public bool $isRoot;
public string $path;
public $files = [];

public function mount(?string $path = '')
{
$this->isRoot = $path == '';
$this->path = $path;
if (!Storage::directoryExists("maven$path")) {
return redirect(route('maven.download', $path));
}
$fullPath = Storage::path("maven/$path");
foreach (new DirectoryIterator($fullPath) as $file) {
if ($file->isDot()) continue;
$path = $file->getRealPath();
$this->files[] = MavenFile::fromPath($path);
}
}

public function render()
{
return view('livewire.maven.directory-index')
->layout('components.layouts.public.maven-header')
->title($this->isRoot ? "Index | Maven" : "Index of $this->path | Maven");
}
}
110 changes: 110 additions & 0 deletions app/Maven/MavenFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

namespace App\Maven;

use Carbon\Carbon;
use Livewire\Wireable;

class MavenFile implements Wireable
{

private string $name;
private Carbon $modified;
private int $size;
private string $hash;
private bool $isDir;

private function __construct(string $name, Carbon $modified, int $size, string $hash, bool $isDir = false)
{
$this->name = $name;
$this->modified = $modified;
$this->size = $size;
$this->hash = $hash;
$this->isDir = $isDir;
}

public function getName(): string
{
return $this->name;
}

public function getModified(): Carbon
{
return $this->modified;
}

public function getSize(): int
{
return $this->size;
}

public function getSizeFormatted(): string
{
if ($this->isDir) {
return '-';
}
if ($this->size < 1024) {
return $this->size . 'B';
}
if ($this->size < 1024 * 1024) {
return round($this->size / 1024) . 'KB';
}
if ($this->size < 1024 * 1024 * 1024) {
return round($this->size / 1024 / 1024) . 'MB';
}
return round($this->size / 1024 / 1024 / 1024) . 'GB';
}

public function getHash(): string
{
return $this->hash;
}

public function isDir(): bool
{
return $this->isDir;
}

public function getIcon(): string
{
if ($this->isDir) {
return 'folder';
}

$extension = pathinfo($this->name, PATHINFO_EXTENSION);

switch ($extension) {
case 'md5';
case 'sha1';
case 'sha256';
case 'sha512':
return 'document-check';
case 'jar':
return 'command-line';
}

return 'document';
}

public function toLivewire()
{
return ['name' => $this->name, 'modified' => $this->modified, 'size' => $this->size, 'hash' => $this->hash];
}

public static function fromLivewire($value)
{
return new static($value['name'], $value['modified'], $value['size'], $value['hash'], $value['hash'] == '-');
}

public static function fromPath(string $path)
{
$isDir = is_dir($path);
return new static(
basename($path),
Carbon::createFromTimestamp(filemtime($path)),
filesize($path),
$isDir ? '-' : hash_file('sha256', $path),
$isDir
);
}
}
11 changes: 11 additions & 0 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use App\Models\User;
use Illuminate\Support\Facades\Hash;

function authenticate_http_user(): bool {
$username = $_SERVER['PHP_AUTH_USER'];
$password = $_SERVER['PHP_AUTH_PW'];
$user = User::where('email', $username)->first();
return $user && Hash::check($password, $user->password);
}
4 changes: 3 additions & 1 deletion bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
//
$middleware->validateCsrfTokens(except: [
'maven/*'
]);
})
->withExceptions(function (Exceptions $exceptions) {
//
Expand Down
6 changes: 5 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"livewire/volt": "^1.7.0"
},
"require-dev": {
"barryvdh/laravel-ide-helper": "^3.6",
"fakerphp/faker": "^1.23",
"laravel/pail": "^1.2.2",
"laravel/pint": "^1.18",
Expand All @@ -30,7 +31,10 @@
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"files": [
"app/helpers.php"
]
},
"autoload-dev": {
"psr-4": {
Expand Down
Loading
Loading