如何将typo3插件扩展转换为后端模块扩展?

发布于 2025-01-09 19:57:13 字数 203 浏览 0 评论 0 原文

我在运行typo3 10的后端模块时仍然遇到问题。我发现了以下扩展,它作为插件可以正常工作。https ://github.com/helhum/upload_example 现在我希望它作为后端模块运行以检查它是否可以这样工作。

I have still problems to run an backend module for typo3 10. I found following extension, which works fine as a plugin.https://github.com/helhum/upload_example Now I want it to run it as a backend module to check if it works that way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习ぎ惯性依靠 2025-01-16 19:57:13

根据评论,这里的目标是通过后端模块上传一些照片。假设您已经有一个模块和一个操作,例如 indexAction (如果没有,以下是创建模块的方法:后端模块)这是您可以执行的操作。

请记住,这是我能想到的最基本的示例。必须根据您的需求考虑验证和各种其他操作。

HTML

<f:form action="upload" controller="YourController" name="files" enctype="multipart/form-data">
     <div class="mb-3">
          <label for="formFile" class="form-label">Select a file</label>
          <f:form.textfield name="file" type="file" class="form-control" id="formFile"/>
     </div>
     <f:form.submit value="File upload" class="btn btn-primary"/>
</f:form>

此示例适用于一个文件,但您也可以使用输入上的 multiple 属性将其转换为多个文件。尽管如此,这里没什么特别的。一种简单的表单,其中包含一个用于选择文件的字段。请记住:TYPO3 使用 Bootstrap,因此您只需复制粘贴代码即可获得漂亮的表单。

UploadAction

我们定义了上传发生的操作,在本例中是uploadAction。您在这里需要做的是获取表单参数并对其执行某些操作,例如验证它们是否是图像、csv 或任何其他文件类型。

public function uploadAction(): void
{
   $fileRequest = $this->request->getArguments()['file'];
   
}

这将为您提供“文件”参数内的值。

小心! file 参数必须具有特定的结构
上传即可工作。它应该看起来像这样

array [
  name => 'filename.csv' (13 chars)
  type => 'text/csv' (8 chars)
  tmp_name => '/tmp/phphwRQXQ' (14 chars)
  error => 0 (integer)
  size => 62505 (integer)
]

下一步是实际保存文件。我正在使用依赖注入,但为了这个答案,我将只使用 GeneralUtility::makeInstance 变体。

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Resource\ResourceFactory;

public function uploadAction(): void
{
   $fileRequest = $this->request->getArguments()['file'];
   
   $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
   $resourceStorage = $resourceFactory->getStorageObject(1);

   $subFolder = 'UploadedPhotos';
   if ($resourceStorage->hasFolder($subFolder) === false) {
       $resourceStorage->createFolder($subFolder);
   }
   $subFolderObject = $this->resourceStorage->getFolder($subFolder);
   $uploadedFile = $subFolderObject->addUploadedFile($file);
}

分解:

  1. 我们实例化ResourceFactory,以便我们可以获得存储
    文件要上传到的位置
  2. 我们通过 UID 获取存储空间,例如 fileadmin。我个人将存储的 UID 保存在 扩展程序设置,然后调用< /a> 它在我想要的任何地方。
  3. 我总是将文件保存到子文件夹中,因此我首先检查该文件夹是否存在。如果没有我创建它。然后获取文件夹并将其保存在变量中。
  4. 上传文件。

函数addUploadedFile接受两个参数。第一个是要上传的文件,第二个是复制行为。如果文件已经存在,它应该做什么。默认情况下,它会取消上传。但您可以重命名它,或替换它。所以它看起来像这样:

use TYPO3\CMS\Core\Resource\DuplicationBehavior;

$uploadedFile = $subFolderObject->addUploadedFile($file, DuplicationBehavior::REPLACE);

就是这样!

提示:

您不能总是相信来自客户端的内容,因此我首先将文件保存到临时文件夹中,检查内容 而不是文件的扩展名。任何人都可以将 php 文件重命名为 file.png 并仍然通过扩展名验证(假设您检查文件的扩展名是 png 或 jpg 等)。如果一切正常,您只需将文件复制到目标文件夹并删除临时文件夹即可。您可以在此处找到如何执行此操作:使用文件

如果你想避免/检查重复行为(如果我没记错的话,DuplicationBehaviour 只检查名称而不是内容),您可以通过两种方式完成。首先,您阅读文件的内容并生成哈希从它。然后您可以检查所有其他文件是否具有相同的哈希值。如果我没记错的话,TYPO3 实际上将文件哈希保存在数据库中,因此在您的 File 对象中,您可以验证保存的哈希是否与您要上传的哈希相同。您可以获得这样的文件:

if ($subFolderObject->getFileCount() > 0) {
  $files = $subFolderObject->getFiles();
  foreach ($files as $file) {
     // do something
   }
}

第二种方法是将文件重命名为随机名称(可能是时间戳加上其他名称),如果您不关心是否再次上传相同的文件。所以你永远不会有重复行为。

希望有帮助

Based on the comments, the goal here is to just upload some photos via the a backend Module. Assuming that you already have a Module and an action for it, for example indexAction (if not, here is how to create a module: Backend Module) here is what you can do.

Keep in mind, this is the most basic example i could think of. Validations and various other actions have to be taken into consideration based on your needs.

HTML

<f:form action="upload" controller="YourController" name="files" enctype="multipart/form-data">
     <div class="mb-3">
          <label for="formFile" class="form-label">Select a file</label>
          <f:form.textfield name="file" type="file" class="form-control" id="formFile"/>
     </div>
     <f:form.submit value="File upload" class="btn btn-primary"/>
</f:form>

This example is for one file but you can convert it to multiple as well by using the multiple attribute on the input. Nevertheless, nothing special here. A simple form with one field that selects a file(s). Remember: TYPO3 uses Bootstrap, so you can just copy paste the code so you can have a nice form.

UploadAction

We defined on which action will the upload take place and in this case is the uploadAction. What you need to do here, is to get the form arguments and do something with them, like validating if they are images or csv or any other file kind.

public function uploadAction(): void
{
   $fileRequest = $this->request->getArguments()['file'];
   
}

This will give you the values inside the 'file' arguments.

Be careful! The file arguments must have a specific structure for
the upload to work. It should look like this

array [
  name => 'filename.csv' (13 chars)
  type => 'text/csv' (8 chars)
  tmp_name => '/tmp/phphwRQXQ' (14 chars)
  error => 0 (integer)
  size => 62505 (integer)
]

The next step is to actually save the file. I am using Dependency Injection but for the sake of this answer, i will just use the GeneralUtility::makeInstance variant.

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Resource\ResourceFactory;

public function uploadAction(): void
{
   $fileRequest = $this->request->getArguments()['file'];
   
   $resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
   $resourceStorage = $resourceFactory->getStorageObject(1);

   $subFolder = 'UploadedPhotos';
   if ($resourceStorage->hasFolder($subFolder) === false) {
       $resourceStorage->createFolder($subFolder);
   }
   $subFolderObject = $this->resourceStorage->getFolder($subFolder);
   $uploadedFile = $subFolderObject->addUploadedFile($file);
}

Breaking it down:

  1. We instantiate the ResourceFactory so we can get the Storage
    where the files going to be uploaded to
  2. We get the storage, for example the fileadmin, by its UID. I personally save the UID of the storage on the extension settings and then call it wherever i want.
  3. I always save the files to a subfolder so i first check if the folder exists. If not i create it. Then get the folder and save it in a variable.
  4. Upload the file.

The function addUploadedFile accepts two arguments. First the file to be uploaded and the second is the Duplication behaviour. What it should do if the file already exists. By default, it cancels the upload. But you can either rename it, or replace it. So it would look like this:

use TYPO3\CMS\Core\Resource\DuplicationBehavior;

$uploadedFile = $subFolderObject->addUploadedFile($file, DuplicationBehavior::REPLACE);

And that is it!

Tipps:

You can not always trust what comes from the client, so i would first save the files to a temp folder, check the CONTENT and not the extension of the file. Anyone could just rename a php file to file.png and still pass the extension validation (assuming that you check if the extension of the file is png or jpg etc). If everything checks out, you can just copy the files to the destination folder and delete the temp. You can find how to do that here: Working with files

If you want to avoid/check duplication behaviour (if i am not wrong, the DuplicationBehaviour only checks the name but not the content), you can do it with two ways. First you read the content of the file and generate a hash from it. Then you can check all the other files if they have the same hash. If i remember correctly, TYPO3 actually saves the file hash in the database, so in your File object you can validate if the saved hash is the same with the one you are about to upload. You can get the files like this:

if ($subFolderObject->getFileCount() > 0) {
  $files = $subFolderObject->getFiles();
  foreach ($files as $file) {
     // do something
   }
}

The second way it is just to rename the file to a random name (maybe timestamp plus something else) if you do not care if the same file is uploaded again. So you ll never have a duplication behaviour.

Hope it was helpful

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文