get and browse team folders shared with me

This commit is contained in:
Čarodej
2021-09-24 16:56:18 +02:00
parent 37cad85a86
commit 07b249346b
10 changed files with 450 additions and 59 deletions
@@ -1,4 +1,5 @@
<?php
namespace App\Console\Commands;
use App\Users\Models\User;
@@ -34,10 +35,11 @@ class SetupDevEnvironment extends Command
public function __construct(
private CreateDiskDirectoriesAction $createDiskDirectories,
private SeedDefaultSettingsAction $seedDefaultSettings,
private SeedDefaultLanguageAction $seedDefaultLanguage,
private SeedDefaultPagesAction $seedDefaultPages,
) {
private SeedDefaultSettingsAction $seedDefaultSettings,
private SeedDefaultLanguageAction $seedDefaultLanguage,
private SeedDefaultPagesAction $seedDefaultPages,
)
{
parent::__construct();
$this->setUpFaker();
}
@@ -71,6 +73,7 @@ class SetupDevEnvironment extends Command
$this->info('Creating default demo content...');
$this->create_admin_default_content();
$this->create_team_folders_content();
$this->create_share_with_me_team_folders_content();
$this->create_share_records();
$this->info('Clearing application cache...');
@@ -125,26 +128,32 @@ class SetupDevEnvironment extends Command
collect([
[
'avatar' => 'avatar-02.png',
'email' => 'alice@vuefilemanager.com',
],
[
'avatar' => 'avatar-03.png',
'email' => $this->faker->email,
],
[
'avatar' => 'avatar-04.png',
'email' => $this->faker->email,
],
[
'avatar' => 'avatar-05.png',
'email' => $this->faker->email,
],
[
'avatar' => 'avatar-06.png',
'email' => $this->faker->email,
],
[
'avatar' => 'avatar-07.png',
'email' => $this->faker->email,
],
])->each(function ($user) {
$newbie = User::forceCreate([
'role' => 'user',
'email' => $this->faker->email,
'email' => $user['email'],
'password' => bcrypt('vuefilemanager'),
'email_verified_at' => now(),
]);
@@ -756,7 +765,7 @@ class SetupDevEnvironment extends Command
collect([$members[0]->id, $members[1]->id])
->each(
fn ($id) => DB::table('team_folder_members')
fn($id) => DB::table('team_folder_members')
->insert([
'parent_id' => $companyProjectFolder->id,
'user_id' => $id,
@@ -766,7 +775,7 @@ class SetupDevEnvironment extends Command
collect([$members[2]->id, $members[3]->id])
->each(
fn ($id) => DB::table('team_folder_members')
fn($id) => DB::table('team_folder_members')
->insert([
'parent_id' => $financeDocumentsFolder->id,
'user_id' => $id,
@@ -777,7 +786,7 @@ class SetupDevEnvironment extends Command
// Create invitations
collect([$members[4], $members[5]])
->each(
fn ($user) => TeamFolderInvitation::factory()
fn($user) => TeamFolderInvitation::factory()
->create([
'email' => $user->email,
'parent_id' => $companyProjectFolder->id,
@@ -787,6 +796,96 @@ class SetupDevEnvironment extends Command
);
}
public function create_share_with_me_team_folders_content(): void
{
$member = User::whereEmail('howdy@hi5ve.digital')
->first();
$owner = User::whereEmail('alice@vuefilemanager.com')
->first();
$folder = Folder::factory()
->create([
'user_id' => $owner->id,
'team_folder' => true,
'name' => "Alice's Project Files",
]);
$memes = Folder::factory()
->create([
'user_id' => $owner->id,
'parent_id' => $folder->id,
'name' => '9 Gag',
]);
$hug = Folder::factory()
->create([
'user_id' => $owner->id,
'parent_id' => $folder->id,
'name' => 'Digital Hug',
]);
DB::table('team_folder_members')
->insert([
'parent_id' => $folder->id,
'user_id' => $member->id,
'permission' => 'can-edit',
]);
// Get meme gallery
collect([
'Sofishticated.jpg',
'whaaaaat.jpg',
'You Are My Sunshine.jpg',
])
->each(function ($file) use ($owner, $memes) {
$basename = Str::random(12) . '-' . $file;
// Copy file into app storage
Storage::putFileAs("files/$owner->id", storage_path("demo/images/memes/$file"), $basename, 'private');
Storage::putFileAs("files/$owner->id", storage_path("demo/images/memes/thumbnail-$file"), "thumbnail-$basename", 'private');
// Create file record
File::create([
'parent_id' => $memes->id,
'user_id' => $owner->id,
'name' => $file,
'basename' => $basename,
'type' => 'image',
'author' => 'user',
'mimetype' => 'jpg',
'filesize' => rand(1000000, 4000000),
'thumbnail' => "thumbnail-$basename",
'created_at' => now()->subMinutes(rand(1, 5)),
]);
});
collect([
'Eggcited bro.jpg',
'Get a Rest.jpg',
])
->each(function ($file) use ($owner, $hug) {
$basename = Str::random(12) . '-' . $file;
// Copy file into app storage
Storage::putFileAs("files/$owner->id", storage_path("demo/images/memes/$file"), $basename, 'private');
Storage::putFileAs("files/$owner->id", storage_path("demo/images/memes/thumbnail-$file"), "thumbnail-$basename", 'private');
// Create file record
File::create([
'parent_id' => $hug->id,
'user_id' => $owner->id,
'name' => $file,
'basename' => $basename,
'type' => 'image',
'author' => 'user',
'mimetype' => 'jpg',
'filesize' => rand(1000000, 4000000),
'thumbnail' => "thumbnail-$basename",
'created_at' => now()->subMinutes(rand(1, 5)),
]);
});
}
private function create_share_records(): void
{
$user = User::whereEmail('howdy@hi5ve.digital')
@@ -1,9 +1,8 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use Illuminate\Http\Request;
use Gate;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Domain\Files\Models\File as UserFile;
use Domain\Files\Actions\DownloadFileAction;
use Domain\Traffic\Actions\RecordDownloadAction;
@@ -14,28 +13,26 @@ class GetFileController extends Controller
public function __construct(
private RecordDownloadAction $recordDownload,
private DownloadFileAction $downloadFile,
) {
}
) {}
/**
* Get file
*/
public function __invoke(
Request $request,
string $filename,
): BinaryFileResponse {
// Get file record
$file = UserFile::withTrashed()
->where('user_id', Auth::id())
->where('basename', $filename)
->firstOrFail();
if (! Gate::any(['can-edit', 'can-visit'], [$file, null])) {
abort(403, 'Access Denied');
}
// Store user download size
($this->recordDownload)(
file_size: (int) $file->getRawOriginal('filesize'),
user_id: Auth::id(),
user_id: $file->user_id,
);
return ($this->downloadFile)($file, Auth::id());
return ($this->downloadFile)($file, $file->user_id);
}
}
@@ -1,9 +1,9 @@
<?php
namespace Domain\Files\Controllers\FileAccess;
use Gate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Domain\Files\Models\File as UserFile;
use Domain\Files\Actions\DownloadThumbnailAction;
use Symfony\Component\HttpFoundation\StreamedResponse;
@@ -13,21 +13,21 @@ class GetThumbnailController extends Controller
{
public function __construct(
private DownloadThumbnailAction $downloadThumbnail,
) {
}
) {}
/**
* Get image thumbnail
*/
public function __invoke(
Request $request,
string $filename,
): FileNotFoundException | StreamedResponse {
$file = UserFile::withTrashed()
->whereUserId(Auth::id())
->whereThumbnail($filename)
->where('thumbnail', $filename)
->firstOrFail();
return ($this->downloadThumbnail)($file, Auth::id());
if (! Gate::any(['can-edit', 'can-visit'], [$file, null])) {
abort(403, 'Access Denied');
}
return ($this->downloadThumbnail)($file, $file->user_id);
}
}
@@ -1,6 +1,10 @@
<?php
namespace Domain\Teams\Controllers;
use Domain\Files\Resources\FilesCollection;
use Domain\Folders\Resources\FolderCollection;
use Domain\Folders\Resources\FolderResource;
use Str;
use Domain\Files\Models\File;
use Domain\Folders\Models\Folder;
@@ -11,43 +15,35 @@ class BrowseSharedWithMeController
{
public function __invoke($id): array
{
$rootId = Str::isUuid($id) ? $id : null;
$requestedFolder = Str::isUuid($id) ? Folder::findOrFail($rootId) : null;
$id = Str::isUuid($id) ? $id : null;
$relations = [
'parent:id,name',
'shared:token,id,item_id,permission,is_protected,expire_in',
];
if ($rootId) {
$folders = Folder::with($relations)
->where('id', $id)
if ($id) {
$folders = Folder::with(['parent:id,name'])
->where('parent_id', $id)
->sortable()
->get();
$files = File::with($relations)
$files = File::with(['parent:id,name'])
->where('parent_id', $id)
->sortable()
->get();
}
if (! $rootId) {
$folderIds = DB::table('team_folder_members')
if (!$id) {
$sharedFolderIds = DB::table('team_folder_members')
->where('user_id', Auth::id())
->pluck('parent_id');
$folders = Folder::with($relations)
->whereIn('id', $folderIds)
$folders = Folder::whereIn('id', $sharedFolderIds)
->sortable()
->get();
$files = [];
}
// Collect folders and files to single array
return [
'content' => collect([$folders, $files])->collapse(),
'folder' => $requestedFolder,
'root' => $id ? new FolderResource(Folder::findOrFail($id)) : null,
'folders' => new FolderCollection($folders),
'files' => isset($files) ? new FilesCollection($files) : new FilesCollection([]),
'teamFolder' => $id ? new FolderResource(Folder::findOrFail($id)->getLatestParent()) : null,
];
}
}