使用 OneUpUploaderBundle 在 Symfony v4.1 中将文件名从 UploadNamer 传递到 UploadListener

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

简介

我正在使用 Symfony v4.1.*OneUpUploaderBundle。 我需要为每个上传的文件设置唯一的名称 - 有效。

我有 tree structure,其中包含上传的文件。 为了管理树中的文件,我有多个属性。 其中一些是:

  1. itemName(音译文件名+计数器+unique_sting)
  2. itemOriginalName(音译文件名+计数器)
  3. itemExtension(文件扩展名 - 例如 .txt.a 如果是目录)
  4. itemSortValue(文件扩展名+音译文件名+计数器+unique_sting)

ItemSortValue 确保树枝(字段)按字母顺序排序。

问题

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

我需要将相同的唯一文件名保存到数据库中。 然而,我面临的问题是——我不知道也找不到如何将信息从 UploadNamer 传递到 uploadListener

我尝试使用 $this->originalName 但没有产生积极的结果。

问题

如何将 UploadNamer 中的值传递给 UploadListener (onPostUpload 事件) in Symfony v4.1 with OneUpUploaderBundle

代码:

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();
    }
}

结论

请指教

感谢您的宝贵时间和知识。

更新 1

在 GitHub

上的 OneupUploaderBundle 中添加了 github issue (#348)

可以从 eventListener.

中使用 $file->getPathname 的命名器获取文件名

我在随后的代码块中出现了偷偷摸摸的错误,因此我相信文件名有问题...