Skip to content

Commit

Permalink
feature #5528 Allow to use stream as upload dir for FileUploadType (e…
Browse files Browse the repository at this point in the history
…mmanuel-tilleuls)

This PR was merged into the 4.x branch.

Discussion
----------

Allow to use stream as upload dir for FileUploadType

As mention in #5136 and #5464, EasyAdmin is tight linked to local filesystem. This is an issue when one wants to use a s3 bucket to store images uploaded through `FileUploadType`.
Instead of override `upload_new` and `upload_delete` closure to move the file to the bucket (tip from SymfonyCast), you can use the stream wrapper of s3 client :
- call `registerStreamWrapper` after instantiate S3 client
- set `ImageField` `uploadDir to `s3://bucket-name/`
- no need to set `upload_new` nor `upload_delete` type options

But doing this isn't enough because EasyAdmin check uploadDir existence prepended with project directory (in order to have an absolute directory name).

I propose with this PR to not prepend project directory when the uploadDir is an url.

Commits
-------

ddbced9 Allow to use stream as upload dir for FileUploadType
  • Loading branch information
javiereguiluz committed Dec 15, 2022
2 parents b985c82 + ddbced9 commit 5d40afe
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/Field/Configurator/ImageConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
throw new \InvalidArgumentException(sprintf('The "%s" image field must define the directory where the images are uploaded using the setUploadDir() method.', $field->getProperty()));
}
$relativeUploadDir = u($relativeUploadDir)->trimStart(\DIRECTORY_SEPARATOR)->ensureEnd(\DIRECTORY_SEPARATOR)->toString();
$absoluteUploadDir = u($relativeUploadDir)->ensureStart($this->projectDir.\DIRECTORY_SEPARATOR)->toString();
if (filter_var($relativeUploadDir, \FILTER_VALIDATE_URL)) {
$absoluteUploadDir = $relativeUploadDir;
} else {
$absoluteUploadDir = u($relativeUploadDir)->ensureStart($this->projectDir.\DIRECTORY_SEPARATOR)->toString();
}
$field->setFormTypeOption('upload_dir', $absoluteUploadDir);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Form/Type/FileUploadType.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public function configureOptions(OptionsResolver $resolver): void
$value .= \DIRECTORY_SEPARATOR;
}

if (!str_starts_with($value, $this->projectDir)) {
if (!filter_var($value, \FILTER_VALIDATE_URL) && !str_starts_with($value, $this->projectDir)) {
$value = $this->projectDir.'/'.$value;
}

Expand Down

0 comments on commit 5d40afe

Please sign in to comment.