- added file resource

- frontend refactoring
This commit is contained in:
Peter Papp
2021-08-26 18:01:57 +02:00
parent f5f2179145
commit 5c6a873b02
37 changed files with 339 additions and 773 deletions

View File

@@ -1,10 +1,15 @@
<?php
namespace Domain\Browsing\Controllers;
use Domain\Files\Resources\FilesCollection;
use Domain\Folders\Resources\FolderCollection;
use Domain\Folders\Resources\FolderResource;
use Illuminate\Http\Request;
use Domain\Files\Models\File;
use Domain\Folders\Models\Folder;
use Illuminate\Support\Facades\Auth;
use Str;
class BrowseFolderController
{
@@ -12,11 +17,8 @@ class BrowseFolderController
Request $request,
string $id,
): array {
$root_id = $id === 'undefined' ? null : $id;
$root_id = Str::isUuid($id) ? $id : null;
$requestedFolder = $root_id ? Folder::findOrFail($root_id) : null;
// Get folders and files
$folders = Folder::with(['parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in'])
->where('parent_id', $root_id)
->where('team_folder', false)
@@ -32,8 +34,9 @@ class BrowseFolderController
// Collect folders and files to single array
return [
'content' => collect([$folders, $files])->collapse(),
'folder' => $requestedFolder,
'folders' => new FolderCollection($folders),
'files' => new FilesCollection($files),
'root' => $root_id ? new FolderResource(Folder::findOrFail($root_id)) : null,
];
}
}

View File

@@ -1,6 +1,8 @@
<?php
namespace Domain\Files\Resources;
use Domain\Sharing\Resources\ShareResource;
use Illuminate\Http\Resources\Json\JsonResource;
class FileResource extends JsonResource
@@ -15,19 +17,28 @@ class FileResource extends JsonResource
{
return [
'data' => [
'id' => $this->id,
'type' => 'file',
'attributes' => [
'id' => $this->id,
'type' => $this->type,
'attributes' => [
'name' => $this->name,
'basename' => $this->basename,
'mimetype' => $this->mimetype,
'filesize' => $this->filesize,
'type' => $this->type,
'file_url' => $this->file_url,
'thumbnail' => $this->thumbnail,
'created_at' => $this->created_at,
'updated_at' => $this->created_at,
'metadata' => $this->metadata,
'updated_at' => format_date(
set_time_by_user_timezone($this->updated_at), __t('time')
),
'created_at' => format_date(
set_time_by_user_timezone($this->created_at), __t('time')
),
],
'relationships' => [
$this->mergeWhen($this->shared, fn() => [
'shared' => new ShareResource($this->shared),
]),
]
],
];
}

View File

@@ -0,0 +1,16 @@
<?php
namespace Domain\Files\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class FilesCollection extends ResourceCollection
{
public $collects = FileResource::class;
public function toArray($request): array
{
return [
'data' => $this->collection,
];
}
}

View File

@@ -2,6 +2,7 @@
namespace Domain\Folders\Resources;
use Domain\Sharing\Resources\ShareResource;
use Domain\Teams\Resources\TeamInvitationsCollection;
use Domain\Teams\Resources\TeamMembersCollection;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -18,6 +19,7 @@ class FolderResource extends JsonResource
'name' => $this->name,
'color' => $this->color,
'emoji' => $this->emoji,
'filesize' => $this->filesize,
'isTeamFolder' => $this->team_folder,
'items' => $this->items,
'trashed_items' => $this->trashed_items,
@@ -29,26 +31,14 @@ class FolderResource extends JsonResource
),
],
'relationships' => [
$this->mergeWhen($this->teamMembers, fn () => [
$this->mergeWhen($this->teamMembers, fn() => [
'members' => new TeamMembersCollection($this->teamMembers),
]),
$this->mergeWhen($this->teamInvitations, fn () => [
$this->mergeWhen($this->teamInvitations, fn() => [
'invitations' => new TeamInvitationsCollection($this->teamInvitations),
]),
$this->mergeWhen($this->shared, fn () => [
'shared' => [
'data' => [
'id' => $this->shared->id,
'type' => 'shared',
'attributes' => [
'expire_in' => $this->shared->expire_in,
'link' => $this->shared->link,
'permission' => $this->shared->permission,
'protected' => $this->shared->protected,
'token' => $this->shared->token,
],
],
],
$this->mergeWhen($this->shared, fn() => [
'shared' => new ShareResource($this->shared),
]),
],
],

View File

@@ -1,4 +1,5 @@
<?php
namespace Domain\Sharing\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
@@ -15,18 +16,16 @@ class ShareResource extends JsonResource
{
return [
'data' => [
'id' => (string) $this->id,
'type' => 'shares',
'id' => $this->id,
'type' => 'shared',
'attributes' => [
'permission' => $this->permission,
'is_protected' => $this->is_protected,
'item_id' => $this->item_id,
'expire_in' => (int) $this->expire_in,
'token' => $this->token,
'link' => $this->link,
'type' => $this->type,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'permission' => $this->permission,
'protected' => $this->is_protected,
'item_id' => $this->item_id,
'expire_in' => (int)$this->expire_in,
'token' => $this->token,
'link' => $this->link,
'type' => $this->type,
],
],
];

View File

@@ -7,6 +7,7 @@ use Domain\Teams\Requests\CreateTeamFolderRequest;
use Domain\Teams\Requests\UpdateTeamFolderMembersRequest;
use Illuminate\Contracts\Routing\ResponseFactory;
use Domain\Files\Models\File;
use Illuminate\Http\Resources\Json\ResourceCollection;
use Illuminate\Http\Response;
use Domain\Folders\Models\Folder;
use App\Http\Controllers\Controller;
@@ -51,11 +52,12 @@ class TeamFoldersController extends Controller
->get();
}
return new FolderCollection($folders);
// Collect folders and files to single array
return [
'content' => collect([$folders, $files])->collapse(),
'content' => collect([
new FolderCollection($folders),
$files
])->collapse(),
'folder' => $requestedFolder,
];
}