Add api for listing subdirectories
continuous-integration/drone/push Build is failing Details

This commit is contained in:
philipp lang 2024-06-28 23:56:34 +02:00
parent 0ad6b40393
commit 71903936a3
11 changed files with 958 additions and 842 deletions

3
.gitmodules vendored
View File

@ -14,3 +14,6 @@
path = packages/medialibrary-helper
url = https://git.zoomyboy.de/zoomyboy/medialibrary-helper.git
branch = version2
[submodule "packages/flysystem-webdav"]
path = packages/flysystem-webdav
url = https://github.com/zoomyboy/flysystem-webdav.git

View File

@ -0,0 +1,19 @@
<?php
namespace App\Fileshare\Actions;
use App\Fileshare\Data\ResourceData;
use App\Fileshare\Models\Fileshare;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
use Spatie\LaravelData\DataCollection;
class ListFilesAction
{
use AsAction;
public function handle(ActionRequest $request, Fileshare $fileshare): DataCollection
{
return ResourceData::collection($fileshare->type->getSubDirectories($request->input('parent')))->wrap('data');
}
}

View File

@ -2,7 +2,10 @@
namespace App\Fileshare\ConnectionTypes;
use App\Fileshare\Data\ResourceData;
use Illuminate\Filesystem\FilesystemAdapter;
use Spatie\LaravelData\Data;
use Spatie\LaravelData\DataCollection;
abstract class ConnectionType extends Data
{
@ -15,6 +18,8 @@ abstract class ConnectionType extends Data
abstract public static function title(): string;
abstract public function getFilesystem(): FilesystemAdapter;
/**
* @return array<int, array{label: string, key: string, type: string}>
*/
@ -32,4 +37,14 @@ abstract class ConnectionType extends Data
->map(fn ($file) => ['id' => $file, 'name' => $file::title(), 'defaults' => $file::defaults(), 'fields' => $file::fields()])
->toArray();
}
/**
* @return array<int, string>
*/
public function getSubDirectories(?string $parent): array
{
$filesystem = $this->getFilesystem();
return $filesystem->directories($parent ?: '/');
}
}

View File

@ -2,8 +2,12 @@
namespace App\Fileshare\ConnectionTypes;
use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Http\Client\ConnectionException;
use Illuminate\Support\Facades\Http;
use League\Flysystem\Filesystem;
use League\Flysystem\WebDAV\WebDAVAdapter;
use Sabre\DAV\Client;
use Spatie\LaravelData\Attributes\MapInputName;
use Spatie\LaravelData\Attributes\MapOutputName;
use Spatie\LaravelData\Mappers\SnakeCaseMapper;
@ -58,4 +62,15 @@ class OwncloudConnection extends ConnectionType
['label' => 'Passwort', 'key' => 'password', 'type' => 'password'],
];
}
public function getFilesystem(): FilesystemAdapter
{
$adapter = new WebDAVAdapter(new Client([
'baseUri' => $this->baseUrl . '/remote.php/dav/files/' . $this->user,
'userName' => $this->user,
'password' => $this->password,
]), '/remote.php/dav/files/' . $this->user);
return new FilesystemAdapter(new Filesystem($adapter), $adapter);
}
}

View File

@ -0,0 +1,24 @@
<?php
namespace App\Fileshare\Data;
use Spatie\LaravelData\Data;
class ResourceData extends Data
{
public function __construct(public $name, public $path, public $parent)
{
}
public static function fromString(string $path): self
{
$dir = '/' . trim($path, '\\/');
return self::from([
'path' => $dir,
'name' => pathinfo($dir, PATHINFO_FILENAME),
'parent' => pathinfo($dir, PATHINFO_DIRNAME),
]);
}
}

View File

@ -14,6 +14,13 @@
"symlink": true
}
},
{
"type": "path",
"url": "./packages/flysystem-webdav",
"options": {
"symlink": true
}
},
{
"type": "path",
"url": "./packages/tex",
@ -46,7 +53,6 @@
"laravel/tinker": "^2.0",
"laravel/ui": "^3.0",
"league/csv": "^9.9",
"league/flysystem-webdav": "^3.28",
"lorisleiva/laravel-actions": "^2.4",
"meilisearch/meilisearch-php": "^1.6",
"monicahq/laravel-sabre": "^1.6",
@ -60,6 +66,7 @@
"worksome/request-factories": "^2.5",
"zoomyboy/laravel-nami": "dev-master",
"zoomyboy/medialibrary-helper": "dev-master as 1.0",
"league/flysystem-webdav": "dev-master as 3.28.0",
"zoomyboy/osm": "1.0.3",
"zoomyboy/phone": "^1.0",
"zoomyboy/tex": "dev-main as 1.0"

1649
composer.lock generated

File diff suppressed because it is too large Load Diff

@ -0,0 +1 @@
Subproject commit c892034c6b6ebe9c70164a352ae78d1634463c4c

View File

@ -22,6 +22,7 @@ use App\Efz\ShowEfzDocumentAction;
use App\Fileshare\Actions\FileshareApiIndexAction;
use App\Fileshare\Actions\FileshareStoreAction;
use App\Fileshare\Actions\FileshareUpdateAction;
use App\Fileshare\Actions\ListFilesAction;
use App\Form\Actions\ExportAction as ActionsExportAction;
use App\Form\Actions\FormDestroyAction;
use App\Form\Actions\FormIndexAction;
@ -174,4 +175,5 @@ Route::group(['middleware' => 'auth:web'], function (): void {
Route::post('/fileshare', FileshareStoreAction::class)->name('fileshare.store');
Route::patch('/fileshare/{fileshare}', FileshareUpdateAction::class)->name('fileshare.update');
Route::get('/api/fileshare', FileshareApiIndexAction::class)->name('api.fileshare.index');
Route::post('/api/fileshare/{fileshare}/files', ListFilesAction::class)->name('api.fileshare.files');
});

View File

@ -17,9 +17,51 @@ class FileshareFilesActionTest extends FileshareTestCase
->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')]))
->create();
$this->postJson(route('fileshare.files', ['fileshare' => $connection]), [
$this->postJson(route('api.fileshare.files', ['fileshare' => $connection]), [
'parent' => null,
])->assertJsonPath('data.0.name', 'pictures')
->assertJsonPath('data.1.name', 'lala');
])
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.name', 'lala')
->assertJsonPath('data.0.path', '/lala')
->assertJsonPath('data.0.parent', '/')
->assertJsonPath('data.1.name', 'pictures')
->assertJsonPath('data.1.path', '/pictures')
->assertJsonPath('data.1.parent', '/');
}
public function testItGetsSubdirectories(): void
{
$this->withoutExceptionHandling()->login()->loginNami()->withOwncloudUser('badenpowell', 'secret')
->withDirs('badenpowell', ['/pictures', '/lala', '/lala/dd', '/lala/ff']);
$connection = Fileshare::factory()
->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')]))
->create();
$this->postJson(route('api.fileshare.files', ['fileshare' => $connection]), ['parent' => '/pictures'])->assertJsonCount(0, 'data');
$this->postJson(route('api.fileshare.files', ['fileshare' => $connection]), ['parent' => '/lala'])
->assertJsonCount(2, 'data')
->assertJsonPath('data.0.name', 'dd')
->assertJsonPath('data.0.path', '/lala/dd')
->assertJsonPath('data.0.parent', '/lala')
->assertJsonPath('data.1.name', 'ff')
->assertJsonPath('data.1.path', '/lala/ff')
->assertJsonPath('data.1.parent', '/lala');
}
public function testItGetsSubdirectoriesOfSubdirectory(): void
{
$this->withoutExceptionHandling()->login()->loginNami()->withOwncloudUser('badenpowell', 'secret')
->withDirs('badenpowell', ['/lala', '/lala/dd', '/lala/dd/ee']);
$connection = Fileshare::factory()
->type(OwncloudConnection::from(['user' => 'badenpowell', 'password' => 'secret', 'base_url' => env('TEST_OWNCLOUD_DOMAIN')]))
->create();
$this->postJson(route('api.fileshare.files', ['fileshare' => $connection]), ['parent' => '/lala/dd'])
->assertJsonCount(1, 'data')
->assertJsonPath('data.0.name', 'ee')
->assertJsonPath('data.0.path', '/lala/dd/ee')
->assertJsonPath('data.0.parent', '/lala/dd');
}
}

View File

@ -7,9 +7,11 @@ use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Client\PendingRequest;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Storage;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\Filesystem;
use League\Flysystem\WebDAV\WebDAVAdapter;
use Sabre\DAV\Client;
use Throwable;
abstract class FileshareTestCase extends TestCase
{
@ -55,16 +57,15 @@ abstract class FileshareTestCase extends TestCase
{
$adapter = $this->adapter($username);
foreach ($adapter->directories() as $directory) {
foreach ($adapter->directories('/') as $directory) {
$adapter->deleteDirectory($directory);
}
dd($adapter->directories());
$adapter->makeDirectory('lala');
$adapter->makeDirectory('aa/zz');
$adapter->makeDirectory('aa/zz');
dd($adapter->directories());
// dd(array_map(fn ($d) => str_replace('remote.php/dav/files/' . $username, '', $d), $adapter->directories()));
foreach ($dirs as $dir) {
$adapter->makeDirectory($dir);
}
return $this;
}
private function adapter(string $username): FilesystemAdapter