1

after searching a lot here and there I am going to put my question here . If any body can help out in this regard . Let me explain the things I am working on Laravel and Filepond . Filepond upload and revert is working perfectly but I am facing problem in restoring the file back if the validation gets failed i-e restore the file on filepond.

                
                 files: [{
                     source: 'filename', 
                     options: {
                       type: 'limbo',
                    },

                }, ],

the source is coming from laravel controller function

FilePond.setOptions({
                server: {

                    process: './filepond-upload',
                    revert: './filepond-delete',
                    restore: './filepond-restore',
                    // restore: {
                    //     url    :'./filepond-restore/?id=',
                    //     method :'GET',

                    // },
                    headers: {
                        'X-CSRF-TOKEN': '{{ csrf_token() }}',
                        'Access-Control-Expose-Headers': 'Content-Disposition,'
                        // 'Access-Control-Expose-Headers': 'Content-Disposition',
                    }

                }

            });

Controller function -

public function filepondRestore(Request $request, string $id) {                
   $abc = ('/posts/tmp/post6399a6ba2ea280.18814893/presentation_1.png');              
   return response()->json('', 200, [                  
      'Content-Type' => 'image/png', 
      'Content-Disposition' => 'inline; 
      filename="'.$abc.'"',              
   ]);              
}

but either get 302 redirection or 500 server error. If any body have implemented such kind of functionality I ll be thankful for sharing. Thanks in advance. Happy Coding

Gonras Karols
  • 1,150
  • 10
  • 30
Gul Muhammad
  • 166
  • 12
  • can you show how you implemented the `/filepond-restore/?id=` function in the controller? – Gonras Karols Dec 16 '22 at 07:57
  • @GonrasKarols I have been doing lot of changes here and there currently I am just returning the response in this way ````public function filepondRestore(Request $request,string $id) { $abc =('/posts/tmp/post6399a6ba2ea280.18814893/presentation_1.png'); return response()->json('', 200, [ 'Content-Type' => 'image/png', 'Content-Disposition' => 'inline; filename="'.$abc.'"', ]); }```` – Gul Muhammad Dec 16 '22 at 09:14
  • Even I went through this its somehow near to my need but https://github.com/Sopamo/laravel-filepond/issues/51 I am not using that package at all I am implementing my own . You can see that there is source chunk id which is hardcoded and how id is passed to route nothing is clear – Gul Muhammad Dec 16 '22 at 09:20

1 Answers1

0

It feels hacky but maybe someone can help us improve.

In the blade

      const pond = FilePond.create(inputElement, {
        acceptedFileTypes: ['video/mp4'],
        @if ($errors->isNotEmpty())
          files: [{
            source: '{{ old('file') }}',
            options: {
              type: 'limbo',
            }
          }],
        @endif
        server: {
          url: '/upload',
          patch: "/",
          headers: {
            'X-CSRF-TOKEN': '{{ csrf_token() }}',
          },
        },
        chunkUploads: true,
        chunkSize: 1048576,
        chunkForce: true,
      });

In the index method of the controller.

    public function index(Request $request)
    {
        $fileName = Storage::disk('local')->files('tmp/' . $request->restore)[0];
        $fileName = explode('/', $fileName);
        $nbArray = count($fileName);
        $fileName = $fileName[$nbArray - 1];
        $filePath = Storage::path(
            Storage::disk('local')->files('tmp/' . $request->restore)[0]
        );


        $shortFilePath = Str::after($filePath, 'storage/app/');
        $fileContents = Storage::disk('local')->get($shortFilePath);

        $response = Response::make($fileContents, 200);


        $response->header('Content-Disposition', 'inline; filename="' . $fileName . '"');
        $response->header('Content-Type', 'video/mp4');

        return $response;
}

I tried with request->load but no success, the filepond square stayed gray after a invalid request. But if I use limbo and request->restore it works.

Let me know if it helps and if you have a better solution.

Selajdin Bilali
  • 131
  • 1
  • 2
  • 9