2
2
3
3
namespace SaschaEnde \T3helpers \Utilities ;
4
4
5
- use TYPO3 \ CMS \ Core \ Resource \ ResourceFactory ;
5
+ use t3h \ t3h ;
6
6
use TYPO3 \CMS \Core \SingletonInterface ;
7
+ use TYPO3 \CMS \Core \Utility \File \BasicFileUtility ;
8
+ use TYPO3 \CMS \Core \Utility \GeneralUtility ;
7
9
8
10
class Upload implements UploadInterface, SingletonInterface {
9
11
10
12
protected $ maxFiles = null ;
11
- protected $ allowedFiletypes = ['jpg ' ,'jpeg ' ,'zip ' ,'rar ' ,'pdf ' ,'gif ' ];
13
+ protected $ allowedFiletypes = ['jpg ' , 'jpeg ' , 'zip ' , 'rar ' , 'pdf ' , 'gif ' ];
12
14
protected $ maxFilesize = null ;
13
- protected $ maxFilesizeTotal = null ;
14
15
protected $ targetFolder = '' ;
15
16
protected $ autoFilenames = true ;
16
17
protected $ FILES = [];
17
18
18
19
/**
20
+ * Set allowed filetypes
19
21
* @param $filetypes
20
22
* @return $this
21
23
*/
22
- public function setAllowedFiletypes ($ filetypes ){
24
+ public function setAllowedFiletypes ($ filetypes ) {
23
25
$ this ->allowedFiletypes = $ filetypes ;
24
26
return $ this ;
25
27
}
26
28
27
29
/**
30
+ * Set max filesize, allowed for each file
28
31
* @param $size
29
32
* @return $this
30
33
*/
31
- public function setMaxFilesize ($ size ){
34
+ public function setMaxFilesize ($ size ) {
32
35
$ this ->maxFilesize = $ size ;
33
36
return $ this ;
34
37
}
35
38
36
39
/**
37
- * @param $size
40
+ * Enable autonaming of uploaded files with hash values
41
+ * @param $setting
38
42
* @return $this
39
43
*/
40
- public function setMaxFilesizeTotal ( $ size ) {
41
- $ this ->maxFilesizeTotal = $ size ;
44
+ public function setAutofilenames ( $ setting ) {
45
+ $ this ->autoFilenames = $ setting ;
42
46
return $ this ;
43
47
}
44
48
45
49
/**
46
- * @param $setting
47
- * @return $this
50
+ * Check uploaded files and set them for upload
51
+ * @return array
48
52
*/
49
- public function setAutofilenames ($ setting ){
50
- $ this ->autoFilenames = $ setting ;
51
- return $ this ;
52
- }
53
+ public function check () {
54
+
55
+ /** @var BasicFileUtility $BasicFileUtility */
56
+ $ BasicFileUtility = t3h::injectClass (BasicFileUtility::class);
57
+
58
+ $ Errors = [];
59
+
60
+ if ($ this ->maxFiles != null && count ($ FILES ['name ' ]) > $ this ->maxFiles ) {
61
+ $ Errors [] = [
62
+ 'error ' => 'count '
63
+ ];
64
+ }
65
+
66
+
67
+ foreach ($ _FILES as $ ext => $ FILES ) {
68
+ foreach ($ FILES ['name ' ] as $ prop => $ value ) {
69
+
70
+ if ($ FILES ['size ' ][$ prop ] == 0 ) {
71
+ continue ;
72
+ }
53
73
54
- public function check (){
55
- foreach ($ _FILES as $ ext =>$ FILES ){
56
- foreach ($ FILES ['name ' ] as $ prop =>$ value ){
57
- $ this ->FILES [$ prop ] = [
58
- 'name ' => $ FILES ['name ' ][$ prop ],
59
- 'type ' => $ FILES ['type ' ][$ prop ],
60
- 'tmp_name ' => $ FILES ['tmp_name ' ][$ prop ],
61
- 'error ' => $ FILES ['error ' ][$ prop ],
62
- 'size ' => $ FILES ['size ' ][$ prop ],
63
- 'pathinfo ' => pathinfo ($ FILES ['name ' ][$ prop ]),
74
+ // get pathinfo for this file
75
+ $ pathinfo = pathinfo ($ FILES ['name ' ][$ prop ]);
76
+
77
+ if ($ this ->autoFilenames ) {
78
+ $ targetFilename = sha1 ($ FILES ['name ' ][$ prop ] . uniqid ()) . time () . '. ' . $ pathinfo ['extension ' ];
79
+ } else {
80
+ $ targetFilename = \TYPO3 \CMS \Core \Resource \Driver \LocalDriver::sanitizeFileName ($ FILES ['name ' ][$ prop ]);
81
+ }
82
+
83
+ $ uploadfile = [
84
+ 'targetFilename ' => $ targetFilename ,
85
+ 'name ' => $ FILES ['name ' ][$ prop ],
86
+ 'type ' => $ FILES ['type ' ][$ prop ],
87
+ 'tmp_name ' => $ FILES ['tmp_name ' ][$ prop ],
88
+ 'error ' => $ FILES ['error ' ][$ prop ],
89
+ 'size ' => $ FILES ['size ' ][$ prop ],
90
+ 'pathinfo ' => $ pathinfo ,
64
91
];
92
+ $ this ->FILES [$ prop ] = $ uploadfile ;
93
+
94
+ // Checks: Dateityp
95
+ if (!in_array (strtolower ($ pathinfo ['extension ' ]), $ this ->allowedFiletypes )) {
96
+ $ Errors [] = [
97
+ 'error ' => 'type ' ,
98
+ 'file ' => $ uploadfile
99
+ ];
100
+ }
101
+
102
+ // Dateigröße maximal
103
+ if ($ this ->maxFilesize != null && $ uploadfile ['size ' ] > $ this ->maxFilesize ) {
104
+ $ Errors [] = [
105
+ 'error ' => 'size ' ,
106
+ 'file ' => $ uploadfile
107
+ ];
108
+ }
109
+
65
110
}
66
111
}
67
- debug ( $ this -> FILES ) ;
112
+ return $ Errors ;
68
113
}
69
114
70
- public function execute ($ target_folder ){
71
- foreach ($ this ->FILES as $ file ){
72
- $ this ->move (
73
- $ file ['tmp_name ' ],
74
- $ target_folder ,
75
- $ file ['name ' ]
76
- );
77
- }
115
+ /**
116
+ * Get the list of files that will be uploaded
117
+ * @return array
118
+ */
119
+ public function getFiles () {
120
+ return $ this ->FILES ;
78
121
}
79
122
80
- private function move ($ tmp_file ,$ target_folder ,$ target_filename ){
81
- $ resourceFactory = ResourceFactory::getInstance ();
82
- $ storage = $ resourceFactory ->getDefaultStorage ();
83
- $ newFile = $ storage ->addFile (
84
- $ tmp_file ,
85
- $ storage ->getFolder ($ target_folder ),
86
- $ target_filename
87
- );
88
- return $ newFile ;
123
+ /**
124
+ * Upload files to target directory
125
+ * @param $target_folder
126
+ */
127
+ public function execute ($ target_folder = false ) {
128
+ $ results = [];
129
+ foreach ($ this ->FILES as $ file ) {
130
+ if ($ target_folder == false ){
131
+ $ results [] = GeneralUtility::upload_to_tempfile ($ file ['targetFilename ' ]);
132
+ }else {
133
+ $ results [] = GeneralUtility::upload_copy_move ($ file ['tmp_name ' ], PATH_site . '/fileadmin/ ' . $ target_folder . '/ ' . $ file ['targetFilename ' ]);
134
+
135
+ // Ordner neu indexieren
136
+ $ resourceFactory = \TYPO3 \CMS \Core \Resource \ResourceFactory::getInstance ();
137
+ $ defaultStorage = $ resourceFactory ->getDefaultStorage ();
138
+ $ folder = $ defaultStorage ->getFolder ($ target_folder );
139
+ $ files = $ defaultStorage ->getFilesInFolder ($ folder ); // Aufruf dieser Zeile sorgt automatisch für eine Reindexierung des Ordners
140
+ }
141
+
142
+ }
143
+ return $ results ;
89
144
}
90
145
91
146
}
0 commit comments