Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Patch requests are not sent to the 'process' endpoint #1023

Open
2 tasks done
gotexx1 opened this issue Nov 13, 2024 · 1 comment
Open
2 tasks done

[Bug] Patch requests are not sent to the 'process' endpoint #1023

gotexx1 opened this issue Nov 13, 2024 · 1 comment

Comments

@gotexx1
Copy link

gotexx1 commented Nov 13, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Have you updated FilePond and its plugins?

  • I have updated FilePond and its plugins

Describe the bug

I'd like to set up a chunk file upload. However, Filepond sends PATCH requests to the current page and not to the ‘process’ endpoint.

The first POST request works correctly and I get an Transfer ID from my back-end. However, filepond then sends PATCH requests to the URL on the current page. Shouldn't it send the requests to /process?
Thank you

Reproduction

This is my configuration:

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/filepond/dist/filepond.min.css">
    <link rel="stylesheet"
          href="https://cdn.jsdelivr.net/npm/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.css">
    <script
        src="https://cdn.jsdelivr.net/npm/filepond-plugin-image-preview/dist/filepond-plugin-image-preview.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/filepond/dist/filepond.min.js"></script>
    <script>
        const inputElement = document.querySelector('input[type="file"]');
        FilePond.registerPlugin(FilePondPluginImagePreview);
        const pond = FilePond.create(inputElement, {
            server: {
                process: {
                    url: '/api/medias/manage/process',
                    method: 'POST',
                    ondata: (formData) => {
                        formData.append('share_id', document.querySelector('input[name="share_id"]').value);
                        return formData;
                    }
                },
                revert: '/api/medias/manage/revert/',
                restore: '/api/medias/manage/restore/',
                load: '/api/medias/manage/load/',
                fetch: '/api/medias/manage/fetch/',
            },
            chunkUploads: true,
            chunkForce: true,
            imagePreviewHeight: 250,
            imagePreviewMarkupShow: false,
            credits: false
        });
    </script>
    ```
    
    and my back-end:
```php
    public function process(Request $request, $transferId = null)
    {
        if ($request->isMethod('post')) {
            $request->validate(['share_id' => 'required|exists:shares,id']);

            // Créer un ID unique pour le transfert
            $transferId = uniqid();
            $tempPath = storage_path("app/temp/{$transferId}");

            if (!file_exists($tempPath)) {
                mkdir($tempPath, 0777, true);
            }

            return response($transferId, 200)->header('Content-Type', 'text/plain');
        }

        if ($request->isMethod('patch')) {
            $request->validate([
                'file' => ['required'],
            ]);

            $tempPath = storage_path("app/temp/{$transferId}");

            if (!file_exists($tempPath)) {
                return response('Transfer ID not found', 404);
            }

            $offset = $request->header('Upload-Offset');
            $fileName = $request->header('Upload-Name');
            $chunkPath = "{$tempPath}/chunk_{$offset}";

            file_put_contents($chunkPath, $request->getContent());

            $totalLength = $request->header('Upload-Length');
            $uploadedChunks = glob("{$tempPath}/*.chunk");
            $uploadedSize = array_sum(array_map('filesize', $uploadedChunks));

            if ($uploadedSize >= $totalLength) {
                $finalPath = storage_path("app/uploads/{$fileName}");
                $finalFile = fopen($finalPath, 'w');

                foreach ($uploadedChunks as $chunk) {
                    fwrite($finalFile, file_get_contents($chunk));
                    unlink($chunk);
                }

                fclose($finalFile);
                rmdir($tempPath);

                return response('File uploaded successfully', 200);
            }

            return response('', 204); // Chunk uploaded
        }

        return response('Method not allowed', 405);
    }
    ```



### Environment

```markdown
- Device: Computer
- OS: Fedora 41
- Browser: Firefox 132.0
@gotexx1 gotexx1 added the bug label Nov 13, 2024
@rikschennink
Copy link
Collaborator

You can add server.patch
https://pqina.nl/filepond/docs/api/server/#url

@rikschennink rikschennink removed the bug label Nov 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants