This commit is contained in:
F4ilji
2026-04-13 14:37:57 +05:00
parent 5eb5a7bc06
commit 2e77978d5c
16 changed files with 241 additions and 34 deletions
@@ -0,0 +1,56 @@
<?php
namespace App\Containers\Dashboard\Actions\ContentBuilder;
use App\Containers\Dashboard\Tasks\Files\UploadFileTask;
use Illuminate\Http\UploadedFile;
class UploadContentBuilderFilesAction
{
public function __construct(
private readonly UploadFileTask $uploadFileTask,
) {}
/**
* Загружает массив файлов и возвращает метаданные
*
* @param UploadedFile[] $files Массив загруженных файлов
* @return array[] Массив метаданных файлов ['path', 'url', 'original_name', 'extension', 'size']
*/
public function run(array $files): array
{
$results = [];
foreach ($files as $file) {
if (!$file instanceof UploadedFile) {
continue;
}
$uploadResult = $this->uploadFileTask->run($file);
$results[] = [
'path' => $uploadResult['path'],
'url' => $uploadResult['url'],
'title' => $uploadResult['original_name'],
'expansion' => strtolower($file->getClientOriginalExtension()),
'size' => $this->formatFileSize($file->getSize()),
];
}
return $results;
}
/**
* Форматирует размер файла в читаемый вид
*/
private function formatFileSize(int $bytes): string
{
$units = ['B', 'KB', 'MB', 'GB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= (1 << (10 * $pow));
return round($bytes, 2) . ' ' . $units[$pow];
}
}
@@ -3,6 +3,7 @@
namespace App\Containers\Dashboard\UI\WEB\Controllers; namespace App\Containers\Dashboard\UI\WEB\Controllers;
use App\Containers\AppStructure\Models\Page; use App\Containers\AppStructure\Models\Page;
use App\Containers\Dashboard\Actions\ContentBuilder\UploadContentBuilderFilesAction;
use App\Containers\Dashboard\Actions\Pages\CreatePageAction; use App\Containers\Dashboard\Actions\Pages\CreatePageAction;
use App\Containers\Dashboard\Actions\Pages\DeletePageAction; use App\Containers\Dashboard\Actions\Pages\DeletePageAction;
use App\Containers\Dashboard\Actions\Pages\ListPagesAction; use App\Containers\Dashboard\Actions\Pages\ListPagesAction;
@@ -10,6 +11,7 @@ use App\Containers\Dashboard\Actions\Pages\UpdatePageAction;
use App\Containers\Dashboard\UI\WEB\Requests\StorePageRequest; use App\Containers\Dashboard\UI\WEB\Requests\StorePageRequest;
use App\Containers\Dashboard\UI\WEB\Requests\UpdatePageRequest; use App\Containers\Dashboard\UI\WEB\Requests\UpdatePageRequest;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse; use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Inertia\Inertia; use Inertia\Inertia;
@@ -21,6 +23,7 @@ class PageController extends Controller
private readonly CreatePageAction $createPageAction, private readonly CreatePageAction $createPageAction,
private readonly UpdatePageAction $updatePageAction, private readonly UpdatePageAction $updatePageAction,
private readonly DeletePageAction $deletePageAction, private readonly DeletePageAction $deletePageAction,
private readonly UploadContentBuilderFilesAction $uploadContentBuilderFilesAction,
) {} ) {}
/** /**
@@ -115,4 +118,29 @@ class PageController extends Controller
->with('error', 'Ошибка при удалении страницы: ' . $e->getMessage()); ->with('error', 'Ошибка при удалении страницы: ' . $e->getMessage());
} }
} }
/**
* Загружает файлы для ContentBuilder и возвращает метаданные
*/
public function uploadFiles(Request $request): JsonResponse
{
$request->validate([
'files' => 'required|array',
'files.*' => 'required|file|mimes:pdf,docx,xlsx,pptx,zip,doc,xls,ppt|max:524288',
]);
try {
$results = $this->uploadContentBuilderFilesAction->run($request->file('files'));
return response()->json([
'success' => true,
'files' => $results,
]);
} catch (\Exception $e) {
return response()->json([
'success' => false,
'error' => 'Ошибка при загрузке файлов: ' . $e->getMessage(),
], 500);
}
}
} }
@@ -364,6 +364,7 @@ Route::middleware(['access-check', 'dashboard.auth'])->group(function () {
Route::get('/', [PageController::class, 'index'])->name('index'); Route::get('/', [PageController::class, 'index'])->name('index');
Route::get('/create', [PageController::class, 'create'])->name('create'); Route::get('/create', [PageController::class, 'create'])->name('create');
Route::post('/', [PageController::class, 'store'])->name('store'); Route::post('/', [PageController::class, 'store'])->name('store');
Route::post('/upload-files', [PageController::class, 'uploadFiles'])->name('upload-files');
Route::get('/{page}/edit', [PageController::class, 'edit'])->name('edit'); Route::get('/{page}/edit', [PageController::class, 'edit'])->name('edit');
Route::put('/{page}', [PageController::class, 'update'])->name('update'); Route::put('/{page}', [PageController::class, 'update'])->name('update');
Route::delete('/{page}', [PageController::class, 'destroy'])->name('destroy'); Route::delete('/{page}', [PageController::class, 'destroy'])->name('destroy');
@@ -23,7 +23,7 @@
export default { export default {
name: 'ContactBlock', name: 'ContactBlock',
props: { modelValue: { type: Object, required: true } }, props: { modelValue: { type: Object, required: true } },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() { return { contacts: [] }; }, data() { return { contacts: [] }; },
async mounted() { async mounted() {
try { try {
@@ -34,6 +34,7 @@ export default {
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
} }
} }
} }
@@ -35,7 +35,7 @@
export default { export default {
name: 'CustomFormBlock', name: 'CustomFormBlock',
props: { modelValue: { type: Object, required: true } }, props: { modelValue: { type: Object, required: true } },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() { return { forms: [] }; }, data() { return { forms: [] }; },
async mounted() { async mounted() {
try { try {
@@ -46,6 +46,7 @@ export default {
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
}, },
updateSettings(field, value) { updateSettings(field, value) {
this.update('settings', { ...this.modelValue.settings, [field]: value }); this.update('settings', { ...this.modelValue.settings, [field]: value });
@@ -22,12 +22,22 @@
</div> </div>
</div> </div>
<!-- Upload Progress -->
<div v-if="uploading" class="flex items-center gap-2 p-3 bg-primary/5 border border-primary/20 rounded-lg">
<svg class="animate-spin h-4 w-4 text-primary" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
</svg>
<span class="text-sm text-primary">Загрузка файлов...</span>
</div>
<input <input
type="file" type="file"
@change="handleFilesUpload" @change="handleFilesUpload"
:disabled="uploading"
accept=".pdf,.docx,.xlsx,.pptx,.zip" accept=".pdf,.docx,.xlsx,.pptx,.zip"
multiple multiple
class="w-full px-3 py-2 border border-layer-line rounded-lg bg-white text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" class="w-full px-3 py-2 border border-layer-line rounded-lg bg-white text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent disabled:opacity-50 disabled:cursor-not-allowed"
/> />
<p class="text-xs text-muted-foreground-1"> <p class="text-xs text-muted-foreground-1">
PDF, DOCX, XLSX, PPTX, ZIP PDF, DOCX, XLSX, PPTX, ZIP
@@ -41,21 +51,66 @@ export default {
props: { props: {
modelValue: { type: Object, required: true } modelValue: { type: Object, required: true }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() {
return {
uploading: false
};
},
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
}, },
removeFile(index) { removeFile(index) {
this.update('path', this.modelValue.path.filter((_, i) => i !== index)); this.update('path', this.modelValue.path.filter((_, i) => i !== index));
}, },
handleFilesUpload(event) { async handleFilesUpload(event) {
const files = Array.from(event.target.files); const files = Array.from(event.target.files);
const newPaths = [...this.modelValue.path]; if (files.length === 0) return;
files.forEach(file => {
newPaths.push(URL.createObjectURL(file)); // Reset input to allow selecting same files again
}); event.target.value = '';
this.update('path', newPaths);
await this.uploadFilesToServer(files);
},
async uploadFilesToServer(files) {
if (files.length === 0) return;
this.uploading = true;
try {
const formData = new FormData();
files.forEach(file => {
formData.append('files[]', file);
});
const response = await fetch(route('dashboard.pages.upload-files'), {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content ?? '',
'Accept': 'application/json',
},
body: formData,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
if (result.success && result.files) {
const newPaths = result.files.map(file => file.path);
this.update('path', [...this.modelValue.path, ...newPaths]);
} else {
console.error('File upload failed:', result.error);
}
} catch (error) {
console.error('File upload error:', error);
} finally {
this.uploading = false;
}
} }
} }
} }
@@ -33,12 +33,22 @@
</div> </div>
</div> </div>
<!-- Upload Progress -->
<div v-if="uploading" class="flex items-center gap-2 p-3 bg-primary/5 border border-primary/20 rounded-lg">
<svg class="animate-spin h-4 w-4 text-primary" fill="none" viewBox="0 0 24 24">
<circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4" />
<path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
</svg>
<span class="text-sm text-primary">Загрузка файлов...</span>
</div>
<input <input
type="file" type="file"
@change="handleFilesUpload" @change="handleFilesUpload"
:disabled="uploading"
accept=".pdf,.docx,.xlsx,.pptx,.zip" accept=".pdf,.docx,.xlsx,.pptx,.zip"
multiple multiple
class="w-full px-3 py-2 border border-layer-line rounded-lg bg-white text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent" class="w-full px-3 py-2 border border-layer-line rounded-lg bg-white text-foreground focus:outline-none focus:ring-2 focus:ring-primary focus:border-transparent disabled:opacity-50 disabled:cursor-not-allowed"
/> />
<p class="text-xs text-muted-foreground-1"> <p class="text-xs text-muted-foreground-1">
PDF, DOCX, XLSX, PPTX, ZIP. Макс. 512MB PDF, DOCX, XLSX, PPTX, ZIP. Макс. 512MB
@@ -60,10 +70,16 @@ export default {
props: { props: {
modelValue: { type: Object, required: true } modelValue: { type: Object, required: true }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() {
return {
uploading: false
};
},
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
}, },
updateFile(index, field, value) { updateFile(index, field, value) {
const files = [...this.modelValue.file]; const files = [...this.modelValue.file];
@@ -71,24 +87,64 @@ export default {
this.update('file', files); this.update('file', files);
}, },
addFile() { addFile() {
this.update('file', [...this.modelValue.file, { title: '', path: '', expansion: '', size: '', time_added: Date.now() }]); this.update('file', [...this.modelValue.file, { title: '', path: '', expansion: '', size: '', time_added: Math.floor(Date.now() / 1000) }]);
}, },
removeFile(index) { removeFile(index) {
this.update('file', this.modelValue.file.filter((_, i) => i !== index)); this.update('file', this.modelValue.file.filter((_, i) => i !== index));
}, },
handleFilesUpload(event) { async handleFilesUpload(event) {
const files = Array.from(event.target.files); const files = Array.from(event.target.files);
files.forEach(file => { if (files.length === 0) return;
const extension = file.name.split('.').pop().toLowerCase();
const size = (file.size / (1024 * 1024)).toFixed(2) + ' MB'; // Reset input to allow selecting same files again
this.update('file', [...this.modelValue.file, { event.target.value = '';
title: file.name,
path: URL.createObjectURL(file), await this.uploadFilesToServer(files);
expansion: extension, },
size: size, async uploadFilesToServer(files) {
time_added: Date.now() if (files.length === 0) return;
}]);
}); this.uploading = true;
try {
const formData = new FormData();
files.forEach(file => {
formData.append('files[]', file);
});
const response = await fetch(route('dashboard.pages.upload-files'), {
method: 'POST',
headers: {
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]')?.content ?? '',
'Accept': 'application/json',
},
body: formData,
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
if (result.success && result.files) {
const newFiles = result.files.map(file => ({
title: file.title,
path: file.path,
expansion: file.expansion,
size: file.size,
time_added: Math.floor(Date.now() / 1000)
}));
this.update('file', [...this.modelValue.file, ...newFiles]);
} else {
console.error('File upload failed:', result.error);
}
} catch (error) {
console.error('File upload error:', error);
} finally {
this.uploading = false;
}
} }
} }
} }
@@ -41,13 +41,14 @@ export default {
required: true required: true
} }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { this.$emit('update:modelValue', {
...this.modelValue, ...this.modelValue,
[field]: value [field]: value
}); });
this.$emit('update');
} }
} }
} }
@@ -23,7 +23,7 @@
export default { export default {
name: 'PageItemBlock', name: 'PageItemBlock',
props: { modelValue: { type: Object, required: true } }, props: { modelValue: { type: Object, required: true } },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() { return { pages: [] }; }, data() { return { pages: [] }; },
async mounted() { async mounted() {
try { try {
@@ -34,6 +34,7 @@ export default {
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
} }
} }
} }
@@ -23,7 +23,7 @@
export default { export default {
name: 'PageResourceListBlock', name: 'PageResourceListBlock',
props: { modelValue: { type: Object, required: true } }, props: { modelValue: { type: Object, required: true } },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() { return { resources: [] }; }, data() { return { resources: [] }; },
async mounted() { async mounted() {
try { try {
@@ -34,6 +34,7 @@ export default {
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
} }
} }
} }
@@ -88,10 +88,11 @@ export default {
props: { props: {
modelValue: { type: Object, required: true } modelValue: { type: Object, required: true }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
}, },
updateInfo(index, field, value) { updateInfo(index, field, value) {
const info = [...this.modelValue.info]; const info = [...this.modelValue.info];
@@ -23,7 +23,7 @@
export default { export default {
name: 'PostItemBlock', name: 'PostItemBlock',
props: { modelValue: { type: Object, required: true } }, props: { modelValue: { type: Object, required: true } },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() { return { posts: [] }; }, data() { return { posts: [] }; },
async mounted() { async mounted() {
try { try {
@@ -34,6 +34,7 @@ export default {
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
} }
} }
} }
@@ -37,7 +37,7 @@
export default { export default {
name: 'PostListBlock', name: 'PostListBlock',
props: { modelValue: { type: Object, required: true } }, props: { modelValue: { type: Object, required: true } },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() { return { categories: [] }; }, data() { return { categories: [] }; },
async mounted() { async mounted() {
try { try {
@@ -48,6 +48,7 @@ export default {
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
} }
} }
} }
@@ -28,7 +28,7 @@ export default {
props: { props: {
modelValue: { type: Object, required: true } modelValue: { type: Object, required: true }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
data() { data() {
return { sliders: [] }; return { sliders: [] };
}, },
@@ -44,6 +44,7 @@ export default {
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
} }
} }
} }
@@ -79,10 +79,11 @@ export default {
props: { props: {
modelValue: { type: Object, required: true } modelValue: { type: Object, required: true }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
}, },
updateStep(index, field, value) { updateStep(index, field, value) {
const steps = [...this.modelValue.steps]; const steps = [...this.modelValue.steps];
@@ -41,10 +41,11 @@ export default {
props: { props: {
modelValue: { type: Object, required: true } modelValue: { type: Object, required: true }
}, },
emits: ['update:modelValue'], emits: ['update:modelValue', 'update'],
methods: { methods: {
update(field, value) { update(field, value) {
this.$emit('update:modelValue', { ...this.modelValue, [field]: value }); this.$emit('update:modelValue', { ...this.modelValue, [field]: value });
this.$emit('update');
}, },
handleFileUpload(event) { handleFileUpload(event) {
const file = event.target.files[0]; const file = event.target.files[0];