-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from bolt/feature/file-attachments
[WIP] Init file attachments
- Loading branch information
Showing
9 changed files
with
223 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
services: | ||
Bolt\BoltForms\EventSubscriber\FileUploadHandler: | ||
arguments: | ||
$projectDir: '%kernel.project_dir%' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bolt\BoltForms\EventSubscriber; | ||
|
||
use Bolt\BoltForms\Event\PostSubmitEvent; | ||
use Bolt\BoltForms\FormHelper; | ||
use Bolt\Common\Str; | ||
use Cocur\Slugify\Slugify; | ||
use Sirius\Upload\Handler; | ||
use Sirius\Upload\Result\File; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\Form\Form; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Tightenco\Collect\Support\Collection; | ||
use Webmozart\PathUtil\Path; | ||
|
||
class FileUploadHandler implements EventSubscriberInterface | ||
{ | ||
/** @var string */ | ||
private $projectDir; | ||
|
||
/** @var FormHelper */ | ||
private $helper; | ||
|
||
public function __construct(string $projectDir = '', FormHelper $helper) | ||
{ | ||
$this->helper = $helper; | ||
$this->projectDir = $projectDir; | ||
} | ||
|
||
public function handleEvent(PostSubmitEvent $event): void | ||
{ | ||
$form = $event->getForm(); | ||
$formConfig = $event->getFormConfig(); | ||
|
||
$fields = $form->all(); | ||
foreach ($fields as $field) { | ||
$fieldConfig = $formConfig->get('fields')[$field->getName()] ?? null; | ||
if ($fieldConfig && $fieldConfig['type'] === 'file') { | ||
$this->processFileField($field, collect($fieldConfig), $event); | ||
} | ||
} | ||
} | ||
|
||
private function processFileField(Form $field, Collection $fieldConfig, PostSubmitEvent $event): void | ||
{ | ||
$file = $field->getData(); | ||
$form = $event->getForm(); | ||
$formConfig = $event->getFormConfig(); | ||
|
||
$filename = $this->getFilename($field->getName(), $form, $formConfig); | ||
$path = $fieldConfig['directory'] ?? '/uploads/'; | ||
Str::ensureStartsWith($path, DIRECTORY_SEPARATOR); | ||
$files = $this->uploadFiles($filename, $file, $path); | ||
|
||
if (isset($fieldConfig['attach']) && $fieldConfig['attach']) { | ||
$event->addAttachments($files); | ||
} | ||
} | ||
|
||
private function getFilename(string $fieldname, Form $form, Collection $formConfig): string | ||
{ | ||
$filenameFormat = $formConfig->get('fields')[$fieldname]['file_format'] ?? 'Uploaded file'.uniqid(); | ||
$filename = $this->helper->get($filenameFormat, $form); | ||
|
||
if (! $filename) { | ||
$filename = uniqid(); | ||
} | ||
|
||
return $filename; | ||
} | ||
|
||
/** | ||
* @param UploadedFile|array $file | ||
*/ | ||
private function uploadFiles(string $filename, $file, string $path = ''): array | ||
{ | ||
$uploadPath = $this->projectDir.$path; | ||
$uploadHandler = new Handler($uploadPath, [ | ||
Handler::OPTION_AUTOCONFIRM => true, | ||
Handler::OPTION_OVERWRITE => false, | ||
]); | ||
|
||
$uploadHandler->setPrefix(mb_substr(md5(time()), 0, 8) . '_' . $filename); | ||
|
||
$uploadHandler->setSanitizerCallback(function ($name) { | ||
return $this->sanitiseFilename($name); | ||
}); | ||
|
||
/** @var File $processed */ | ||
$processed = $uploadHandler->process($file); | ||
|
||
$result = []; | ||
if ($processed->isValid()) { | ||
$processed->confirm(); | ||
|
||
if (is_iterable($processed)) { | ||
foreach ($processed as $file) { | ||
$result[] = $uploadPath . $file->__get('name'); | ||
} | ||
} else { | ||
$result[] = $uploadPath . $processed->__get('name'); | ||
} | ||
} | ||
|
||
// Very ugly. But it works when later someone uses Request::createFromGlobals(); | ||
$_FILES = []; | ||
|
||
return $result; | ||
} | ||
|
||
private function sanitiseFilename(string $filename): string | ||
{ | ||
$extensionSlug = new Slugify(['regexp' => '/([^a-z0-9]|-)+/']); | ||
$filenameSlug = new Slugify(['lowercase' => false]); | ||
|
||
$extension = $extensionSlug->slugify(Path::getExtension($filename)); | ||
$filename = $filenameSlug->slugify(Path::getFilenameWithoutExtension($filename)); | ||
|
||
return $filename . '.' . $extension; | ||
} | ||
|
||
public static function getSubscribedEvents() | ||
{ | ||
return [ | ||
'boltforms.post_submit' => ['handleEvent', 40], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bolt\BoltForms; | ||
|
||
use Symfony\Component\Form\Form; | ||
|
||
class FormHelper | ||
{ | ||
public function get(?string $format, Form $form, $values = []): ?string | ||
{ | ||
if (! $format || ! $form->isSubmitted()) { | ||
return null; | ||
} | ||
|
||
return preg_replace_callback( | ||
'/{([\w]+)}/i', | ||
function ($match) use ($form, $values) { | ||
if (array_key_exists($match[1], $form->all())) { | ||
return (string) $form->get($match[1])->getData(); | ||
} | ||
|
||
if (array_key_exists($match[1], $values)) { | ||
return (string) $values[$match[1]]; | ||
} | ||
|
||
return '(unknown)'; | ||
}, | ||
$format | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters