dtpa98038 2018-10-15 18:53
浏览 67
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • doujiangao4229 2018-10-17 16:50
    关注

    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...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么