.. post::
:tags: TYPO3, Extbase
Project: | TYPO3 CMS extension ext:fal_upload for TYPO3 >= 6.2.4 |
---|---|
Author: | |
Repository: | At Github mindscreen/t3ext-fal_upload |
Credit: |
|
Overview:
This is an extension simplifying the addition of file upload functionality in other extensions.
- The heart of the extension is the UploadedFileReferenceConverter
- an extended FileReference model is needed
- an extended ObjectStorageConverter is needed
- an extended UploadViewHelper is needed
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.
We don't want to just throw exceptions but use the TypeConverter API to return useful error messages to the user.
Things should be configurable, especially the TypeConverter. It needs to know about
- the folder to upload to
- what to do in case of a name conflict for the uploaded file
- the allowed file extensions
- 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;
}
Different cases need to be handled.
- 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 />
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.
To make file upload secure the TypeConverter needs at least needs to care about these two issues:
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.
- Get from Github, install as extension
- Create folder ./fileadmin/content
- No extra TypoScript needs to be included
- Create an extension and use the classes provided by the plugin
- Start playing
- 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.
- ...
Enjoy!