@@ -206,7 +206,7 @@ This directive automatically includes all required CSS, JavaScript, and route co
206206
207207### Using the HasFilex Trait
208208
209- The ` HasFilex ` trait provides convenient methods for handling file uploads :
209+ The ` HasFilex ` trait provides a clean, simple API for file upload operations :
210210
211211``` php
212212<?php
@@ -222,20 +222,14 @@ class DocumentController extends Controller
222222
223223 public function store(Request $request)
224224 {
225- // Validate the request
225+ // Validate the request with trait helper
226226 $request->validate([
227227 'title' => 'required|string',
228- 'documents' => 'required|array',
229- 'documents.*' => 'string|starts_with:temp/',
228+ ...$this->getFilesValidationRules('documents', true), // Simple validation rules
230229 ]);
231230
232- // Process uploaded files (moves from temp to permanent storage)
233- $filePaths = $this->processFiles(
234- $request,
235- 'documents', // Field name
236- 'documents/user-uploads', // Target directory
237- 'public' // Disk (optional)
238- );
231+ // Upload multiple files - clean and simple
232+ $filePaths = $this->moveFiles($request, 'documents', 'documents/user-uploads');
239233
240234 // Create your model with the processed file paths
241235 Document::create([
@@ -247,46 +241,38 @@ class DocumentController extends Controller
247241 return redirect()->back()->with('success', 'Documents uploaded successfully!');
248242 }
249243
250- public function update (Request $request, Document $document )
244+ public function updateAvatar (Request $request, User $user )
251245 {
252- // Handle bulk file updates (replaces old files with new ones)
253- $newFilePaths = $this->handleBulkUpdate(
254- $request,
255- 'documents',
256- $document->file_paths, // Existing files
257- 'documents/user-uploads',
258- 'public'
259- );
246+ // Validate single file upload
247+ $request->validate([
248+ ...$this->getFileValidationRules('avatar', true),
249+ ]);
250+
251+ // Upload single file
252+ $avatarPath = $this->moveFile($request, 'avatar', 'avatars');
260253
261- $document ->update(['file_paths ' => $newFilePaths ]);
254+ $user ->update(['avatar ' => $avatarPath ]);
262255
263- return redirect()->back()->with('success', 'Documents updated successfully!');
256+ return redirect()->back()->with('success', 'Avatar updated successfully!');
264257 }
265258}
266259```
267260
268261### Available HasFilex Methods
269262
270- #### Core Processing Methods
263+ #### Simple Upload Methods
271264
272- - ` processFiles() ` - Process multiple files from temp to permanent storage with bulk operations
273- - ` processSingleFile() ` - Process a single file with enhanced error handling
274- - ` validateFiles() ` - Validate temporary files with comprehensive security checks
275- - ` cleanupFiles() ` - Clean up temporary files with batch processing
276- - ` handleBulkUpdate() ` - Handle file updates with automatic cleanup of old files
265+ - ` moveFile() ` - Move a single file from request to permanent storage
266+ - ` moveFiles() ` - Move multiple files from request to permanent storage
277267
278- #### Utility Methods
268+ #### Validation Helpers
279269
280- - ` getFilesInfo() ` - Get detailed information about uploaded files including metadata
281- - ` getValidationRules() ` - Generate validation rules for form requests dynamically
282- - ` prepareFileData() ` - Prepare file data for database storage with sanitization
283- - ` formatFileSize() ` - Format file sizes in human-readable format
284- - ` getUploadStats() ` - Get upload statistics and analytics for monitoring
285- - ` moveFilesWithProgress() ` - Move files with progress tracking callbacks
286- - ` validateFilesBatch() ` - Batch validate multiple files efficiently
287- - ` getDisk() ` - Get storage disk instance with caching
288- - ` getTempDisk() ` - Get temporary storage disk instance
289- - ` cleanupExpired() ` - Clean up expired temporary files
270+ - ` getFileValidationRules() ` - Get validation rules for single file fields
271+ - ` getFilesValidationRules() ` - Get validation rules for multiple file fields
272+
273+ #### Cleanup
274+
275+ - ` cleanupTempFiles() ` - Clean up temporary files if validation fails
290276
291277### Using the Service Directly
292278
@@ -857,21 +843,13 @@ class DocumentController extends Controller
857843 {
858844 $tempPaths = $request->input('documents', []);
859845
860- // Process files with progress tracking
861- $results = $this->moveFilesWithProgress(
862- $tempPaths,
863- 'documents/bulk-upload',
864- 'public',
865- function($completed, $total) {
866- // Progress callback
867- Log::info("Bulk upload progress: {$completed}/{$total}");
868- }
869- );
846+ // Simple bulk upload
847+ $documentPaths = $this->moveFiles($request, 'documents', 'documents/bulk-upload');
870848
871849 return response()->json([
872850 'success' => true,
873- 'processed ' => count($results ),
874- 'files' => $results
851+ 'uploaded ' => count($documentPaths ),
852+ 'files' => $documentPaths
875853 ]);
876854 }
877855}
@@ -1052,28 +1030,39 @@ public function renderFilexAssets(): string
10521030
10531031# ## HasFilex Trait Methods
10541032
1033+ The simplified trait provides these essential methods:
1034+
10551035` ` ` php
1056- // File processing with enhanced performance
1057- protected function processFiles(Request $request , string $fieldName , string $targetDirectory , ? string $disk = null, bool $required = false): array
1058- protected function processSingleFile(Request $request , string $fieldName , string $targetDirectory , ? string $disk = null, bool $required = false): ? string
1059- protected function handleBulkUpdate(Request $request , string $fieldName , array $existingFiles , string $targetDirectory , ? string $disk = null): array
1060-
1061- // Advanced validation and utilities
1062- protected function getValidationRules(string $fieldName , bool $required = false, array $additionalRules = []): array
1063- protected function validateFiles(array $tempPaths ): array
1064- protected function validateFilesBatch(array $tempPaths ): array
1065- protected function getFilesInfo(array $tempPaths ): array
1066- protected function cleanupFiles(array $tempPaths ): array
1067-
1068- // Progress tracking and monitoring
1069- protected function moveFilesWithProgress(array $tempPaths , string $targetDirectory , ? string $disk = null, ? callable $progressCallback = null): array
1070- protected function getUploadStats(array $tempPaths ): array
1071- protected function prepareFileData(array $tempPaths , array $metadata = []): array
1072-
1073- // Storage utilities
1074- protected function getDisk(?string $disk = null): \I lluminate\C ontracts\F ilesystem\F ilesystem
1075- protected function getTempDisk(): \I lluminate\C ontracts\F ilesystem\F ilesystem
1076- protected function cleanupExpired(): array
1036+ // Simple upload methods
1037+ protected function moveFile(Request $request , string $fieldName , string $directory , ? string $disk = null): ? string
1038+ protected function moveFiles(Request $request , string $fieldName , string $directory , ? string $disk = null): array
1039+
1040+ // Validation helpers
1041+ protected function getFileValidationRules(string $fieldName , bool $required = false): array
1042+ protected function getFilesValidationRules(string $fieldName , bool $required = false): array
1043+
1044+ // Cleanup utilities
1045+ protected function cleanupTempFiles(array $tempPaths ): int
1046+ ` ` `
1047+
1048+ ** Example Usage:**
1049+
1050+ ` ` ` php
1051+ // Single file upload
1052+ $avatarPath = $this -> moveFile($request , ' avatar' , ' avatars' );
1053+
1054+ // Multiple file upload
1055+ $documentPaths = $this -> moveFiles($request , ' documents' , ' documents/uploads' );
1056+
1057+ // Get validation rules
1058+ $rules = [
1059+ ' title' => ' required|string' ,
1060+ ...$this -> getFilesValidationRules(' documents' , true),
1061+ ];
1062+
1063+ // Cleanup if validation fails
1064+ $tempPaths = $request -> input(' documents' , []);
1065+ $cleaned = $this -> cleanupTempFiles($tempPaths );
10771066` ` `
10781067
10791068# ## FilexResult Class
0 commit comments