Skip to content

Commit 5d40afe

Browse files
committed
feature #5528 Allow to use stream as upload dir for FileUploadType (emmanuel-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
2 parents b985c82 + ddbced9 commit 5d40afe

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

src/Field/Configurator/ImageConfigurator.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,11 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
5252
throw new \InvalidArgumentException(sprintf('The "%s" image field must define the directory where the images are uploaded using the setUploadDir() method.', $field->getProperty()));
5353
}
5454
$relativeUploadDir = u($relativeUploadDir)->trimStart(\DIRECTORY_SEPARATOR)->ensureEnd(\DIRECTORY_SEPARATOR)->toString();
55-
$absoluteUploadDir = u($relativeUploadDir)->ensureStart($this->projectDir.\DIRECTORY_SEPARATOR)->toString();
55+
if (filter_var($relativeUploadDir, \FILTER_VALIDATE_URL)) {
56+
$absoluteUploadDir = $relativeUploadDir;
57+
} else {
58+
$absoluteUploadDir = u($relativeUploadDir)->ensureStart($this->projectDir.\DIRECTORY_SEPARATOR)->toString();
59+
}
5660
$field->setFormTypeOption('upload_dir', $absoluteUploadDir);
5761
}
5862

src/Form/Type/FileUploadType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ public function configureOptions(OptionsResolver $resolver): void
139139
$value .= \DIRECTORY_SEPARATOR;
140140
}
141141

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

0 commit comments

Comments
 (0)