This repository was archived by the owner on Jun 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathMediaFactory.php
More file actions
204 lines (173 loc) · 7.55 KB
/
Copy pathMediaFactory.php
File metadata and controls
204 lines (173 loc) · 7.55 KB
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
194
195
196
197
198
199
200
201
202
203
204
<?php
namespace Foolz\FoolFuuka\Model;
use Foolz\FoolFrame\Model\Model;
use Foolz\Plugin\Hook;
use Foolz\Plugin\Plugsuit;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\Request;
class MediaFactory extends Model
{
use Plugsuit;
public function __construct(\Foolz\FoolFrame\Model\Context $context)
{
parent::__construct($context);
$this->dc = $context->getService('doctrine');
}
/**
* Creates an empty Media object
*
* @param Radix $radix The Radix the Media will refer to
*
* @return \Foolz\FoolFuuka\Model\Media An empty Media object, with all the values unset
*/
public function forgeEmpty(Radix $radix)
{
$media = new \stdClass();
return new Media($this->getContext(), $media, $radix);
}
/**
* Return a Media object by a chosen column
*
* @param Radix $radix The Radix where the Media can be found
* @param string $where The database column to match on
* @param string $value The value searched for
* @param boolean $op If the object is for an opening post
*
* @return \Foolz\FoolFuuka\Model\MediaData The searched object
* @throws MediaNotFoundException If the media has not been found
*/
protected function p_getBy(Radix $radix, $where, $value, $op = true)
{
$result = $this->dc->qb()
->select('*')
->from($radix->getTable('_images'), 'ri')
->where($this->dc->getConnection()->quoteIdentifier($where).' = '.$this->dc->getConnection()->quote($value))
->execute()
->fetch();
if ($result) {
$md = new MediaData();
$md->import($result);
return $md;
}
throw new MediaNotFoundException(_i('The image could not be found.'));
}
/**
* Return a Media object by the media_id column
*
* @param Radix $radix The Radix where the Media can be found
* @param string $value The media ID
* @param boolean $op If the object is for an opening post
*
* @return \Foolz\FoolFuuka\Model\Media The searched object
* @throws MediaNotFoundException If the media has not been found
*/
protected function p_getByMediaId(Radix $radix, $value, $op = false)
{
return $this->getBy($radix, 'media_id', $value, $op);
}
/**
* Return a Media object by the media_hash column
*
* @param Radix $radix The Radix where the Media can be found
* @param string $value The media hash
* @param boolean $op If the object is for an opening post
*
* @return \Foolz\FoolFuuka\Model\Media The searched object
* @throws MediaNotFoundException If the media has not been found
*/
protected function p_getByMediaHash(Radix $radix, $value, $op = false)
{
return $this->getBy($radix, 'media_hash', $value, $op);
}
/**
* Return a Media object by the media_hash column
*
* @param Radix $radix The Radix where the Media can be found
* @param string $value The filename
*
* @return \Foolz\FoolFuuka\Model\MediaData The searched object
* @throws MediaNotFoundException If the media has not been found
*/
protected function p_getByFilename(Radix $radix, $filename)
{
$result = $this->dc->qb()
->select('*')
->from($radix->getTable(), 'r')
->leftJoin('r', $radix->getTable('_images'), 'mg', 'mg.media_id = r.media_id')
->where('r.media_orig = :media_orig')
->setParameter(':media_orig', $filename)
->execute()
->fetch();
if ($result) {
$bulk = new CommentBulk();
$bulk->import($result, $radix);
return $bulk;
}
throw new MediaNotFoundException;
}
/**
* Takes an uploaded file and makes an object. It doesn't do the ->insert()
*
* @param Radix $radix The Radix where this Media belongs
*
* @return \Foolz\FoolFuuka\Model\Media A new Media object with the upload data
* @throws MediaUploadNoFileException If there's no file uploaded
* @throws MediaUploadMultipleNotAllowedException If there's multiple uploads
* @throws MediaUploadInvalidException If the file format is not allowed
*/
protected function p_forgeFromUpload(Request $request, Radix $radix)
{
$config = Hook::forge('Foolz\FoolFuuka\Model\MediaFactory::forgeFromUpload#var.config')
->setParams([
'ext_whitelist' => ['jpg', 'jpeg', 'gif', 'png'],
'mime_whitelist' => ['image/jpeg', 'image/png', 'image/gif']
])
->execute()
->getParams();
if (!$request->files->count())
{
throw new MediaUploadNoFileException(_i('You must upload an image or your image was too large.'));
}
if ($request->files->count() !== 1) {
throw new MediaUploadMultipleNotAllowedException(_i('You can\'t upload multiple images.'));
}
/** @var UploadedFile[] $files */
$files = $request->files->all();
$max_size = $radix->getValue('max_image_size_kilobytes') * 1024;
foreach ($files as $file) {
if (!$file->isValid()) {
if ($file->getError() === UPLOAD_ERR_INI_SIZE) {
throw new MediaUploadInvalidException(
_i('The server is misconfigured: the FoolFuuka upload size should be lower than PHP\'s upload limit.'));
}
if ($file->getError() === UPLOAD_ERR_PARTIAL) {
throw new MediaUploadInvalidException(_i('You uploaded the file partially.'));
}
if ($file->getError() === UPLOAD_ERR_CANT_WRITE) {
throw new MediaUploadInvalidException(_i('The image couldn\'t be saved on the disk.'));
}
if ($file->getError() === UPLOAD_ERR_EXTENSION) {
throw new MediaUploadInvalidException(_i('A PHP extension broke and made processing the image impossible.'));
}
throw new MediaUploadInvalidException(_i('Unexpected upload error.'));
}
if (mb_strlen($file->getFilename(), 'utf-8') > 64) {
throw new MediaUploadInvalidException(_i('You uploaded a file with a too long filename.'));
}
if (!in_array(strtolower($file->getClientOriginalExtension()), $config['ext_whitelist'])) {
throw new MediaUploadInvalidException(_i('You uploaded a file with an invalid extension.'));
}
if (!in_array(strtolower($file->getMimeType()), $config['mime_whitelist'])) {
throw new MediaUploadInvalidException(_i('You uploaded a file with an invalid mime type.'));
}
if ($file->getClientSize() > $max_size && !$this->getAuth()->hasAccess('media.limitless_media')) {
throw new MediaUploadInvalidException(
_i('You uploaded a too big file. The maxmimum allowed filesize is %s',
$radix->getValue('max_image_size_kilobytes')));
}
}
$media = new Media($this->getContext());
$media->setTempFile($radix, $files['file_image']);
return $media;
}
}