Skip to content

Commit 8708e73

Browse files
committed
feat: Add file visibility control for uploads; enhance HasFilex trait and Filex service methods
1 parent d1f3d9c commit 8708e73

File tree

5 files changed

+300
-338
lines changed

5 files changed

+300
-338
lines changed

README.md

Lines changed: 82 additions & 262 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,19 @@ class UploadController extends Controller
315315
```php
316316
use DevWizard\Filex\Facades\Filex;
317317

318-
// Move files
318+
// Move files with default visibility
319319
$result = Filex::moveFiles($tempPaths, 'uploads', 'public');
320320

321+
// Move files with specific visibility
322+
$result = Filex::moveFiles($tempPaths, 'uploads', 'public', 'private');
323+
324+
// Move files with convenience methods
325+
$result = Filex::moveFilesPublic($tempPaths, 'uploads/public');
326+
$result = Filex::moveFilesPrivate($tempPaths, 'uploads/private');
327+
328+
// Move single file with visibility control
329+
$result = Filex::moveFilePrivate($tempPath, 'uploads/documents');
330+
321331
// Generate unique filename
322332
$uniqueName = Filex::generateFileName('document.pdf');
323333

@@ -328,6 +338,77 @@ $validation = Filex::validateTemp($tempPath, $originalName);
328338
$cleaned = Filex::cleanup();
329339
```
330340

341+
### File Visibility Control
342+
343+
Laravel Filex supports controlling file visibility when moving from temporary to permanent storage:
344+
345+
```php
346+
use DevWizard\Filex\Traits\HasFilex;
347+
348+
class DocumentController extends Controller
349+
{
350+
use HasFilex;
351+
352+
public function storeDocument(Request $request)
353+
{
354+
// Validate file upload
355+
$request->validate([
356+
'document' => ['required', 'string', 'starts_with:temp/'],
357+
'is_private' => ['boolean'],
358+
]);
359+
360+
// Move file with user-controlled visibility
361+
$visibility = $request->boolean('is_private') ? 'private' : 'public';
362+
$documentPath = $this->moveFile($request, 'document', 'documents', null, $visibility);
363+
364+
// Or use convenience methods
365+
if ($request->boolean('is_private')) {
366+
$documentPath = $this->moveFilePrivate($request, 'document', 'documents');
367+
} else {
368+
$documentPath = $this->moveFilePublic($request, 'document', 'documents');
369+
}
370+
371+
// Save to database
372+
$document = Document::create([
373+
'name' => $request->input('name'),
374+
'file_path' => $documentPath,
375+
'is_private' => $request->boolean('is_private'),
376+
]);
377+
378+
return redirect()->back()->with('success', 'Document uploaded successfully!');
379+
}
380+
381+
public function storeMultipleDocuments(Request $request)
382+
{
383+
// Validate multiple file uploads
384+
$request->validate([
385+
'documents' => ['required', 'array'],
386+
'documents.*' => ['required', 'string', 'starts_with:temp/'],
387+
'visibility' => ['required', 'in:public,private'],
388+
]);
389+
390+
// Move files with specified visibility
391+
$documentPaths = $this->moveFiles(
392+
$request,
393+
'documents',
394+
'documents',
395+
null,
396+
$request->input('visibility')
397+
);
398+
399+
// Save all documents
400+
foreach ($documentPaths as $path) {
401+
Document::create([
402+
'file_path' => $path,
403+
'is_private' => $request->input('visibility') === 'private',
404+
]);
405+
}
406+
407+
return redirect()->back()->with('success', 'Documents uploaded successfully!');
408+
}
409+
}
410+
```
411+
331412
## 📝 Validation Rules
332413

333414
Laravel Filex provides custom validation rules for enhanced file validation:
@@ -956,267 +1037,6 @@ App::setLocale('bn');
9561037
{{-- Messages will automatically use the current locale --}}
9571038
```
9581039

959-
## 🚨 Troubleshooting
960-
961-
### Common Issues
962-
963-
1. **Files not uploading**
964-
965-
- Check file size limits in PHP config
966-
- Verify storage permissions
967-
- Check network connectivity
968-
969-
2. **Assets not loading**
970-
971-
- Run `php artisan filex:install --force`
972-
- Clear browser cache
973-
- Check asset paths
974-
975-
3. **Validation errors**
976-
- Verify temp files exist
977-
- Check file permissions
978-
- Review validation rules
979-
980-
### Debug Mode
981-
982-
Enable debug mode for detailed logging:
983-
984-
```blade
985-
<x-filex-uploader name="files" :debug="true" />
986-
```
987-
988-
## 📚 API Reference
989-
990-
### FilexService Methods
991-
992-
```php
993-
// Core file operations
994-
public function generateFileName(string $originalName): string
995-
public function validateTemp(string $tempPath, string $originalName): array
996-
public function validateSecure(string $tempPath, string $originalName): array
997-
public function moveFiles(array $tempPaths, string $targetDirectory, ?string $disk = null): array
998-
public function moveFilesBulk(array $tempPaths, string $targetDirectory, ?string $disk = null): array
999-
public function cleanup(): array
1000-
public function cleanupQuarantine(): array
1001-
1002-
// File information and metadata
1003-
public function getTempMeta(string $tempPath): ?array
1004-
public function markTemp(string $tempPath, array $metadata): bool
1005-
public function deleteTemp(string $tempPath): bool
1006-
public function allowsExtension(string $extension): bool
1007-
public function allowsMimeType(string $mimeType): bool
1008-
public function getFileIcon(string $extension): string
1009-
1010-
// Streaming and performance
1011-
public function storeStream(UploadedFile $file, string $directory, ?string $disk = null, ?string $filename = null): string
1012-
public function storeOptimized(UploadedFile $file, string $directory, ?string $disk = null, ?string $filename = null): string
1013-
public function copyStream($sourceDisk, string $sourcePath, $targetDisk, string $targetPath): bool
1014-
1015-
// Bulk operations
1016-
public function bulkFileOperations(array $operations): array
1017-
public function validateChunkedFile(string $chunkPath, int $chunkSize): array
1018-
1019-
// Cache and performance
1020-
public function logPerformanceMetrics(): array
1021-
public function optimizeCaches(): array
1022-
public function formatSize(int $bytes): string
1023-
1024-
// Utility methods
1025-
public function getDefaultDisk()
1026-
public function getTempDisk()
1027-
public function renderFilexAssetsAndRoutes(): string
1028-
public function renderFilexAssets(): string
1029-
```
1030-
1031-
### HasFilex Trait Methods
1032-
1033-
The simplified trait provides these essential methods:
1034-
1035-
```php
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);
1066-
```
1067-
1068-
### FilexResult Class
1069-
1070-
The `FilexResult` class wraps array results from file operations and provides convenient access methods:
1071-
1072-
```php
1073-
// Single file results
1074-
$result->getPath(): ?string // Get first successful file path
1075-
$result->getSuccessfulPaths(): array // Get all successful file paths
1076-
$result->getFailedPaths(): array // Get all failed file paths
1077-
1078-
// Detailed results
1079-
$result->getResults(): array // Get raw results array
1080-
$result->getSuccessful(): array // Get successful operations
1081-
$result->getFailed(): array // Get failed operations
1082-
$result->getFirstSuccessful(): ?array // Get first successful operation
1083-
$result->getFirstFailed(): ?array // Get first failed operation
1084-
1085-
// Checking results
1086-
$result->hasSuccessful(): bool // Check if any succeeded
1087-
$result->hasFailed(): bool // Check if any failed
1088-
$result->isComplete(): bool // Check if all succeeded
1089-
$result->isEmpty(): bool // Check if no results
1090-
1091-
// Array access and iteration
1092-
$result[0] // Access by index
1093-
count($result) // Countable
1094-
foreach ($result as $item) // Iterable
1095-
```
1096-
1097-
## 🧪 Testing
1098-
1099-
```bash
1100-
composer test
1101-
```
1102-
1103-
## Changelog
1104-
1105-
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
1106-
1107-
## Security Vulnerabilities
1108-
1109-
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
1110-
1111-
## Credits
1112-
1113-
- [IQBAL HASAN](https://github.com/devwizardhq)
1114-
- [All Contributors](../../contributors)
1115-
1116-
## License
1117-
1118-
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
1119-
1120-
## Localization
1121-
1122-
Laravel Filex provides comprehensive localization support with built-in translations for multiple languages.
1123-
1124-
### Publishing Language Files
1125-
1126-
To customize the language files, publish them to your application:
1127-
1128-
```bash
1129-
php artisan vendor:publish --provider="DevWizard\Filex\FilexServiceProvider" --tag="filex-lang"
1130-
```
1131-
1132-
This will publish the language files to `resources/lang/vendor/filex/`.
1133-
1134-
### Supported Languages
1135-
1136-
- 🇺🇸 **English** (en) - Default
1137-
- 🇧🇩 **Bengali** (bn) - Complete
1138-
- 🇪🇸 **Spanish** (es) - Complete
1139-
- 🇫🇷 **French** (fr) - Complete
1140-
- 🇩🇪 **German** (de) - Complete
1141-
- �� **Arabic** (ar) - Complete
1142-
- �� **Chinese** (zh) - Complete
1143-
- 🇷🇺 **Russian** (ru) - Complete
1144-
- 🇮🇳 **Hindi** (hi) - Complete
1145-
- 🇧🇷 **Portuguese** (pt) - Complete
1146-
- 🇯🇵 **Japanese** (ja) - Complete
1147-
- 🇮🇹 **Italian** (it) - Complete
1148-
1149-
### Customizing Messages
1150-
1151-
After publishing, you can customize any message in the language files:
1152-
1153-
```php
1154-
// resources/lang/vendor/filex/en/translations.php - UI and general messages
1155-
return [
1156-
'ui' => [
1157-
'drop_files' => 'Drop your files here or click to browse',
1158-
'file_too_big' => 'File is too large (:filesize MB). Maximum size: :maxFilesize MB.',
1159-
'invalid_file_type' => 'This file type is not allowed.',
1160-
// ... more UI messages
1161-
],
1162-
'errors' => [
1163-
'file_not_found' => 'File not found',
1164-
'validation_failed' => 'File validation failed',
1165-
// ... more error messages
1166-
],
1167-
];
1168-
1169-
// resources/lang/vendor/filex/en/validation.php - Validation rule messages
1170-
return [
1171-
'filex_mimes' => 'The :attribute must be a file of type: :values.',
1172-
'filex_max' => 'The :attribute may not be greater than :max kilobytes.',
1173-
'filex_min' => 'The :attribute must be at least :min kilobytes.',
1174-
'filex_image' => 'The :attribute must be an image.',
1175-
// ... more validation messages
1176-
];
1177-
```
1178-
1179-
### Creating New Language Files
1180-
1181-
To add support for a new language:
1182-
1183-
1. Create a new language directory: `resources/lang/vendor/filex/[locale]/`
1184-
2. Copy the English language files:
1185-
```bash
1186-
cp resources/lang/vendor/filex/en/translations.php resources/lang/vendor/filex/[locale]/translations.php
1187-
cp resources/lang/vendor/filex/en/validation.php resources/lang/vendor/filex/[locale]/validation.php
1188-
```
1189-
3. Translate all the messages in both files
1190-
4. Set your application locale in `config/app.php` or dynamically
1191-
1192-
### Language Keys Reference
1193-
1194-
The language files contain the following key groups:
1195-
1196-
- **`translations.php`** - UI messages, upload error messages, help text, and general error messages
1197-
- **`validation.php`** - Validation rule messages for all Filex validation rules (filex_mimes, filex_max, etc.)
1198-
1199-
### Dynamic Language Switching
1200-
1201-
You can switch languages dynamically in your application:
1202-
1203-
```php
1204-
// In your controller or middleware
1205-
App::setLocale('bn'); // Switch to Bengali
1206-
1207-
// Or use helper
1208-
app()->setLocale('bn');
1209-
```
1210-
1211-
### Usage with Blade Components
1212-
1213-
The file upload component automatically uses the correct language based on your application's locale:
1214-
1215-
```blade
1216-
{{-- Messages will be displayed in the current locale --}}
1217-
<x-filex-uploader name="files" />
1218-
```
1219-
12201040
### Contributing Translations
12211041

12221042
We welcome contributions for new languages! To contribute:

config/filex.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@
1717
],
1818
'max_file_size' => env('FILEX_MAX_SIZE', 10),
1919
'temp_expiry_hours' => env('FILEX_TEMP_EXPIRY', 24),
20+
21+
/*
22+
|--------------------------------------------------------------------------
23+
| File Visibility Settings
24+
|--------------------------------------------------------------------------
25+
|
26+
| Configure the default visibility for uploaded files.
27+
| Supported values: 'public', 'private'
28+
|
29+
*/
30+
'visibility' => [
31+
'default' => env('FILEX_DEFAULT_VISIBILITY', 'public'),
32+
],
2033
],
2134

2235
/*

0 commit comments

Comments
 (0)