mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-05 18:23:48 +00:00
added it_get_navigator_tree and it_get_folder_content
This commit is contained in:
@@ -2,16 +2,16 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Content;
|
||||
use App\File;
|
||||
use App\Folder;
|
||||
use App\Models\Content;
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Http\Requests\PublicPages\SendMessageRequest;
|
||||
use App\Http\Resources\PageResource;
|
||||
use App\Http\Tools\Demo;
|
||||
use App\Mail\SendSupportForm;
|
||||
use App\Page;
|
||||
use App\Setting;
|
||||
use App\User;
|
||||
use App\Models\Setting;
|
||||
use App\Models\Page;
|
||||
use App\Models\User;
|
||||
use Artisan;
|
||||
use Doctrine\DBAL\Driver\PDOException;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
@@ -3,15 +3,14 @@
|
||||
namespace App\Http\Controllers\FileBrowser;
|
||||
|
||||
use App\Http\Requests\FileBrowser\SearchRequest;
|
||||
use App\User;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use App\Models\User;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Folder;
|
||||
use App\File;
|
||||
use App\Share;
|
||||
use App\Models\Folder;
|
||||
use App\Models\File;
|
||||
use App\Models\Share;
|
||||
|
||||
class BrowseController extends Controller
|
||||
{
|
||||
@@ -71,13 +70,13 @@ class BrowseController extends Controller
|
||||
->pluck('item_id');
|
||||
|
||||
// Get folders and files
|
||||
$folders = Folder::with(['parent', 'shared:token,id,item_id,permission,protected,expire_in'])
|
||||
$folders = Folder::with(['parent', 'shared:token,id,item_id,permission,is_protected,expire_in'])
|
||||
->where('user_id', $user_id)
|
||||
->whereIn('unique_id', $folder_ids)
|
||||
->sortable()
|
||||
->get();
|
||||
|
||||
$files = File::with(['parent', 'shared:token,id,item_id,permission,protected,expire_in'])
|
||||
$files = File::with(['parent', 'shared:token,id,item_id,permission,is_protected,expire_in'])
|
||||
->where('user_id', $user_id)
|
||||
->whereIn('unique_id', $file_ids)
|
||||
->sortable()
|
||||
@@ -92,11 +91,12 @@ class BrowseController extends Controller
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function latest() {
|
||||
public function latest()
|
||||
{
|
||||
|
||||
// Get User
|
||||
$user = User::with(['latest_uploads' => function($query) {
|
||||
$query->sortable(['created_at' => 'desc']);
|
||||
$user = User::with(['latest_uploads' => function ($query) {
|
||||
$query->sortable(['created_at' => 'desc']);
|
||||
}])
|
||||
->where('id', Auth::id())
|
||||
->first();
|
||||
@@ -109,7 +109,8 @@ class BrowseController extends Controller
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function participant_uploads() {
|
||||
public function participant_uploads()
|
||||
{
|
||||
|
||||
// Get User
|
||||
$uploads = File::with(['parent'])
|
||||
@@ -125,29 +126,24 @@ class BrowseController extends Controller
|
||||
* Get directory with files
|
||||
*
|
||||
* @param Request $request
|
||||
* @param $unique_id
|
||||
* @param $id
|
||||
* @return Collection
|
||||
*/
|
||||
public function folder(Request $request, $unique_id)
|
||||
public function folder(Request $request, $id)
|
||||
{
|
||||
// Get user
|
||||
$user_id = Auth::id();
|
||||
|
||||
// Get folder trash items
|
||||
if ($request->query('trash')) {
|
||||
|
||||
// Get folders and files
|
||||
$folders = Folder::onlyTrashed()
|
||||
->with('parent')
|
||||
->where('user_id', $user_id)
|
||||
->where('parent_id', $unique_id)
|
||||
->where('parent_id', $id)
|
||||
->sortable()
|
||||
->get();
|
||||
|
||||
$files = File::onlyTrashed()
|
||||
->with('parent')
|
||||
->where('user_id', $user_id)
|
||||
->where('folder_id', $unique_id)
|
||||
->where('folder_id', $id)
|
||||
->sortable()
|
||||
->get();
|
||||
|
||||
@@ -156,20 +152,19 @@ class BrowseController extends Controller
|
||||
}
|
||||
|
||||
// Get folders and files
|
||||
$folders = Folder::with(['parent', 'shared:token,id,item_id,permission,protected,expire_in'])
|
||||
->where('user_id', $user_id)
|
||||
->where('parent_id', $unique_id)
|
||||
$folders = Folder::with(['parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in'])
|
||||
->where('parent_id', $id)
|
||||
->sortable()
|
||||
->get();
|
||||
|
||||
$files = File::with(['parent', 'shared:token,id,item_id,permission,protected,expire_in'])
|
||||
->where('user_id', $user_id)
|
||||
->where('folder_id', $unique_id)
|
||||
$files = File::with(['parent:id,name', 'shared:token,id,item_id,permission,is_protected,expire_in'])
|
||||
->where('folder_id', $id)
|
||||
->sortable()
|
||||
->get();
|
||||
|
||||
// Collect folders and files to single array
|
||||
return collect([$folders, $files])->collapse();
|
||||
return collect([$folders, $files])
|
||||
->collapse();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -177,20 +172,18 @@ class BrowseController extends Controller
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function navigation_tree() {
|
||||
|
||||
$folders = Folder::with('folders:id,parent_id,unique_id,name')
|
||||
->where('parent_id', 0)
|
||||
->where('user_id', Auth::id())
|
||||
public function navigation_tree()
|
||||
{
|
||||
$folders = Folder::with('folders:id,parent_id,id,name')
|
||||
->where('parent_id', null)
|
||||
->sortable()
|
||||
->get(['id', 'parent_id', 'unique_id', 'name']);
|
||||
->get(['id', 'parent_id', 'id', 'name']);
|
||||
|
||||
return [
|
||||
[
|
||||
'unique_id' => 0,
|
||||
'name' => __('vuefilemanager.home'),
|
||||
'location' => 'base',
|
||||
'folders' => $folders,
|
||||
'folders' => $folders,
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
@@ -172,7 +172,7 @@ class File extends Model
|
||||
*/
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(Folder::class, 'folder_id', 'unique_id');
|
||||
return $this->belongsTo(Folder::class, 'folder_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -180,7 +180,7 @@ class File extends Model
|
||||
*/
|
||||
public function folder()
|
||||
{
|
||||
return $this->hasOne(Folder::class, 'unique_id', 'folder_id');
|
||||
return $this->hasOne(Folder::class, 'id', 'folder_id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -188,7 +188,7 @@ class File extends Model
|
||||
*/
|
||||
public function shared()
|
||||
{
|
||||
return $this->hasOne(Share::class, 'item_id', 'unique_id');
|
||||
return $this->hasOne(Share::class, 'item_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -19,7 +19,9 @@ class Folder extends Model
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'items', 'trashed_items'
|
||||
'items',
|
||||
'trashed_items',
|
||||
'type'
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
@@ -40,6 +42,11 @@ class Folder extends Model
|
||||
|
||||
protected $keyType = 'string';
|
||||
|
||||
public function getTypeAttribute()
|
||||
{
|
||||
return 'folder';
|
||||
}
|
||||
|
||||
/**
|
||||
* Index folder
|
||||
*
|
||||
@@ -100,7 +107,7 @@ class Folder extends Model
|
||||
*/
|
||||
public function getDeletedAtAttribute()
|
||||
{
|
||||
if (! $this->attributes['deleted_at']) return null;
|
||||
if (!$this->attributes['deleted_at']) return null;
|
||||
|
||||
return format_date(set_time_by_user_timezone($this->attributes['deleted_at']), __('vuefilemanager.time'));
|
||||
}
|
||||
@@ -112,12 +119,12 @@ class Folder extends Model
|
||||
*/
|
||||
public function parent()
|
||||
{
|
||||
return $this->belongsTo(Folder::class, 'parent_id', 'unique_id');
|
||||
return $this->belongsTo(Folder::class, 'parent_id', 'id');
|
||||
}
|
||||
|
||||
public function folderIds()
|
||||
{
|
||||
return $this->children()->with('folderIds')->select(['unique_id', 'parent_id']);
|
||||
return $this->children()->with('folderIds')->select(['id', 'parent_id']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -127,7 +134,7 @@ class Folder extends Model
|
||||
*/
|
||||
public function files()
|
||||
{
|
||||
return $this->hasMany(File::class, 'folder_id', 'unique_id');
|
||||
return $this->hasMany(File::class, 'folder_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -138,7 +145,7 @@ class Folder extends Model
|
||||
public function trashed_files()
|
||||
{
|
||||
|
||||
return $this->hasMany(File::class, 'folder_id', 'unique_id')->withTrashed();
|
||||
return $this->hasMany(File::class, 'folder_id', 'id')->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,7 +165,7 @@ class Folder extends Model
|
||||
*/
|
||||
public function trashed_folders()
|
||||
{
|
||||
return $this->children()->with('trashed_folders')->withTrashed()->select(['parent_id', 'unique_id', 'name']);
|
||||
return $this->children()->with('trashed_folders')->withTrashed()->select(['parent_id', 'id', 'name']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -168,7 +175,7 @@ class Folder extends Model
|
||||
*/
|
||||
public function children()
|
||||
{
|
||||
return $this->hasMany(Folder::class, 'parent_id', 'unique_id');
|
||||
return $this->hasMany(Folder::class, 'parent_id', 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -178,7 +185,7 @@ class Folder extends Model
|
||||
*/
|
||||
public function trashed_children()
|
||||
{
|
||||
return $this->hasMany(Folder::class, 'parent_id', 'unique_id')->withTrashed();
|
||||
return $this->hasMany(Folder::class, 'parent_id', 'id')->withTrashed();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -202,19 +209,19 @@ class Folder extends Model
|
||||
|
||||
static::deleting(function ($item) {
|
||||
|
||||
if ( $item->isForceDeleting() ) {
|
||||
if ($item->isForceDeleting()) {
|
||||
|
||||
$item->trashed_children()->each(function($folder) {
|
||||
$item->trashed_children()->each(function ($folder) {
|
||||
$folder->forceDelete();
|
||||
});
|
||||
|
||||
} else {
|
||||
|
||||
$item->children()->each(function($folder) {
|
||||
$item->children()->each(function ($folder) {
|
||||
$folder->delete();
|
||||
});
|
||||
|
||||
$item->files()->each(function($file) {
|
||||
$item->files()->each(function ($file) {
|
||||
$file->delete();
|
||||
});
|
||||
}
|
||||
@@ -223,12 +230,12 @@ class Folder extends Model
|
||||
static::restoring(function ($item) {
|
||||
|
||||
// Restore children folders
|
||||
$item->trashed_children()->each(function($folder) {
|
||||
$item->trashed_children()->each(function ($folder) {
|
||||
$folder->restore();
|
||||
});
|
||||
|
||||
// Restore children files
|
||||
$item->trashed_files()->each(function($files) {
|
||||
$item->trashed_files()->each(function ($files) {
|
||||
$files->restore();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -137,7 +137,7 @@ class User extends Authenticatable
|
||||
*/
|
||||
public function getFolderTreeAttribute()
|
||||
{
|
||||
return File::with(['folders.shared', 'shared:token,id,item_id,permission,protected,expire_in'])
|
||||
return File::with(['folders.shared', 'shared:token,id,item_id,permission,is_protected,expire_in'])
|
||||
->where('parent_id', 0)
|
||||
->where('user_id', $this->id)
|
||||
->sortable()
|
||||
@@ -244,7 +244,7 @@ class User extends Authenticatable
|
||||
public function favourite_folders()
|
||||
{
|
||||
return $this->belongsToMany(Folder::class, 'favourite_folder', 'user_id', 'folder_id', 'id', 'id')
|
||||
->with('shared:token,id,item_id,permission,protected,expire_in');
|
||||
->with('shared:token,id,item_id,permission,is_protected,expire_in');
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -24,7 +24,7 @@ class FolderFactory extends Factory
|
||||
return [
|
||||
'id' => $this->faker->uuid,
|
||||
'user_id' => $this->faker->uuid,
|
||||
'name' => $this->faker->name,
|
||||
'name' => $this->faker->word,
|
||||
'user_scope' => $this->faker->randomElement(
|
||||
['master', 'editor', 'visitor']
|
||||
),
|
||||
|
||||
@@ -49,8 +49,8 @@ Route::group(['middleware' => ['auth:sanctum']], function () {
|
||||
Route::group(['prefix' => 'browse'], function () {
|
||||
Route::get('/participant-uploads', [BrowseController::class, 'participant_uploads']);
|
||||
Route::get('/navigation', [BrowseController::class, 'navigation_tree']);
|
||||
Route::get('/folders/{unique_id}', [BrowseController::class, 'folder']);
|
||||
Route::get('/shared-all', [BrowseController::class, 'shared']);
|
||||
Route::get('/folders/{id}', [BrowseController::class, 'folder']);
|
||||
Route::get('/sharing', [BrowseController::class, 'shared']);
|
||||
Route::get('/latest', [BrowseController::class, 'latest']);
|
||||
Route::get('/search', [BrowseController::class, 'search']);
|
||||
});
|
||||
|
||||
@@ -2,23 +2,150 @@
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\File;
|
||||
use App\Models\Folder;
|
||||
use App\Models\User;
|
||||
use Illuminate\Foundation\Testing\DatabaseMigrations;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Foundation\Testing\WithFaker;
|
||||
use Laravel\Sanctum\Sanctum;
|
||||
use Tests\TestCase;
|
||||
|
||||
class BrowseTest extends TestCase
|
||||
{
|
||||
use DatabaseMigrations;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_navigator_tree()
|
||||
{
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$folder_level_1 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 1',
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_2 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 2',
|
||||
'parent_id' => $folder_level_1->id,
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_3 = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 3',
|
||||
'parent_id' => $folder_level_2->id,
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$folder_level_2_sibling = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'name' => 'level 2 Sibling',
|
||||
'parent_id' => $folder_level_1->id,
|
||||
'user_scope' => 'master',
|
||||
'user_id' => $user->id,
|
||||
]);
|
||||
|
||||
$this->getJson("/api/browse/navigation")
|
||||
->assertStatus(200)
|
||||
->assertJson([
|
||||
[
|
||||
"name" => "Home",
|
||||
"location" => "base",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_1->id,
|
||||
"parent_id" => null,
|
||||
"name" => "level 1",
|
||||
"items" => 2,
|
||||
"trashed_items" => 2,
|
||||
"type" => "folder",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_2->id,
|
||||
"parent_id" => $folder_level_1->id,
|
||||
"name" => "level 2",
|
||||
"items" => 1,
|
||||
"trashed_items" => 1,
|
||||
"type" => "folder",
|
||||
"folders" => [
|
||||
[
|
||||
"id" => $folder_level_3->id,
|
||||
"user_id" => $user->id,
|
||||
"parent_id" => $folder_level_2->id,
|
||||
"name" => "level 3",
|
||||
"color" => null,
|
||||
"emoji" => null,
|
||||
"user_scope" => "master",
|
||||
"deleted_at" => null,
|
||||
"created_at" => $folder_level_3->created_at,
|
||||
"updated_at" => $folder_level_3->updated_at->toJson(),
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
"folders" => [],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
"id" => $folder_level_2_sibling->id,
|
||||
"parent_id" => $folder_level_1->id,
|
||||
"name" => "level 2 Sibling",
|
||||
"items" => 0,
|
||||
"trashed_items" => 0,
|
||||
"type" => "folder",
|
||||
"folders" => []
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
public function it_get_folder_files()
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
public function it_get_folder_content()
|
||||
{
|
||||
$root = Folder::factory(Folder::class)
|
||||
->create();
|
||||
|
||||
$folder = Folder::factory(Folder::class)
|
||||
->create([
|
||||
'parent_id' => $root->id
|
||||
]);
|
||||
|
||||
$file = File::factory(File::class)
|
||||
->create([
|
||||
'folder_id' => $root->id
|
||||
]);
|
||||
|
||||
$user = User::factory(User::class)
|
||||
->create();
|
||||
|
||||
Sanctum::actingAs($user);
|
||||
|
||||
$response = $this->getJson("/api/browse/folders/$root->id")
|
||||
->assertStatus(200);
|
||||
|
||||
collect([$folder, $file])
|
||||
->each(function ($item) use ($response) {
|
||||
$response->assertJsonFragment([
|
||||
'id' => $item->id,
|
||||
'type' => $item->type,
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
public function it_get_searched_file()
|
||||
|
||||
@@ -27,11 +27,11 @@ class FileTest extends TestCase
|
||||
*/
|
||||
public function it_test_file_factory()
|
||||
{
|
||||
$folder = File::factory(File::class)
|
||||
$file = File::factory(File::class)
|
||||
->create();
|
||||
|
||||
$this->assertDatabaseHas('files', [
|
||||
'id' => $folder->id
|
||||
'id' => $file->id
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user