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

Revert changes of #1441 #1444

Merged
merged 5 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions docs/storage/flysystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,38 @@ vich_uploader:
upload_destination: default.storage # Use the name you defined for your storage here
```

### Issues with adapters public url

If you are using certain adapters like S3, Cloudflare, etc., the generated public URL for assets may be incorrect.
To resolve this, you can create your own
[custom storage](https://github.com/dustin10/VichUploaderBundle/blob/master/docs/storage/custom.md)
by duplicating the existing FlysystemStorage and adding this function:

```php
public function resolveUri(object|array $obj, ?string $fieldName = null, ?string $className = null): ?string
{
$path = $this->resolvePath($obj, $fieldName, $className, true);

if (empty($path)) {
return null;
}

$mapping = null === $fieldName ?
$this->factory->fromFirstField($obj, $className) :
$this->factory->fromField($obj, $fieldName, $className);
$fs = $this->getFilesystem($mapping);

try {
return $fs->publicUrl($path);
} catch (FilesystemException) {
return $mapping->getUriPrefix().'/'.$path;
}
}
```

Be careful with that; if you have existing implementations,
it can break them. See [there](https://github.com/dustin10/VichUploaderBundle/pull/1441).

## Integrating with [oneup/flysystem-bundle](https://github.com/1up-lab/OneupFlysystemBundle)

To install the bundle, run the following command:
Expand Down
20 changes: 0 additions & 20 deletions src/Storage/FlysystemStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,24 +104,4 @@ protected function getFilesystem(PropertyMapping $mapping): FilesystemOperator

return $this->registry->get($mapping->getUploadDestination());
}

public function resolveUri(object|array $obj, ?string $fieldName = null, ?string $className = null): ?string
{
$path = $this->resolvePath($obj, $fieldName, $className, true);

if (empty($path)) {
return null;
}

$mapping = null === $fieldName ?
$this->factory->fromFirstField($obj, $className) :
$this->factory->fromField($obj, $fieldName, $className);
$fs = $this->getFilesystem($mapping);

try {
return $fs->publicUrl($path);
} catch (FilesystemException) {
return $mapping->getUriPrefix().'/'.$path;
}
}
}
23 changes: 0 additions & 23 deletions tests/Storage/Flysystem/AbstractFlysystemStorageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,27 +158,4 @@ public static function pathProvider(): array
['foo', '/absolute/foo/file.txt', false],
];
}

public function testResolveUri(): void
{
$this->mapping
->expects(self::once())
->method('getUriPrefix')
->willReturn('/uploads');

$this->mapping
->expects(self::once())
->method('getFileName')
->willReturn('file.txt');

$this->factory
->expects(self::once())
->method('fromField')
->with($this->object, 'file_field')
->willReturn($this->mapping);

$path = $this->getStorage()->resolveUri($this->object, 'file_field');

self::assertEquals('/uploads/file.txt', $path);
}
}
Loading