phpsymfonyfile-uploadblueimponeupuploaderbundle

Passing file name from UploadNamer to UploadListener in Symfony v4.1 with OneUpUploaderBundle


Introduction

I am using Symfony v4.1.* with OneUpUploaderBundle. I need to set unique name for each uploaded file - that works.

I have tree structure, that contains uploaded files. In order to manage files in the tree i have multiple properties. Some of them are:

  1. itemName (transliterated file name + counter + unique_sting)
  2. itemOriginalName (transliterated file name + counter)
  3. itemExtension (file extension - for example .txt or .a if directory)
  4. itemSortValue (file extension + transliterated file name + counter + unique_sting)

ItemSortValue assures that tree branches (fields) are sorted alphabetically.

Problem

I need also to persist information about uploaded file in database. For that purpose i need to get each complete file name that is managed by UploadNamer in my UploadListener in onPostUpload event.

path/to/file/my_file_name(counter)_{time}_{uniqueString}.extension

I need to save same unique file name to the database. Yet the problem that i am facing is - i do not know and can not find just how to pass information from UploadNamer to uploadListener.

I tried to use $this->originalName but it did not yield positive results.

Question

How do i pass a value from UploadNamer to UploadListener (onPostUpload event) in Symfony v4.1 with OneUpUploaderBundle?

CODE:

custom namer

public function name(FileInterface $file)
{
    $upload_file_path = $this->ultraHelpers->getUploadableFilePath();

    $unique_string = $this->ultraHelpers->getUniqueString(true);

    $file_name = $file->getClientOriginalName();
    $file_info = $this->ultraHelpers->filterFileInfoFromFilename($file_name);

    return sprintf('%s/%s_%s.%s',
        $upload_file_path,
        $this->ultraText->transliterateText($file_info['name']),
        $unique_string,
        $file_info['extension']
    );
}

UploadNamer example output

dir1/dir2/dir3/my_file_name_1539617029_23bf16f962c658103f10ece7d3ba7a88.txt

that corresponds to
path/to/file/my_file_name(counter)_{time}_{uniqueString}.extension

my UploadListener

<?php

namespace App\EventListener;

use App\UltraHelpers\UltraFileTree;
use App\UltraHelpers\UltraHelpers;
use App\UltraHelpers\UltraText;
use DateTime;
use App\Entity\FileTree;
use Doctrine\ORM\EntityManagerInterface;
use Oneup\UploaderBundle\Event\PreUploadEvent;
use Oneup\UploaderBundle\Event\PostUploadEvent;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

class UploadListener
{
    /**
     * @var EntityManagerInterface
     */
    protected $entityManager;

    /**
     * @var SessionInterface
     */
    protected $session;

    /**
     * @var ContainerInterface
     */
    protected $container;

    /**
     * @var UltraHelpers
     */
    protected $ultraHelpers;

    /**
     * @var UltraText
     */
    protected $ultraText;

    /**
     * @var UltraFileTree
     */
    protected $ultraFileTree;

    protected $originalName;

    public function __construct(EntityManagerInterface $entityManager, SessionInterface $session, ContainerInterface $container, UltraHelpers $ultraHelpers, UltraText $ultraText, UltraFileTree $ultraFileTree)
    {
        $this->entityManager = $entityManager;
        $this->session = $session;
        $this->container = $container;
        $this->ultraHelpers = $ultraHelpers;
        $this->ultraText = $ultraText;
        $this->ultraFileTree = $ultraFileTree;
        $this->originalName = [];
    }

    public function onUpload(PreUploadEvent $event)
    {
        $file = $event->getFile();

        $file_info = $this->ultraHelpers->filterFileInfoFromPath($file->getClientOriginalName());
        $this->originalName = $this->ultraText->transliterateText($file_info['name']);

        $response = $event->getResponse();
        $message = [
            'error' => 'none'
        ];
        $response->addToOffset($message, array('files'));
    }

    public function onPostUpload(PostUploadEvent $event)
    {
        $now_date_time = date('Y-m-d H:i:s');

        $repo_file_tree = $this->entityManager->getRepository('App:FileTree');
        $repo_project = $this->entityManager->getRepository('App:Project');
        $repo_whereabouts = $this->entityManager->getRepository('App:Whereabouts');

        $whereabouts = $this->session->get('whereabouts');
        $my_whereabouts = $repo_whereabouts->getWhereabouts($whereabouts);

        // get project object
        $project_obj = $repo_project->findOneBy(array('id' => $my_whereabouts['project_id']));
        // get current directory object
        $node_selected_obj = $repo_file_tree->findOneBy(array('id' => $my_whereabouts['node_selected_id']));

        $file = $event->getFile();
        $file_size = $file->getSize();

        $current_file_name_parts = $this->ultraHelpers->getFileNameParts($this->originalName);
        $postfix_txt = $this->ultraHelpers->getFileNamePostfixTxt($current_file_name_parts['name'], $my_whereabouts['node_selected_id']);

        $file_tree = new FileTree();
        $file_tree->setItemOriginalName($current_file_name_parts['name'] . $postfix_txt);
        $file_tree->setItemName($current_file_name_parts['name'] . $postfix_txt);
        $file_tree->setItemExtension('.'. $current_file_name_parts['extension']);
        $file_tree->setItemSortValue('.'. $current_file_name_parts['extension'] .'.'. $current_file_name_parts['name'] . $postfix_txt);
        $file_tree->setItemSize($file_size);
        $file_tree->setUpdatedAt(new DateTime($now_date_time));
        $file_tree->setIsFile(true);
        $file_tree->setParent($node_selected_obj);
        $file_tree->setProject($project_obj);

        $this->entityManager->persist($file_tree);
        $this->entityManager->flush();
        $this->entityManager->clear();

        // pārkārto FileTree koka zaru ievērojot jaunpievienoto failu
        $file_tree_branch_node_id = $repo_file_tree->getOneFileTreeNode($my_whereabouts['node_selected_id']);
        $this->ultraFileTree->reorderFileTreeBranch($file_tree_branch_node_id);

        $event->getResponse();
    }
}

Conclusion

Please advise.

Thank You for your time and knowledge.

Update 1

Added github issue (#348) in OneupUploaderBundle on GitHub


Solution

  • One can get file name from namer with $file->getPathname in the eventListener.

    I had sneaky error in code block that followed, thus it led me believe there was a problem with file name...