|
| 1 | + |
| 2 | +============================================== |
| 3 | +File Upload using Extbase and FAL in TYPO3 6.2 |
| 4 | +============================================== |
| 5 | + |
| 6 | +.. post:: |
| 7 | + :tags: TYPO3, Extbase |
| 8 | + |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +.. highlight:: php |
| 13 | +.. default-role:: code |
| 14 | + |
| 15 | + |
| 16 | +:Project: |
| 17 | + TYPO3 CMS extension ext:upload_example for TYPO3 >= 6.2.4 |
| 18 | + |
| 19 | +:Author: |
| 20 | + `Helmut Hummel <helmut.hummel@typo3.org>`__ |
| 21 | + |
| 22 | +:Repository: |
| 23 | + At Github `helhum/upload_example <https://github.com/helhum/upload_example>`__ |
| 24 | + |
| 25 | +:Blogpost: |
| 26 | + `File Upload using Extbase and FAL in TYPO3 6.2 |
| 27 | + <http://insight.helhum.io/post/85015526410/file-upload-using-extbase-and-fal-in-typo3-6-2>`__ |
| 28 | + |
| 29 | +:Credit: |
| 30 | + - `Anja Leichsenring <anja.leichsenring@typo3.org>`__ - for pushing and motivating |
| 31 | + - `Stefan Frömken <froemken@gmail.com>`__ - for handing over private code |
| 32 | + - `Martin Bless <martin@mbless.de>`__ - for help with the documentation |
| 33 | + |
| 34 | + |
| 35 | +**Overview:** |
| 36 | + |
| 37 | +.. contents:: |
| 38 | + :local: |
| 39 | + :depth: 3 |
| 40 | + :backlinks: none |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +What does it do? |
| 45 | +================ |
| 46 | + |
| 47 | +Version 6.2 of the Extbase framework has no support for file upload and image |
| 48 | +upload at all. This is a complete and working example claiming to do it it the *right* way. |
| 49 | + |
| 50 | + |
| 51 | +How does it work? |
| 52 | +================= |
| 53 | + |
| 54 | +- The heart of the extension is the UploadedFileReferenceConverter |
| 55 | +- an extended FileReference model is needed |
| 56 | +- an extended ObjectStorageConverter is needed |
| 57 | +- an extended UploadViewHelper is needed |
| 58 | + |
| 59 | +Everything else in this example extension is more or less plain code as generated |
| 60 | +by the extension builder. |
| 61 | + |
| 62 | + |
| 63 | +What needs to be done? |
| 64 | +====================== |
| 65 | + |
| 66 | +TypeConverter |
| 67 | +------------- |
| 68 | + |
| 69 | +We want to have a custom TypeConverter to: |
| 70 | + |
| 71 | +- evaluate the file upload array |
| 72 | +- move the uploaded file to a FAL storage using the FAL API |
| 73 | +- and have the result persisted in the database using the Extbase persistence. |
| 74 | + |
| 75 | + |
| 76 | +Error handling |
| 77 | +-------------- |
| 78 | + |
| 79 | +We don't want to just throw exceptions but use the TypeConverter API |
| 80 | +to return useful error messages to the user. |
| 81 | + |
| 82 | + |
| 83 | +Configurability |
| 84 | +--------------- |
| 85 | + |
| 86 | +Things should be configurable, especially the TypeConverter. It needs to know |
| 87 | +about |
| 88 | + |
| 89 | +1. the folder to upload to |
| 90 | +2. what to do in case of a name conflict for the uploaded file |
| 91 | +3. the allowed file extensions |
| 92 | +4. how to deal with an already attached resource. |
| 93 | + |
| 94 | +The actual configuration is done through by PropertyMappingConfiguration. |
| 95 | + |
| 96 | +Some configuration options:: |
| 97 | + |
| 98 | + <?php |
| 99 | + class UploadedFileReferenceConverter extends \TYPO3\CMS\Extbase\Property\TypeConverter\AbstractTypeConverter { |
| 100 | + |
| 101 | + /** |
| 102 | + * Folder where the file upload should go to |
| 103 | + * (including storage). |
| 104 | + */ |
| 105 | + const CONFIGURATION_UPLOAD_FOLDER = 1; |
| 106 | + |
| 107 | + /** |
| 108 | + * How to handle an upload when the name |
| 109 | + * of the uploaded file conflicts. |
| 110 | + */ |
| 111 | + const CONFIGURATION_UPLOAD_CONFLICT_MODE = 2; |
| 112 | + |
| 113 | + /** |
| 114 | + * Whether to replace an already present resource. |
| 115 | + * Useful for "maxitems = 1" fields and properties |
| 116 | + * with no ObjectStorage annotation. |
| 117 | + */ |
| 118 | + const CONFIGURATION_ALLOWED_FILE_EXTENSIONS = 4; |
| 119 | + } |
| 120 | + |
| 121 | + |
| 122 | +Handle validation errors and already attached resources |
| 123 | +------------------------------------------------------- |
| 124 | + |
| 125 | +Different cases need to be handled. |
| 126 | + |
| 127 | +Case: A file is already attached |
| 128 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 129 | + |
| 130 | +- When editing an entity that has already an image attached to it, |
| 131 | + through a previous upload for example, saving the entity without |
| 132 | + re-uploading a file should keep the attached resource. |
| 133 | + |
| 134 | +Knowing about an already attached resource is not only in the domain |
| 135 | +of the TypeConverter. Therefore the UploadViewHelper assigns such values |
| 136 | +to a hidden input and protects it by an hash value (hmac). |
| 137 | + |
| 138 | +Additionally the viewhhelper accept child nodes and provides an object "resource". |
| 139 | +This means that you can render the attached resource if you like to. In this |
| 140 | +example a preview of the image is shown: |
| 141 | + |
| 142 | +.. code-block:: html |
| 143 | + |
| 144 | + <h:form.upload property="image" > |
| 145 | + <f:if condition="{resource}"> |
| 146 | + <f:image image="{resource}" alt="" width="50"/> |
| 147 | + </f:if> |
| 148 | + </h:form.upload><br /> |
| 149 | + |
| 150 | + |
| 151 | +Case: Upload succeeds, validation fails |
| 152 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 153 | + |
| 154 | +In this case the file upload succeeds but due to validation errors in some other |
| 155 | +fields the whole form isn't accepted. This also means it isn't persisted yet but we |
| 156 | +nevertheless want to keep the uploaded file as a resource as we don't want to upload it again. |
| 157 | + |
| 158 | +Security |
| 159 | +-------- |
| 160 | + |
| 161 | +To make file upload secure the TypeConverter needs at least needs to care about these two issues: |
| 162 | + |
| 163 | +1. Deny upload of PHP files! :: |
| 164 | + |
| 165 | + <?php |
| 166 | + if (!GeneralUtility::verifyFilenameAgainstDenyPattern($uploadInfo['name'])) { |
| 167 | + throw new TypeConverterException('Uploading files with PHP file extensions is not allowed!', 1399312430); |
| 168 | + } |
| 169 | + ?> |
| 170 | + |
| 171 | + It cannot be stressed enough how important these three lines of code are! |
| 172 | + |
| 173 | + .. important:: |
| 174 | + |
| 175 | + - These lines are mandatory and NOT optional. |
| 176 | + - These lines are independent from the configurable allowed file extensions. |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | +Usage |
| 181 | +===== |
| 182 | + |
| 183 | +1. Get from Github, install as extension |
| 184 | +2. Create folder ./fileadmin/contents |
| 185 | +3. No extra TypoScript needs to be included |
| 186 | +4. Create a page, insert the plugin as a content element |
| 187 | +5. Start playing in the frontend. |
| 188 | + |
| 189 | + |
| 190 | +Adaptation |
| 191 | +========== |
| 192 | + |
| 193 | +- Look into the controller to get an idea about how how to configure the type converter. |
| 194 | +- Look into the TCA to see how to properly set the match_fields so that Extbase Persistence |
| 195 | + does the right thing. |
| 196 | +- ... |
| 197 | + |
| 198 | + |
| 199 | +Contribute |
| 200 | +========== |
| 201 | + |
| 202 | +- `Send pull requests to the repository. <https://github.com/helhum/upload_example>`__ |
| 203 | +- `Use the issue tracker for feedback and discussions. <https://github.com/helhum/upload_example/issues>`__ |
| 204 | + |
| 205 | +Enjoy! |
0 commit comments