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

UploadedFile infinite loop #29360

Closed
BonBonSlick opened this Issue Nov 28, 2018 · 2 comments

Comments

Projects
None yet
4 participants
@BonBonSlick
Copy link

BonBonSlick commented Nov 28, 2018

dump($newFileName); // "c86cbb038454.jpeg"
dump( $destination.$fileExtension); // "/srv/site/src/../public/media/c86cbb038454.jpeg"
dump( new UploadedFile($destination.$fileExtension, $newFileName)); // if this line called, got 502 error

When creating UploadedFile from some existing on server file script has error 504 Gateway Time-out. Nginx and Symfony logs are ok, no exceptions or errors. Script just stops on line where new UploadedFile instance initialized.
Tried to catch exceptions, no exceptions.
Manual creation like this always work
new UploadedFile('/srv/site/src/../public/media/users/04cc16a82d7d.jpeg', '04cc16a82d7d.jpeg');

This is method to parse and save image from facebook, next steps are create UserAvatar image and assign relations.

More code


    public function handleFileUpload(User $user, string $url): User
    {
        $fileName    = \bin2hex(\openssl_random_pseudo_bytes(6));
        $destination = $this->uploadDirectory.'/'.$fileName;
        $isFileSaved = \file_put_contents($destination, \fopen($url, 'rb'));
        if (false === $isFileSaved) {
            $this->logger->error(\sprintf('Failed to save profile image. User: %d URL: %s', $user->getId(), $url));

            return $user;
        }

        // Get the file extension
        try {
        $fileExtension = $this->imageManager->getExtensionNameByPath($destination);

        // replace old extension if exists and add new one
        $newFileName = preg_replace('/(\.png|\.jpg|\.jpeg)$/i', '$1$2', $fileName).$fileExtension;

        // Rename file with new extension name
        $isFileRenamed = \rename($this->uploadDirectory.'/'.$fileName, $this->uploadDirectory.'/'.$newFileName);
        if (false === $isFileRenamed) {
            $this->logger->error(sprintf('Failed to rename profile image. User: %d URL: %s', $user->getId(), $url));

            return $user;
        }
// ERROR NEXT LINE, file exists, path and name correct. 
        $imageFile = new UploadedFile($destination.$fileExtension, $newFileName);
        $image = new UserProfileImage($imageFile, $user);
        $image->setOwner($user);
        $user->addImage($image);
 $this->entityManager->persist($image);
        } catch (NotAllowedMimeTypeException $exception) {
            $this->logger->info($exception);
        }

        return $user;

This is occur not always only sometimes in runtime, when image uploading from facebook. Tell me please what may cause such behaviour?

@BonBonSlick BonBonSlick changed the title UploadFile intinite loop UploadedFile infinite loop Nov 28, 2018

@webnet-fr

This comment has been minimized.

Copy link
Contributor

webnet-fr commented Nov 30, 2018

Hi, @BonBonSlick.
Which version of HttpFoundaiton do you use ?
What file you're trying to download ?
It would be nice if you provide a more explicit examle like:

$destination = __DIR__.'/random.jpg';
\file_put_contents($destination, \fopen('https://pp.userapi.com/c850616/v850616593/56299/KaaA_5RV8Qw.jpg', 'rb'));
new UploadedFile($destination, 'dog.jpeg'); // works perfectly

In fact there is no loop in the constructor of UploadedFile. On the other hand check if you can do the following with your file:

  • filesize($destination)
  • is_file($destination)
  • new \SplFileInfo($destination)

Cheers

@BonBonSlick

This comment has been minimized.

Copy link
Author

BonBonSlick commented Dec 1, 2018

Strange thing, for other devs and production code works fine, only I have bug on my local machine sometimes. 3 methods above worked well.
Issue can be closed, because something related to configs, VM or something else related to my environment. It is not bug of symfony, because it would fail also for other devs.
I did not figure out what exactly is going on, XDebug does not help, nginx, symfony logs are ok, script just stops sometimes.
Thank you for your time.

@xabbuh xabbuh closed this Dec 1, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.