mindscreen/fal_upload

Handling file uploads via Extbase, inspired by upload_example of helhum

Installs: 3 417

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 3

Forks: 3

Open Issues: 0

Type:typo3-cms-extension

0.1.1 2018-09-06 13:49 UTC

This package is auto-updated.

Last update: 2024-11-28 16:33:31 UTC


README

.. post::
   :tags: TYPO3, Extbase


Overview:

What does it do?

This is an extension simplifying the addition of file upload functionality in other extensions.

How does it work?

  • The heart of the extension is the UploadedFileReferenceConverter
  • an extended FileReference model is needed
  • an extended ObjectStorageConverter is needed
  • an extended UploadViewHelper is needed

What needs to be done?

TypeConverter

We want to have a custom TypeConverter to:

  • evaluate the file upload array
  • move the uploaded file to a FAL storage using the FAL API
  • and have the result persisted in the database using the Extbase persistence.

Error handling

We don't want to just throw exceptions but use the TypeConverter API to return useful error messages to the user.

Configurability

Things should be configurable, especially the TypeConverter. It needs to know about

  1. the folder to upload to
  2. what to do in case of a name conflict for the uploaded file
  3. the allowed file extensions
  4. how to deal with an already attached resource.

The actual configuration is done through by PropertyMappingConfiguration.

Some configuration options:

<?php
class UploadedFileReferenceConverter extends \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter {

   /**
    * Folder where the file upload should go to
    * (including storage).
    */
   const CONFIGURATION_UPLOAD_FOLDER = 1;

   /**
    * How to handle an upload when the name
    * of the uploaded file conflicts.
    */
   const CONFIGURATION_UPLOAD_CONFLICT_MODE = 2;

   /**
    * Whether to replace an already present resource.
    * Useful for "maxitems = 1" fields and properties
    * with no ObjectStorage annotation.
    */
   const CONFIGURATION_ALLOWED_FILE_EXTENSIONS = 4;
}

Handle validation errors and already attached resources

Different cases need to be handled.

Case: A file is already attached

  • When editing an entity that has already an image attached to it, through a previous upload for example, saving the entity without re-uploading a file should keep the attached resource.

Knowing about an already attached resource is not only in the domain of the TypeConverter. Therefore the UploadViewHelper assigns such values to a hidden input and protects it by an hash value (hmac).

Additionally the viewhhelper accept child nodes and provides an object "resource". This means that you can render the attached resource if you like to. In this example a preview of the image is shown:

<h:form.upload property="image" >
   <f:if condition="{resource}">
      <f:image image="{resource}" alt="" width="50"/>
   </f:if>
</h:form.upload><br />

Case: Upload succeeds, validation fails

In this case the file upload succeeds but due to validation errors in some other fields the whole form isn't accepted. This also means it isn't persisted yet but we nevertheless want to keep the uploaded file as a resource as we don't want to upload it again.

Security

To make file upload secure the TypeConverter needs at least needs to care about these two issues:

  1. Deny upload of PHP files!

    <?php
    if (!GeneralUtility::verifyFilenameAgainstDenyPattern($uploadInfo['name'])) {
       throw new TypeConverterException('Uploading files with PHP file extensions is not allowed!', 1399312430);
    }

    It cannot be stressed enough how important these three lines of code are!

    Important

    • These lines are mandatory and NOT optional.
    • These lines are independent from the configurable allowed file extensions.

Install

  1. Get from Github, install as extension
  2. Create folder ./fileadmin/content
  3. No extra TypoScript needs to be included
  4. Create an extension and use the classes provided by the plugin
  5. Start playing

Adaptation

  • Look into the controller to get an idea about how how to configure the type converter.
  • Look into the TCA to see how to properly set the match_fields so that Extbase Persistence does the right thing.
  • ...

Contribute

Enjoy!