Skip to content

Commit

Permalink
Revert changes of #1441 (#1444)
Browse files Browse the repository at this point in the history
  • Loading branch information
sovetski committed Apr 23, 2024
1 parent ec9ad4b commit 0ed4538
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 43 deletions.
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);
}
}

0 comments on commit 0ed4538

Please sign in to comment.