Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.

Commit 1c1554e

Browse files
Milos Dencevslashrsm
authored andcommitted
Some cleanup, added remove archive in form, reformat validation we now have a trait
1 parent 1349e92 commit 1c1554e

File tree

6 files changed

+116
-146
lines changed

6 files changed

+116
-146
lines changed

file_entity.module

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,6 @@ function file_entity_help($route_name, RouteMatchInterface $route_match) {
8383
* Implements hook_menu().
8484
*/
8585
function file_entity_menu() {
86-
$items['file/add/upload/archive'] = array(
87-
'title' => 'Archive',
88-
'page callback' => 'drupal_get_form',
89-
'page arguments' => array('file_entity_upload_archive_form'),
90-
'access arguments' => array('administer files'),
91-
'file' => 'file_entity.pages.inc',
92-
'type' => MENU_LOCAL_TASK,
93-
'weight' => -5,
94-
);
95-
9686
// Optional devel module integration
9787
if (\Drupal::moduleHandler()->moduleExists('devel')) {
9888
if (\Drupal::moduleHandler()->moduleExists('token')) {

file_entity.routing.yml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,16 +77,6 @@ file_entity.file_add_upload_file:
7777
options:
7878
_admin_route: TRUE
7979

80-
file_entity.file_add_upload_archive:
81-
path: /file/add/upload/archive
82-
defaults:
83-
_controller: '\Drupal\file_entity\Controller\FileController::FileAddUploadArchive'
84-
_title: 'Archive'
85-
requirements:
86-
_permission: 'administer files'
87-
options:
88-
_admin_route: TRUE
89-
9080
entity.file.canonical:
9181
path: /file/{file}
9282
defaults:
@@ -146,10 +136,10 @@ file_entity.settings:
146136
_permission: 'administer files'
147137

148138
file_entity.file_add_archive_form:
149-
path: /file/add/upload/archive
139+
path: admin/content/files/archive
150140
defaults:
151141
_form: '\Drupal\file_entity\Form\FileAddArchiveForm'
152-
_title: 'Add files'
142+
_title: 'Upload archive'
153143
requirements:
154144
_permission: 'administer files'
155145
options:

src/Controller/FileController.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ public function FileAddUploadFile() {
3636

3737
}
3838

39-
/**
40-
* Archive
41-
*/
42-
public function FileAddUploadArchive() {
43-
44-
}
45-
4639
/**
4740
* Usage
4841
*

src/Form/FileAddArchiveForm.php

Lines changed: 34 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
use Drupal\Core\Form\FormStateInterface;
1313
use Drupal\Component\Utility\Bytes;
1414
use Drupal\file\Entity\File;
15+
use Drupal\file_entity\UploadValidatorsTrait;
1516

1617
/**
17-
* Class FileAddArchiveForm.
18-
*
19-
* @package Drupal\file_entity\Form
18+
* Form controller for archive type forms.
2019
*/
2120
class FileAddArchiveForm extends FormBase {
2221

22+
use UploadValidatorsTrait;
23+
2324
/**
2425
* {@inheritdoc}
2526
*/
@@ -31,27 +32,38 @@ public function getFormId() {
3132
* {@inheritdoc}
3233
*/
3334
public function buildForm(array $form, FormStateInterface $form_state) {
34-
$validators = $this->getUploadValidators($form_state->get('options'));
35+
$options = [
36+
'file_extensions' => archiver_get_extensions(),
37+
];
38+
$options = $form_state->get('options') ? $form_state->get('options') : $options;
39+
$validators = $this->getUploadValidators($options);
3540

3641
$form['upload'] = array(
3742
'#type' => 'managed_file',
3843
'#title' => $this->t('Upload an archive file'),
39-
'#upload_location' => NULL,
44+
'#upload_location' => 'public://',
4045
'#progress_indicator' => 'bar',
4146
'#default_value' => $form_state->has('file') ? array($form_state->get('file')->id()) : NULL,
4247
'#required' => TRUE,
43-
'#description' => 'Files must be less than <strong>' . format_size($validators['file_validate_size'][0]) . '</strong><br> Allowed file types: <strong>' . $validators['file_validate_extensions'][0] . '</strong>',
48+
'#description' => $this->t('Files must be less than <strong>' . format_size($validators['file_validate_size'][0]) . '</strong><br> Allowed file types: <strong>' . $validators['file_validate_extensions'][0] . '</strong>'),
4449
'#upload_validators' => $validators,
4550
);
4651

4752
$form['pattern'] = array(
4853
'#type' => 'textfield',
4954
'#title' => $this->t('Pattern'),
50-
'#description' => $this->t('Only files matching this pattern will be imported. For example, to import all jpg and gif files, the pattern would be <em>*.jpg|*.gif</em>. Use <em>.*</em> to extract all files in the archive.'),
55+
'#description' => $this->t('Only files matching this pattern will be imported. For example, to import all jpg and gif files, the pattern would be <strong>.*jpg|.*gif</strong>. Use <strong>.*</strong> to extract all files in the archive.'),
5156
'#default_value' => '.*',
5257
'#required' => TRUE,
5358
);
5459

60+
$form['remove_archive'] = array(
61+
'#type' => 'checkbox',
62+
'#title' => $this->t('Remove archive'),
63+
'#description' => $this->t('Removes archive after extraction.'),
64+
'#default_value' => FALSE,
65+
);
66+
5567
$form['actions'] = array('#type' => 'actions');
5668
$form['actions']['submit'] = array(
5769
'#type' => 'submit',
@@ -91,62 +103,28 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
91103
]);
92104
$file->save();
93105
}
106+
$all_files = file_scan_directory($extract_dir, '/.*/');
107+
// Get all files that don't match the pattern so we can remove them.
108+
$remainig_files = array_diff_key($all_files, $files);
109+
foreach ($remainig_files as $file) {
110+
drupal_unlink($file->uri);
111+
}
94112
}
95113
drupal_set_message($this->t('Extracted %file and added @count new files.', array('%file' => $archive->getFilename(), '@count' => count($files))));
114+
if ($form_state->getValue('remove_archive')) {
115+
drupal_set_message($this->t('Removed archive.'));
116+
$archive->delete();
117+
}
118+
else {
119+
$archive->setPermanent();
120+
$archive->save();
121+
}
96122
}
97123
else {
98-
throw new \Exception(t('Cannot extract %file, not a valid archive.', array('%file' => $archive->getFileUri())));
124+
$form_state->setErrorByName('', $this->t('Cannot extract %file, not a valid archive.', array('%file' => $archive->getFileUri())));
99125
}
100126
}
101127
$this->redirect('entity.file.collection')->send();
102128
}
103129

104-
/**
105-
* Retrieves the upload validators for a file.
106-
*
107-
* @param array $options
108-
* (optional) An array of options for file validation.
109-
*
110-
* @return array
111-
* An array suitable for passing to file_save_upload() or for a managed_file
112-
* or upload element's '#upload_validators' property.
113-
*/
114-
public function getUploadValidators(array $options = array()) {
115-
// Set up file upload validators.
116-
$validators = array();
117-
118-
// Validate file extensions. If there are no file extensions in $params and
119-
// there are no Media defaults, there is no file extension validation.
120-
if (!empty($options['file_extensions'])) {
121-
$validators['file_validate_extensions'] = array($options['file_extensions']);
122-
}
123-
else {
124-
$validators['file_validate_extensions'] = array(archiver_get_extensions());
125-
}
126-
127-
// Cap the upload size according to the system or user defined limit.
128-
$max_filesize = file_upload_max_size();
129-
$user_max_filesize = Bytes::toInt(\Drupal::config('file_entity.settings')
130-
->get('max_filesize'));
131-
132-
// If the user defined a size limit, use the smaller of the two.
133-
if (!empty($user_max_filesize)) {
134-
$max_filesize = min($max_filesize, $user_max_filesize);
135-
}
136-
137-
if (!empty($options['max_filesize']) && $options['max_filesize'] < $max_filesize) {
138-
$max_filesize = Bytes::toInt($options['max_filesize']);
139-
}
140-
141-
// There is always a file size limit due to the PHP server limit.
142-
$validators['file_validate_size'] = array($max_filesize);
143-
144-
// Add other custom upload validators from options.
145-
if (!empty($options['upload_validators'])) {
146-
$validators += $options['upload_validators'];
147-
}
148-
149-
return $validators;
150-
}
151-
152130
}

src/Form/FileAddForm.php

Lines changed: 11 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
use Drupal\file\Entity\File;
1818
use Drupal\file\FileInterface;
1919
use Drupal\file_entity\Entity\FileType;
20+
use Drupal\file_entity\UploadValidatorsTrait;
2021

2122
/**
2223
* Form controller for file type forms.
2324
*/
2425
class FileAddForm extends FormBase {
2526

27+
use UploadValidatorsTrait;
28+
2629
/**
2730
* Returns a unique string identifying the form.
2831
*
@@ -71,11 +74,18 @@ public function buildForm(array $form, FormStateInterface $form_state, array $op
7174
* Returns form data
7275
*/
7376
function stepUpload(array $form, FormStateInterface $form_state) {
77+
$options = [
78+
'file_extensions' => \Drupal::config('file_entity.settings')
79+
->get('default_allowed_extensions'),
80+
];
81+
$options = $form_state->get('options') ? $form_state->get('options') : $options;
82+
$validators = $this->getUploadValidators($options);
83+
7484
$form['upload'] = array(
7585
'#type' => 'managed_file',
7686
'#title' => t('Upload a new file'),
7787
'#upload_location' => $this->getUploadDestinationUri($form_state->get('options')),
78-
'#upload_validators' => $this->getUploadValidators($form_state->get('options')),
88+
'#upload_validators' => $validators,
7989
'#progress_indicator' => 'bar',
8090
'#required' => TRUE,
8191
'#default_value' => $form_state->has('file') ? array($form_state->get('file')->id()) : NULL,
@@ -124,66 +134,6 @@ function getUploadDestinationUri(array $params, array $data = array()) {
124134
return $params['uri_scheme'] . '://' . $destination;
125135
}
126136

127-
/**
128-
* Retrieves the upload validators for a file.
129-
*
130-
* @param array $options
131-
* (optional) An array of options for file validation.
132-
*
133-
* @return array
134-
* An array suitable for passing to file_save_upload() or for a managed_file
135-
* or upload element's '#upload_validators' property.
136-
*/
137-
public static function getUploadValidators(array $options = array()) {
138-
// Set up file upload validators.
139-
$validators = array();
140-
141-
// Validate file extensions. If there are no file extensions in $params and
142-
// there are no Media defaults, there is no file extension validation.
143-
if (!empty($options['file_extensions'])) {
144-
$validators['file_validate_extensions'] = array($options['file_extensions']);
145-
}
146-
else {
147-
$validators['file_validate_extensions'] = array(
148-
\Drupal::config('file_entity.settings')
149-
->get('default_allowed_extensions')
150-
);
151-
}
152-
153-
// Cap the upload size according to the system or user defined limit.
154-
$max_filesize = Bytes::toInt(file_upload_max_size());
155-
$user_max_filesize = Bytes::toInt(\Drupal::config('file_entity.settings')
156-
->get('max_filesize'));
157-
158-
// If the user defined a size limit, use the smaller of the two.
159-
if (!empty($user_max_filesize)) {
160-
$max_filesize = min($max_filesize, $user_max_filesize);
161-
}
162-
163-
if (!empty($options['max_filesize']) && $options['max_filesize'] < $max_filesize) {
164-
$max_filesize = Bytes::toInt($options['max_filesize']);
165-
}
166-
167-
// There is always a file size limit due to the PHP server limit.
168-
$validators['file_validate_size'] = array($max_filesize);
169-
170-
// Add image validators.
171-
$options += array('min_resolution' => 0, 'max_resolution' => 0);
172-
if ($options['min_resolution'] || $options['max_resolution']) {
173-
$validators['file_validate_image_resolution'] = array(
174-
$options['max_resolution'],
175-
$options['min_resolution']
176-
);
177-
}
178-
179-
// Add other custom upload validators from options.
180-
if (!empty($options['upload_validators'])) {
181-
$validators += $options['upload_validators'];
182-
}
183-
184-
return $validators;
185-
}
186-
187137
/**
188138
* Form Step 2
189139
* Select file types.

src/UploadValidatorsTrait.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
/**
3+
* @file
4+
* Contains \Drupal\file_entity\UploadValidatorsTrait.
5+
*/
6+
7+
namespace Drupal\file_entity;
8+
9+
use Drupal\Component\Utility\Bytes;
10+
11+
/**
12+
* Trait for validating form uploads.
13+
*/
14+
trait UploadValidatorsTrait {
15+
16+
/**
17+
* Retrieves the upload validators for a file or archive.
18+
*
19+
* @param array $options
20+
* (optional) An array of options for file validation.
21+
*
22+
* @return array
23+
* An array suitable for passing to file_save_upload() or for a managed_file
24+
* or upload element's '#upload_validators' property.
25+
*/
26+
public function getUploadValidators(array $options = array()) {
27+
// Set up file upload validators.
28+
$validators = array();
29+
30+
// Validate file extensions. If there are no file extensions in $params and
31+
// there are no Media defaults, there is no file extension validation.
32+
if (!empty($options['file_extensions'])) {
33+
$validators['file_validate_extensions'] = array($options['file_extensions']);
34+
}
35+
36+
// Cap the upload size according to the system or user defined limit.
37+
$max_filesize = file_upload_max_size();
38+
$user_max_filesize = Bytes::toInt(\Drupal::config('file_entity.settings')
39+
->get('max_filesize'));
40+
41+
// If the user defined a size limit, use the smaller of the two.
42+
if (!empty($user_max_filesize)) {
43+
$max_filesize = min($max_filesize, $user_max_filesize);
44+
}
45+
46+
if (!empty($options['max_filesize']) && $options['max_filesize'] < $max_filesize) {
47+
$max_filesize = Bytes::toInt($options['max_filesize']);
48+
}
49+
50+
// There is always a file size limit due to the PHP server limit.
51+
$validators['file_validate_size'] = array($max_filesize);
52+
53+
// Add image validators.
54+
if ($options['min_resolution'] || $options['max_resolution']) {
55+
$validators['file_validate_image_resolution'] = array(
56+
$options['max_resolution'],
57+
$options['min_resolution'],
58+
);
59+
}
60+
61+
// Add other custom upload validators from options.
62+
if (!empty($options['upload_validators'])) {
63+
$validators += $options['upload_validators'];
64+
}
65+
66+
return $validators;
67+
}
68+
69+
}

0 commit comments

Comments
 (0)