我将Symfony v4.1.*
与OneUpUploaderBundle一起使用。
我需要为每个上传的文件设置唯一的名称-可以。
我有tree structure
,其中包含上传的文件。
为了管理树中的文件,我有多个属性。
其中一些是:
.txt
或.a
(如果是目录) ItemSortValue
确保树枝(字段)按字母顺序排序。
我还需要将有关上传文件的信息保留在数据库中。 为此,我需要在
complete file name
事件的UploadNamer
中获得UploadListener
管理的每个onPostUpload
。
path/to/file/my_file_name(counter)_{time}_{uniqueString}.extension
我需要将相同的唯一文件名保存到数据库。
然而,我面临的问题是-我不知道,也找不到如何将信息从UploadNamer
传递到uploadListener
。
我尝试使用$this->originalName
,但没有得到积极的结果。
如何使用UploadNamer
将UploadListener
中的值从Symfony v4.1
传递到OneUpUploaderBundle
中的{em>(onPostUpload事件)?
自定义名称
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
示例输出
dir1/dir2/dir3/my_file_name_1539617029_23bf16f962c658103f10ece7d3ba7a88.txt
that corresponds to
path/to/file/my_file_name(counter)_{time}_{uniqueString}.extension
我的
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();
}
}
请告知。
感谢您的时间和知识。
在GitHub的OneupUploaderBundle中添加了github issue(#348)
答案 0 :(得分:0)
一个人可以在eventListener
中用wrap_content
从命名者那里获取文件名。
随后的代码块中出现了偷偷摸摸的错误,因此使我相信文件名存在问题...