blob: 2dd4cbe22f5dad43945597af4f95312df91dd0fb (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
<?php
namespace Drupal\file\Upload;
use Drupal\file\FileInterface;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
* Value object for a file upload result.
*/
class FileUploadResult {
/**
* If the filename was renamed for security reasons.
*
* @var bool
*/
protected $securityRename = FALSE;
/**
* The sanitized filename.
*
* @var string
*/
protected $sanitizedFilename;
/**
* The original filename.
*
* @var string
*/
protected $originalFilename;
/**
* The File entity.
*
* @var \Drupal\file\FileInterface
*/
protected $file;
/**
* The constraint violations.
*
* @var \Symfony\Component\Validator\ConstraintViolationListInterface
*/
protected ConstraintViolationListInterface $violations;
/**
* Creates a new FileUploadResult.
*/
public function __construct() {
$this->violations = new ConstraintViolationList();
}
/**
* Flags the result as having had a security rename.
*
* @return $this
*/
public function setSecurityRename(): FileUploadResult {
$this->securityRename = TRUE;
return $this;
}
/**
* Sets the sanitized filename.
*
* @param string $sanitizedFilename
* The sanitized filename.
*
* @return $this
*/
public function setSanitizedFilename(string $sanitizedFilename): FileUploadResult {
$this->sanitizedFilename = $sanitizedFilename;
return $this;
}
/**
* Gets the original filename.
*
* @return string
* The original filename.
*/
public function getOriginalFilename(): string {
return $this->originalFilename;
}
/**
* Sets the original filename.
*
* @param string $originalFilename
* The original filename.
*
* @return $this
*/
public function setOriginalFilename(string $originalFilename): FileUploadResult {
$this->originalFilename = $originalFilename;
return $this;
}
/**
* Sets the File entity.
*
* @param \Drupal\file\FileInterface $file
* A file entity.
*
* @return $this
*/
public function setFile(FileInterface $file): FileUploadResult {
$this->file = $file;
return $this;
}
/**
* Returns if there was a security rename.
*
* @return bool
* TRUE when the file was renamed for a security reason, FALSE otherwise.
*/
public function isSecurityRename(): bool {
return $this->securityRename;
}
/**
* Returns if there was a file rename.
*
* @return bool
* TRUE when the file was renamed, FALSE otherwise.
*/
public function isRenamed(): bool {
return $this->originalFilename !== $this->sanitizedFilename;
}
/**
* Gets the sanitized filename.
*
* @return string
* The sanitized filename.
*/
public function getSanitizedFilename(): string {
return $this->sanitizedFilename;
}
/**
* Gets the File entity.
*
* @return \Drupal\file\FileInterface
* The file entity.
*/
public function getFile(): FileInterface {
return $this->file;
}
/**
* Adds a constraint violation.
*
* @param \Symfony\Component\Validator\ConstraintViolationInterface $violation
* The constraint violation.
*/
public function addViolation(ConstraintViolationInterface $violation): void {
$this->violations->add($violation);
}
/**
* Adds constraint violations.
*
* @param \Symfony\Component\Validator\ConstraintViolationListInterface $violations
* The constraint violations.
*/
public function addViolations(ConstraintViolationListInterface $violations): void {
$this->violations->addAll($violations);
}
/**
* Gets the constraint violations.
*
* @return \Symfony\Component\Validator\ConstraintViolationListInterface
* The constraint violations.
*/
public function getViolations(): ConstraintViolationListInterface {
return $this->violations;
}
/**
* Returns TRUE if there are constraint violations.
*/
public function hasViolations(): bool {
return $this->violations->count() > 0;
}
}
|