open-admin-org/open-admin

Custom save UploadedFile to individual user folder

vincentchoo-intern opened this issue · 2 comments

Is there a way to save each file uploads into individual folders in the storage?
As far as I know, the storage path can be modified using $form->move('your_path').
I want to attempt saving a set of files into a dedicated user folder, when I am creating a new form.
I tried doing it in $form->saving() but I got stuck on how to change the pathname to user id
(e.g. /uploads/{user_id}/surat_sokongan)


$form->file('surat_sokongan', __('Surat sokongan'))
            ->uniqueName()
            ->disabled($disableStatus)
            ->retainable()
            ->removable($removeableStatus);

$form->saving(function ($form) {

            $destination = config('filesystems.disks.admin.url') . $form->input('holder_id') ;
            //output: $destination = "http://127.0.0.1:8000/uploads/2" 


        });

Hi there, you can hook onto the file input via the $form->saving() hook

Or you can create a custom component and register in accordance to docs,
Check out the chained methods again in the docs

It shows you that you can set disk or use "move()"

$form->file()->disk()
$form->file()->move()

Disk will require a defined disk in config

Hi thanks for the reply.
I tried both methods, i dont think it works.
I have a holder_id field in Select input field in the form.
My aim is to save each file dynamically according to the holder_id selected, so i wrote like this but it doesn't work

$form->file('surat_sokongan', __('Surat sokongan'))
        ->uniqueName()
        ->disabled($disableStatus)
        ->retainable()
        ->removable($removeableStatus)
        ->move($form->input('holder_id'));

On the other hand, the saving hook in $form->saving() I tried to code it like this
(I tried editing the ->storeAs method but it became worst the more i change it)


$form->saving(function ($form) {
            $holderId = $form->input('holder_id');
            

            $form->surat_sokongan->storeAs($holderId, null,  []);

            // $form->file('surat_sokongan', __('Surat sokongan'))->move('mondaytest');
           //doesnt work

        });

I find that the "move()" method is the best solution but I can't configure its path dynamically according to "input('holder_id'), it can only be fixed into one directory right?
I also tried to use it in the 'saving()' hook, which also didn't work.
Will try to look into chained methods.

EDIT:


$form->multipleFile('gambar_kenderaan', __('Gambar Kenderaan'))
        ->move(function ($form) {
            $holderId = $form->model->holder_id;
            $path = 'files/'. $holderId . '/gambar_kenderaan/';
            return $path;
        }, 'file')
            ->disabled($disableStatus)
            ->retainable()
            ->removable($removeableStatus);



Ok I found out the solution, this works