Skip to content

Commit

Permalink
FilePond now uses storeAsFile: true
Browse files Browse the repository at this point in the history
  • Loading branch information
priyadi committed Oct 10, 2023
1 parent 8825627 commit cb895c5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
3 changes: 1 addition & 2 deletions packages/file-filepond/assets/src/filepond.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,9 @@ export default class extends Controller {
}

FilePond.create(input, {
storeAsFile: true,
files: files,
credits: false
})

}
}

33 changes: 24 additions & 9 deletions packages/file-filepond/src/FilePondType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
namespace Rekalogika\File\Bridge\FilePond;

use Rekalogika\Contracts\File\FileInterface;
use Rekalogika\File\Bridge\Symfony\HttpFoundation\FromHttpFoundationFileAdapter;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
Expand All @@ -35,23 +37,33 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

$builder
->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) use ($options) {
/** @var null|string */
/** @var null|string|UploadedFile|FileInterface */
$data = $event->getData();
if ($data instanceof UploadedFile) {
$data = FromHttpFoundationFileAdapter::adapt($data);
}

// if the client sent NOT_DELETED_SENTINEL, it means the user
// didn't remove the preview image from the filepond field. so,
// we set the data from the existing value.

if ($data == self::NOT_DELETED_SENTINEL) { // the image is not deleted by the user
if (
(is_string($data) && $data == self::NOT_DELETED_SENTINEL)
||
($data instanceof FileInterface
&& $data->getSize() == strlen(self::NOT_DELETED_SENTINEL)
&& $data->getContent() == self::NOT_DELETED_SENTINEL)
) {
$event->setData($event->getForm()->getData());
return;
}

// the client didn't send any data, most probably because they
// removed the preview image from the upload box. if
// 'remove_on_null' is off, we set the data using the existing
// data. if 'remove_on_null' is on, then the data remains null,
// and the existing file (if exists) will be removed by doctrine
// the client did not send any data, because they file did not
// exist in the first place, or because the user has removed the
// preview image from the upload box. if 'remove_on_null' is
// off, we set the data using the existing data. if
// 'remove_on_null' is on, then the data remains null, and the
// existing file (if exists) will be removed by doctrine

if (!$data) {
if (!$options['remove_on_null']) {
Expand All @@ -62,8 +74,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void

// save the file, and add it as our data

$file = FilePondFileEncodeAdapter::adapt($data);
$event->setData($file);
if (is_string($data)) {
$data = FilePondFileEncodeAdapter::adapt($data);
}

$event->setData($data);
});
}

Expand Down

0 comments on commit cb895c5

Please sign in to comment.