F
Filament15mo ago
ryfy

FileUpload disk and storeFileNamesIn settings not working

Hey there. I'm using the following field configuration:
Forms\Components\FileUpload::make('documents')
->disk('private')
->multiple(true)
->storeFileNamesIn('document_filenames')
->acceptedFileTypes(['application/pdf'])
->minFiles(1)
->maxSize(12288)
Forms\Components\FileUpload::make('documents')
->disk('private')
->multiple(true)
->storeFileNamesIn('document_filenames')
->acceptedFileTypes(['application/pdf'])
->minFiles(1)
->maxSize(12288)
In my config/filesystems.php I have this configuration:
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'visibility' => 'private',
'throw' => false,
],
'private' => [
'driver' => 'local',
'root' => storage_path('app/private'),
'visibility' => 'private',
'throw' => false,
],
The problems are these: 1. The file does not get saved to the private directory, it gets saved to the app directory as if the 'local' disk was chosen 2. The storeFileNamesIn value never works, there is no document_filenames in the data values. Any ideas?
7 Replies
toeknee
toeknee15mo ago
1. Did you clear your cache? 2. Does the model allow fillable on odcument_filenames
ryfy
ryfyOP15mo ago
1. Yes 2. I don't have the field or form hooked to a model, I'm manually handling the save. Maybe that's where the disconnect is? I see the livewire temporary file upload but Filament doesn't appear to be doing anything with it.
"documents" => array:1 [▼
"ce0c4713-fa5c-4d32-82fd-bf8d39ed29f4" => Livewire\Features\SupportFileUploads\TemporaryUploadedFile {#2142 ▼
-test: false
-originalName: "ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
-mimeType: "application/octet-stream"
-error: 0
#hashName: "nbFyGFd5FluTUigvdjPRTycrb4Cc2MKvcInkOhCy"
#disk: "local"
#storage: Illuminate\Filesystem\FilesystemAdapter {#2011 ▶}
#path: "livewire-tmp/ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
path: "/var/www/html/storage/app/livewire-tmp"
filename: "ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
basename: "phpqw4NxX"
pathname: "/tmp/phpqw4NxX"
extension: ""
realPath: "/var/www/html/storage/app/livewire-tmp/ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
size: 63865
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
]
"documents" => array:1 [▼
"ce0c4713-fa5c-4d32-82fd-bf8d39ed29f4" => Livewire\Features\SupportFileUploads\TemporaryUploadedFile {#2142 ▼
-test: false
-originalName: "ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
-mimeType: "application/octet-stream"
-error: 0
#hashName: "nbFyGFd5FluTUigvdjPRTycrb4Cc2MKvcInkOhCy"
#disk: "local"
#storage: Illuminate\Filesystem\FilesystemAdapter {#2011 ▶}
#path: "livewire-tmp/ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
path: "/var/www/html/storage/app/livewire-tmp"
filename: "ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
basename: "phpqw4NxX"
pathname: "/tmp/phpqw4NxX"
extension: ""
realPath: "/var/www/html/storage/app/livewire-tmp/ysCY1G8lXII5SBqQL0t4PAyc0cO2XF-metaU3RlYW1QdXJjaGFzZV8yMDIxLTA2LTI5LnBkZg==-.pdf"
size: 63865
writable: false
readable: false
executable: false
file: false
dir: false
link: false
}
]
toeknee
toeknee15mo ago
Can you provide the code you are using for the save/
ryfy
ryfyOP15mo ago
Also worth noting, I'm only using the form builder, not the filament admin. Currently the save code just uses the Livewire file upload process and configuration since that seems to be what the form builder is delivering to me. This is a small part of a much larger process involving multiple models, hence the manual save method. Here's the relevant bit of the save code:
if(isset($this->data['documents']) && !empty($this->data['documents'])) {

$user_id = $user->id;
$job_id = $job->id;
$docs = [];

foreach($this->data['documents'] as $key => $document) {
$dir = $entity->id . '/' . date('Y-m-d');
$name = $document->getClientOriginalName();
$mime = $document->getMimeType();
$meta = json_encode(['test' => true]);
try {
$path = $document->store($dir, 'private');
} catch(\Exception $e) {
DB::rollBack();
\Log::error($e->getMessage());
return;
}
$docs[] = compact('user_id', 'job_id', 'path', 'name', 'mime', 'meta');
}

try {
Document::insert($docs);
} catch(\Exception $e) {
DB::rollBack();
\Log::error($e->getMessage());
}



}
if(isset($this->data['documents']) && !empty($this->data['documents'])) {

$user_id = $user->id;
$job_id = $job->id;
$docs = [];

foreach($this->data['documents'] as $key => $document) {
$dir = $entity->id . '/' . date('Y-m-d');
$name = $document->getClientOriginalName();
$mime = $document->getMimeType();
$meta = json_encode(['test' => true]);
try {
$path = $document->store($dir, 'private');
} catch(\Exception $e) {
DB::rollBack();
\Log::error($e->getMessage());
return;
}
$docs[] = compact('user_id', 'job_id', 'path', 'name', 'mime', 'meta');
}

try {
Document::insert($docs);
} catch(\Exception $e) {
DB::rollBack();
\Log::error($e->getMessage());
}



}
This works but I would expect there to be some sort of method I could call that would handle the file upload "the Filament way" that makes uses of the configuration I set on the component. The docs for using the Form Builder in a custom application make no note of how to handle the save without a model, though it seems implied that setting a model is optional.
Matthew
Matthew15mo ago
1) 'document_filenames' needs to be casted as an array in the Model file 2) Have you linked the storage link for private? php artisan storage:link Lemme know if this helps Aso you need to add the LINK in the config/filesystems.php | -> Since you are creating a custom disk. at the bottom of the file, add this in the links
'links' => [
// ... (Other paths)
public_path('private_storage') => storage_path('app/private'),
],
'links' => [
// ... (Other paths)
public_path('private_storage') => storage_path('app/private'),
],
ryfy
ryfyOP15mo ago
Thanks, please see above. I'm not linking a model, I'm handling the save manually. I suppose my question can be boiled down to this: If I am saving manually, do I have to use the Livewire TemporaryUploadedFile ->store() method or is there a Filament way that makes use of the disk and visibility configuration I passed to the component?
cheesegrits
cheesegrits15mo ago
Are you calling getState() at the start of your form submission method?
Want results from more Discord servers?
Add your server