mirror of
https://github.com/VueFileManager/vuefilemanager.git
synced 2026-04-28 19:10:40 +00:00
test refactoring
This commit is contained in:
@@ -7,7 +7,6 @@ use Domain\Files\Models\File;
|
||||
use Domain\Folders\Models\Folder;
|
||||
use Laravel\Sanctum\HasApiTokens;
|
||||
use Domain\Traffic\Models\Traffic;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Database\Factories\UserFactory;
|
||||
use Domain\Settings\Models\Setting;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace App\Users\Resources;
|
||||
|
||||
use ByteUnits\Metric;
|
||||
@@ -21,18 +20,18 @@ class UserStorageResource extends JsonResource
|
||||
|
||||
$totalCapacity = match (get_settings('subscription_type')) {
|
||||
'metered' => $this->usedCapacity / 1000000000,
|
||||
'fixed' => $this->limitations->max_storage_amount,
|
||||
default => $this->limitations->max_storage_amount,
|
||||
'fixed' => $this->limitations->max_storage_amount,
|
||||
default => $this->limitations->max_storage_amount,
|
||||
};
|
||||
|
||||
return [
|
||||
'data' => [
|
||||
'id' => (string)$this->id,
|
||||
'id' => (string) $this->id,
|
||||
'type' => 'storage',
|
||||
'attributes' => [
|
||||
'used' => Metric::bytes($this->usedCapacity)->format(),
|
||||
'capacity' => format_gigabytes($totalCapacity),
|
||||
'percentage' => (float)get_storage_percentage($this->usedCapacity, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($this->usedCapacity, $totalCapacity),
|
||||
],
|
||||
'meta' => [
|
||||
'traffic' => [
|
||||
@@ -45,23 +44,23 @@ class UserStorageResource extends JsonResource
|
||||
],
|
||||
'images' => [
|
||||
'used' => Metric::bytes($images)->format(),
|
||||
'percentage' => (float)get_storage_percentage($images, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($images, $totalCapacity),
|
||||
],
|
||||
'audios' => [
|
||||
'used' => Metric::bytes($audios)->format(),
|
||||
'percentage' => (float)get_storage_percentage($audios, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($audios, $totalCapacity),
|
||||
],
|
||||
'videos' => [
|
||||
'used' => Metric::bytes($videos)->format(),
|
||||
'percentage' => (float)get_storage_percentage($videos, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($videos, $totalCapacity),
|
||||
],
|
||||
'documents' => [
|
||||
'used' => Metric::bytes($documents)->format(),
|
||||
'percentage' => (float)get_storage_percentage($documents, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($documents, $totalCapacity),
|
||||
],
|
||||
'others' => [
|
||||
'used' => Metric::bytes($others)->format(),
|
||||
'percentage' => (float)get_storage_percentage($others, $totalCapacity),
|
||||
'percentage' => (float) get_storage_percentage($others, $totalCapacity),
|
||||
],
|
||||
],
|
||||
],
|
||||
@@ -133,13 +132,13 @@ class UserStorageResource extends JsonResource
|
||||
->orderBy('created_at')
|
||||
->get();
|
||||
|
||||
$upload = $trafficRecords->map(fn($record) => [
|
||||
$upload = $trafficRecords->map(fn ($record) => [
|
||||
'created_at' => format_date($record->created_at, '%d. %B'),
|
||||
'percentage' => $uploadMax !== 0 ? round(($record->upload / $uploadMax) * 100, 2) : 0,
|
||||
'amount' => Metric::bytes($record->upload)->format(),
|
||||
]);
|
||||
|
||||
$download = $trafficRecords->map(fn($record) => [
|
||||
$download = $trafficRecords->map(fn ($record) => [
|
||||
'created_at' => format_date($record->created_at, '%d. %B'),
|
||||
'percentage' => $downloadMax !== 0 ? round(($record->download / $downloadMax) * 100, 2) : 0,
|
||||
'amount' => Metric::bytes($record->download)->format(),
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
<?php
|
||||
|
||||
namespace Domain\Admin\Controllers\Dashboard;
|
||||
|
||||
use ByteUnits\Metric;
|
||||
@@ -66,13 +65,13 @@ class GetDashboardDataController extends Controller
|
||||
->groupBy('created_at')
|
||||
->get();
|
||||
|
||||
$upload = $trafficRecords->map(fn($record) => [
|
||||
$upload = $trafficRecords->map(fn ($record) => [
|
||||
'created_at' => format_date($record->created_at, '%d. %B'),
|
||||
'percentage' => intval($trafficRecords->max('upload')) !== 0 ? round(($record->upload / $trafficRecords->max('upload')) * 100, 2) : 0,
|
||||
'amount' => Metric::bytes($record->upload)->format(),
|
||||
]);
|
||||
|
||||
$download = $trafficRecords->map(fn($record) => [
|
||||
$download = $trafficRecords->map(fn ($record) => [
|
||||
'created_at' => format_date($record->created_at, '%d. %B'),
|
||||
'percentage' => intval($trafficRecords->max('download')) !== 0 ? round(($record->download / $trafficRecords->max('download')) * 100, 2) : 0,
|
||||
'amount' => Metric::bytes($record->download)->format(),
|
||||
|
||||
@@ -1,22 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Domain\Files\Actions;
|
||||
namespace Domain\Files\Actions;
|
||||
|
||||
use Domain\Folders\Models\Folder;
|
||||
|
||||
|
||||
class CreateFolderStructureAction
|
||||
{
|
||||
/**
|
||||
* Create a new action instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Prepare the action for execution, leveraging constructor injection.
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the action.
|
||||
*
|
||||
@@ -24,7 +12,6 @@ class CreateFolderStructureAction
|
||||
*/
|
||||
public function __invoke($path, $parent, $user_id)
|
||||
{
|
||||
|
||||
$folders = array_slice(explode('/', $path), 1, -1);
|
||||
|
||||
$parent_id = $parent;
|
||||
@@ -35,44 +22,36 @@ class CreateFolderStructureAction
|
||||
$structure = Folder::whereIn('name', $folders)->with('parent')->get();
|
||||
|
||||
// If file have some parent folders
|
||||
if( count($folders) > 0) {
|
||||
|
||||
if (count($folders) > 0) {
|
||||
// If uploading structure has same lenght as a already existed structure
|
||||
if( count($folders) === count($structure) ) {
|
||||
|
||||
if (count($folders) === count($structure)) {
|
||||
// Get correct file parent from the already craeted structure
|
||||
$last_folder = $this->get_file_parent($structure, $folders);
|
||||
|
||||
} else if ( count($folders) !== count($structure) ) {
|
||||
|
||||
|
||||
if( count($structure) > 0 ) {
|
||||
|
||||
} elseif (count($folders) !== count($structure)) {
|
||||
if (count($structure) > 0) {
|
||||
// Check what folders are missed in structure and return missed folder with last created folder in structure
|
||||
$data = $this->check_exist_folders($structure, $folders);
|
||||
|
||||
$folders = $data[0];
|
||||
|
||||
$parent_id = $data[1];
|
||||
|
||||
}
|
||||
|
||||
// Create folders
|
||||
foreach($folders as $folder) {
|
||||
|
||||
foreach ($folders as $folder) {
|
||||
$new_folder = Folder::create([
|
||||
'name' => $folder,
|
||||
'parent_id' => $parent_id,
|
||||
'user_id' => $user_id,
|
||||
]);
|
||||
|
||||
'name' => $folder,
|
||||
'parent_id' => $parent_id,
|
||||
'user_id' => $user_id,
|
||||
]);
|
||||
|
||||
$parent_id = $new_folder->id;
|
||||
|
||||
|
||||
$last_folder = $new_folder->id;
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $last_folder;
|
||||
}
|
||||
|
||||
@@ -83,44 +62,38 @@ class CreateFolderStructureAction
|
||||
{
|
||||
$parent_name = '';
|
||||
|
||||
foreach(array_reverse($folders) as $folder) {
|
||||
|
||||
foreach (array_reverse($folders) as $folder) {
|
||||
$item = $structure->where('name', $folder);
|
||||
|
||||
$parent = $item->pluck('parent')->pluck('name')[0];
|
||||
|
||||
// Check if folder have valid parent name
|
||||
if( $parent && $folder === $parent_name || $parent_name == '') {
|
||||
|
||||
if ($parent && $folder === $parent_name || $parent_name == '') {
|
||||
$parent_name = $parent;
|
||||
}
|
||||
|
||||
|
||||
return $structure->where('name', $folders[array_key_last($folders)])->first()->id;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the folders that is need to create in already created structure and last created parent
|
||||
*/
|
||||
private function check_exist_folders($structure, $folders)
|
||||
private function check_exist_folders($structure, $folders): array
|
||||
{
|
||||
$create_folders = [];
|
||||
$last_parent = '';
|
||||
|
||||
$create_folders = [];
|
||||
$last_parent = '';
|
||||
|
||||
foreach($folders as $folder) {
|
||||
|
||||
foreach ($folders as $folder) {
|
||||
// Filter folders that is need to create
|
||||
if(! $structure->where('name', $folder)->first()) {
|
||||
|
||||
array_push($create_folders, $folder);
|
||||
}else {
|
||||
|
||||
if (! $structure->where('name', $folder)->first()) {
|
||||
array_push($create_folders, $folder);
|
||||
} else {
|
||||
// Find last created folder
|
||||
$last_parent = $structure->where('name', $folder)->first()->id;
|
||||
}
|
||||
}
|
||||
$last_parent = $structure->where('name', $folder)->first()->id;
|
||||
}
|
||||
}
|
||||
|
||||
return [$create_folders, $last_parent];
|
||||
return [$create_folders, $last_parent];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ use Domain\Files\Requests\UploadRequest;
|
||||
use Domain\Files\Models\File as UserFile;
|
||||
use Domain\Traffic\Actions\RecordUploadAction;
|
||||
use App\Users\Exceptions\InvalidUserActionException;
|
||||
use Domain\Files\Actions\CreateFolderStructureAction;
|
||||
use Illuminate\Contracts\Filesystem\FileNotFoundException;
|
||||
|
||||
class UploadFileAction
|
||||
{
|
||||
@@ -25,7 +25,7 @@ class UploadFileAction
|
||||
/**
|
||||
* Upload new file
|
||||
*
|
||||
* @throws InvalidUserActionException
|
||||
* @throws InvalidUserActionException|FileNotFoundException
|
||||
*/
|
||||
public function __invoke(
|
||||
UploadRequest $request,
|
||||
|
||||
Reference in New Issue
Block a user