mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-18 16:22:14 +00:00
zip api update
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
APP_NAME=Laravel
|
APP_NAME=Laravel
|
||||||
APP_ENV=local
|
APP_ENV=local
|
||||||
APP_KEY=base64:yzhXnwQenw4j+JAuM4CrRiNKyIznOSnET2NJFxW66CQ=
|
APP_KEY=base64:w38JvwR4OwWaggjhc23zHJeOh/7hiHTf512npivEVNE=
|
||||||
APP_DEBUG=true
|
APP_DEBUG=true
|
||||||
APP_URL=http://localhost
|
APP_URL=http://localhost
|
||||||
APP_DEMO=false
|
APP_DEMO=false
|
||||||
|
|||||||
@@ -1,39 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace Domain\Zip\Actions;
|
|
||||||
|
|
||||||
use Domain\Files\Models\File;
|
|
||||||
use Domain\Folders\Models\Folder;
|
|
||||||
|
|
||||||
class GetItemsListFromUrlParamAction
|
|
||||||
{
|
|
||||||
public function __invoke(): array
|
|
||||||
{
|
|
||||||
$list = explode(',', request()->get('items'));
|
|
||||||
|
|
||||||
$itemList = collect($list)
|
|
||||||
->map(function ($chunk) {
|
|
||||||
$items = explode('|', $chunk);
|
|
||||||
|
|
||||||
return [
|
|
||||||
'id' => $items[0],
|
|
||||||
'type' => $items[1],
|
|
||||||
];
|
|
||||||
});
|
|
||||||
|
|
||||||
$folderIds = $itemList
|
|
||||||
->where('type', 'folder')
|
|
||||||
->pluck('id');
|
|
||||||
|
|
||||||
$fileIds = $itemList
|
|
||||||
->where('type', 'file')
|
|
||||||
->pluck('id');
|
|
||||||
|
|
||||||
$folders = Folder::whereIn('id', $folderIds)
|
|
||||||
->get();
|
|
||||||
|
|
||||||
$files = File::whereIn('id', $fileIds)
|
|
||||||
->get();
|
|
||||||
|
|
||||||
return [$folders, $files];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Domain\Zip\Controllers;
|
namespace Domain\Zip\Controllers;
|
||||||
|
|
||||||
|
use Domain\Folders\Models\Folder;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
use ZipStream\ZipStream;
|
use ZipStream\ZipStream;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Domain\Files\Models\File;
|
use Domain\Files\Models\File;
|
||||||
@@ -10,12 +13,10 @@ use App\Http\Controllers\Controller;
|
|||||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||||
use Domain\Sharing\Actions\ProtectShareRecordAction;
|
use Domain\Sharing\Actions\ProtectShareRecordAction;
|
||||||
use Domain\Sharing\Actions\VerifyAccessToItemAction;
|
use Domain\Sharing\Actions\VerifyAccessToItemAction;
|
||||||
use Domain\Zip\Actions\GetItemsListFromUrlParamAction;
|
|
||||||
|
|
||||||
class VisitorZipController extends Controller
|
class VisitorZipController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public GetItemsListFromUrlParamAction $getItemsListFromUrlParam,
|
|
||||||
public ProtectShareRecordAction $protectShareRecord,
|
public ProtectShareRecordAction $protectShareRecord,
|
||||||
public VerifyAccessToItemAction $verifyAccessToItem,
|
public VerifyAccessToItemAction $verifyAccessToItem,
|
||||||
public RecordDownloadAction $recordDownload,
|
public RecordDownloadAction $recordDownload,
|
||||||
@@ -23,11 +24,38 @@ class VisitorZipController extends Controller
|
|||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ValidationException
|
||||||
|
*/
|
||||||
public function __invoke(
|
public function __invoke(
|
||||||
Request $request,
|
Request $request,
|
||||||
Share $shared,
|
Share $shared,
|
||||||
): ZipStream {
|
): ZipStream {
|
||||||
list($folders, $files) = ($this->getItemsListFromUrlParam)();
|
$items = extractItemsFromGetAttribute($request->get('items'));
|
||||||
|
|
||||||
|
// Validate items GET attribute
|
||||||
|
Validator::make(['items' => $items->toArray()], [
|
||||||
|
'items' => 'array',
|
||||||
|
'items.*.id' => 'required|uuid',
|
||||||
|
'items.*.type' => 'required|string',
|
||||||
|
])->validate();
|
||||||
|
|
||||||
|
// Get list of folders and files from requested url parameter
|
||||||
|
$folderIds = $items
|
||||||
|
->where('type', 'folder')
|
||||||
|
->pluck('id');
|
||||||
|
|
||||||
|
$fileIds = $items
|
||||||
|
->where('type', 'file')
|
||||||
|
->pluck('id');
|
||||||
|
|
||||||
|
$folders = Folder::query()
|
||||||
|
->whereIn('id', $folderIds)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$files = File::query()
|
||||||
|
->whereIn('id', $fileIds)
|
||||||
|
->get();
|
||||||
|
|
||||||
// Check access to requested folders
|
// Check access to requested folders
|
||||||
if ($folders->isNotEmpty()) {
|
if ($folders->isNotEmpty()) {
|
||||||
@@ -52,8 +80,7 @@ class VisitorZipController extends Controller
|
|||||||
$zip = ($this->zip)($folders, $files, $shared);
|
$zip = ($this->zip)($folders, $files, $shared);
|
||||||
|
|
||||||
($this->recordDownload)(
|
($this->recordDownload)(
|
||||||
file_size: $zip->predictZipSize(),
|
$zip->predictZipSize(), $shared->user_id
|
||||||
user_id: $shared->user_id,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return $zip;
|
return $zip;
|
||||||
|
|||||||
@@ -1,34 +1,61 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Domain\Zip\Controllers;
|
namespace Domain\Zip\Controllers;
|
||||||
|
|
||||||
|
use Domain\Files\Models\File;
|
||||||
|
use Domain\Folders\Models\Folder;
|
||||||
|
use Illuminate\Support\Facades\Validator;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
use ZipStream\ZipStream;
|
use ZipStream\ZipStream;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Domain\Zip\Actions\ZipAction;
|
use Domain\Zip\Actions\ZipAction;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use Domain\Traffic\Actions\RecordDownloadAction;
|
use Domain\Traffic\Actions\RecordDownloadAction;
|
||||||
use Domain\Zip\Actions\GetItemsListFromUrlParamAction;
|
|
||||||
|
|
||||||
class ZipController extends Controller
|
class ZipController extends Controller
|
||||||
{
|
{
|
||||||
public function __construct(
|
public function __construct(
|
||||||
public ZipAction $zip,
|
public ZipAction $zip,
|
||||||
public RecordDownloadAction $recordDownload,
|
public RecordDownloadAction $recordDownload,
|
||||||
public GetItemsListFromUrlParamAction $getItemsListFromUrlParam,
|
|
||||||
) {
|
) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ValidationException
|
||||||
|
*/
|
||||||
public function __invoke(
|
public function __invoke(
|
||||||
Request $request,
|
Request $request,
|
||||||
): ZipStream {
|
): ZipStream {
|
||||||
|
$items = extractItemsFromGetAttribute($request->get('items'));
|
||||||
|
|
||||||
|
// Validate items GET attribute
|
||||||
|
Validator::make(['items' => $items->toArray()], [
|
||||||
|
'items' => 'array',
|
||||||
|
'items.*.id' => 'required|uuid',
|
||||||
|
'items.*.type' => 'required|string',
|
||||||
|
])->validate();
|
||||||
|
|
||||||
// Get list of folders and files from requested url parameter
|
// Get list of folders and files from requested url parameter
|
||||||
list($folders, $files) = ($this->getItemsListFromUrlParam)();
|
$folderIds = $items
|
||||||
|
->where('type', 'folder')
|
||||||
|
->pluck('id');
|
||||||
|
|
||||||
|
$fileIds = $items
|
||||||
|
->where('type', 'file')
|
||||||
|
->pluck('id');
|
||||||
|
|
||||||
|
$folders = Folder::query()
|
||||||
|
->whereIn('id', $folderIds)
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$files = File::query()
|
||||||
|
->whereIn('id', $fileIds)
|
||||||
|
->get();
|
||||||
|
|
||||||
// Zip items
|
// Zip items
|
||||||
$zip = ($this->zip)($folders, $files);
|
$zip = ($this->zip)($folders, $files);
|
||||||
|
|
||||||
($this->recordDownload)(
|
($this->recordDownload)(
|
||||||
file_size: $zip->predictZipSize(),
|
$zip->predictZipSize(), auth()->id()
|
||||||
user_id: auth()->id(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
return $zip;
|
return $zip;
|
||||||
|
|||||||
@@ -1178,3 +1178,34 @@ if (! function_exists('replace_occurrence')) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!function_exists('extractItemsFromGetAttribute')) {
|
||||||
|
/**
|
||||||
|
* Extract items from get url attribute
|
||||||
|
*/
|
||||||
|
function extractItemsFromGetAttribute(string $string): Collection
|
||||||
|
{
|
||||||
|
return collect(
|
||||||
|
explode(',', $string)
|
||||||
|
)->map(function ($chunk) {
|
||||||
|
// explode single attribute chunk
|
||||||
|
$items = explode('|', $chunk);
|
||||||
|
|
||||||
|
// Abort code if keys doesn't exists
|
||||||
|
if (! array_key_exists(0, $items) || ! array_key_exists(1, $items)) {
|
||||||
|
abort(
|
||||||
|
response()->json([
|
||||||
|
'type' => 'error',
|
||||||
|
'message' => 'Incorrect argument format.',
|
||||||
|
], 422)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// return item attributes
|
||||||
|
return [
|
||||||
|
'id' => $items[0],
|
||||||
|
'type' => $items[1],
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ class FileTest extends TestCase
|
|||||||
'id' => $file->id,
|
'id' => $file->id,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
$this->assertDatabaseHas('files', [
|
$this->assertDatabaseHas('files', [
|
||||||
'id' => $file->id,
|
'id' => $file->id,
|
||||||
@@ -288,7 +288,7 @@ class FileTest extends TestCase
|
|||||||
'force_delete' => true,
|
'force_delete' => true,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
// Assert primary file was deleted
|
// Assert primary file was deleted
|
||||||
Storage::assertMissing("files/$user->id/fake-image.jpeg");
|
Storage::assertMissing("files/$user->id/fake-image.jpeg");
|
||||||
@@ -328,7 +328,7 @@ class FileTest extends TestCase
|
|||||||
'force_delete' => false,
|
'force_delete' => false,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
$files
|
$files
|
||||||
->each(function ($file) {
|
->each(function ($file) {
|
||||||
@@ -379,7 +379,7 @@ class FileTest extends TestCase
|
|||||||
'force_delete' => true,
|
'force_delete' => true,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
$file_ids
|
$file_ids
|
||||||
->each(function ($id, $index) use ($user) {
|
->each(function ($id, $index) use ($user) {
|
||||||
|
|||||||
@@ -180,7 +180,7 @@ class FolderTest extends TestCase
|
|||||||
'id' => $children->id,
|
'id' => $children->id,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
$this->assertEquals(
|
$this->assertEquals(
|
||||||
$root->id,
|
$root->id,
|
||||||
@@ -225,7 +225,7 @@ class FolderTest extends TestCase
|
|||||||
'force_delete' => false,
|
'force_delete' => false,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
collect([$folder_1, $folder_2])
|
collect([$folder_1, $folder_2])
|
||||||
->each(function ($folder) {
|
->each(function ($folder) {
|
||||||
@@ -273,7 +273,7 @@ class FolderTest extends TestCase
|
|||||||
'force_delete' => true,
|
'force_delete' => true,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
$this->assertDatabaseMissing('folders', [
|
$this->assertDatabaseMissing('folders', [
|
||||||
'id' => $folder_1->id,
|
'id' => $folder_1->id,
|
||||||
@@ -326,7 +326,7 @@ class FolderTest extends TestCase
|
|||||||
'force_delete' => false,
|
'force_delete' => false,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
collect([$file_1, $file_2])
|
collect([$file_1, $file_2])
|
||||||
->each(function ($file) {
|
->each(function ($file) {
|
||||||
@@ -392,7 +392,7 @@ class FolderTest extends TestCase
|
|||||||
'force_delete' => $index,
|
'force_delete' => $index,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
});
|
});
|
||||||
|
|
||||||
$uploaded_files
|
$uploaded_files
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ class TrashTest extends TestCase
|
|||||||
'force_delete' => false,
|
'force_delete' => false,
|
||||||
],
|
],
|
||||||
],
|
],
|
||||||
])->assertStatus(204);
|
])->assertStatus(200);
|
||||||
|
|
||||||
$this->deleteJson('/api/trash/dump')
|
$this->deleteJson('/api/trash/dump')
|
||||||
->assertStatus(200);
|
->assertStatus(200);
|
||||||
|
|||||||
Reference in New Issue
Block a user