VichUploaderBundle: Symfony FileValidator constraint is not working with flysystem v2

Bug Report

Q A
BC Break no
Bundle version 1.18.0
Symfony version 4.4.26
PHP version 7.4

Summary

Hi @garak, I started to use this bundle with flysystem v2. It works well for uploading and downloading files. But it seems like none of my entity validation are passing now.

The FileValidator of Symfony is checking for is_file($path) and the file is never found. With gaufrette, the file was image

The path is coming from https://github.com/dustin10/VichUploaderBundle/blob/master/src/Storage/GaufretteStorage.php#L77, and in the FileValidator is_path were returning true for this value.

With the flysystem, the file is now image And now the check done by the FileValidator with is_file is returning false. It seems normal because no prefix is added to say to use flysystem and to precise the upload destination.

It doesn’t seems easy to fix this, because the resolvePath is used in multiple places, and I assume for instance that the resolveStream method is working well since I can download files. https://github.com/dustin10/VichUploaderBundle/blob/94ba188f2603fad9bf9d2a88c61444f5e52a11df/src/Storage/FlysystemStorage.php#L79

So it would mean that sometimes, we need the “naked” path and sometimes we would need something more elaborate in order to have is_file returning true for the path. Maybe @frankdejonge could help us on the question “Which path should be use with flysystem in order to have is_file($path) returning true”.

Maybe I’m taking this problem the wrong way, and we should focus about the place VichUploaderBundle is generating the file (which symfony is validating). If I understand correctly, it’s done here: https://github.com/dustin10/VichUploaderBundle/blob/94ba188f2603fad9bf9d2a88c61444f5e52a11df/src/Injector/FileInjector.php#L26-L33 I would say, the file should be generated differently for Flysystem. But if we want to avoid downloading the file everytime we’re loading the entity, just in order to have a correct path, we end with the same question, “Which path should be use with flysystem in order to have is_file($path) returning true”. Don’t know if a new method should be added to the flysystem API or flysystem-bundle API (cc @tgalopin)

@fullbl, you added the support for flysystem 2 in https://github.com/dustin10/VichUploaderBundle/pull/1179, don’t you have the same issue ? Any idea how to fix it ?

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 1
  • Comments: 20 (9 by maintainers)

Commits related to this issue

Most upvoted comments

As discussed with Frank, I’m working on a small lib implementing a stream wrapper for Flysystem. I hope to release a first version soon.

Perhaps an intermediate solution could be to provide the stream wrapper in flysystem-bundle? I guess it would make sense as it’ll mostly used by Symfony devs and I’d be fine maintaining it.

This was broken in #1179.

https://github.com/dustin10/VichUploaderBundle/pull/1179/files#diff-2b8001f2c9bd1b9232145a0f4f8f046185f9449271c43eda05b15b03d369fb12L70

The absolute part has been removed.

This method is used by the FileInjector: https://github.com/dustin10/VichUploaderBundle/blob/master/src/Injector/FileInjector.php#L29

Unless I’m missing something this would be a hard fix, since flysystem doesn’t provide a stream wrapper, unlike gaufrette. Until this is fixed, I would consider the flysystem storage as unsafe to use, since workarounds are needed to adapt to the situation.

You cannot rely on the injected files at all.

@tgalopin if you want to maintain it I can create a separate repository for this so people can depend on the stream wrapper directly. Let’s touch base on the slack 👍

As discussed with Frank, I’m working on a small lib implementing a stream wrapper for Flysystem. I hope to release a first version soon.

With gaufrette being deprecated, having an “official” stream-wrapper for flysystem seems even more useful now.

I started to do so but didn’t manage to finish yet. I’ll push initial code soon so that you can contribute if you wish.

What is the current state/link of the WIP library ?