1 | <?php |
||
2 | |||
3 | /* |
||
4 | * @copyright 2014 Mautic Contributors. All rights reserved |
||
5 | * @author Mautic |
||
6 | * |
||
7 | * @link http://mautic.org |
||
8 | * |
||
9 | * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html |
||
10 | */ |
||
11 | |||
12 | namespace Mautic\AssetBundle\Controller\Api; |
||
13 | |||
14 | use Mautic\ApiBundle\Controller\CommonApiController; |
||
15 | use Symfony\Component\HttpFoundation\Response; |
||
16 | use Symfony\Component\HttpKernel\Event\FilterControllerEvent; |
||
17 | |||
18 | /** |
||
19 | * Class AssetApiController. |
||
20 | */ |
||
21 | class AssetApiController extends CommonApiController |
||
22 | { |
||
23 | public function initialize(FilterControllerEvent $event) |
||
24 | { |
||
25 | $this->model = $this->getModel('asset'); |
||
26 | $this->entityClass = 'Mautic\AssetBundle\Entity\Asset'; |
||
27 | $this->entityNameOne = 'asset'; |
||
28 | $this->entityNameMulti = 'assets'; |
||
29 | $this->serializerGroups = ['assetDetails', 'categoryList', 'publishDetails']; |
||
30 | |||
31 | parent::initialize($event); |
||
32 | } |
||
33 | |||
34 | /** |
||
35 | * Gives child controllers opportunity to analyze and do whatever to an entity before going through serializer. |
||
36 | * |
||
37 | * @param $entity |
||
38 | * @param string $action |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | protected function preSerializeEntity(&$entity, $action = 'view') |
||
43 | { |
||
44 | $entity->setDownloadUrl( |
||
45 | $this->model->generateUrl($entity, true) |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
46 | ); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Convert posted parameters into what the form needs in order to successfully bind. |
||
51 | * |
||
52 | * @param $parameters |
||
53 | * @param $entity |
||
54 | * @param $action |
||
55 | * |
||
56 | * @return mixed |
||
57 | */ |
||
58 | protected function prepareParametersForBinding($parameters, $entity, $action) |
||
59 | { |
||
60 | $assetDir = $this->get('mautic.helper.core_parameters')->get('upload_dir'); |
||
61 | $entity->setUploadDir($assetDir); |
||
62 | |||
63 | if (isset($parameters['file'])) { |
||
64 | if ('local' === $parameters['storageLocation']) { |
||
65 | $entity->setPath($parameters['file']); |
||
66 | $entity->setFileInfoFromFile(); |
||
67 | |||
68 | if (null === $entity->loadFile()) { |
||
69 | return $this->returnError('File '.$parameters['file'].' was not found in the asset directory.', Response::HTTP_BAD_REQUEST); |
||
70 | } |
||
71 | } elseif ('remote' === $parameters['storageLocation']) { |
||
72 | $parameters['remotePath'] = $parameters['file']; |
||
73 | $entity->setTitle($parameters['title']); |
||
74 | $entity->setStorageLocation('remote'); |
||
75 | $entity->setRemotePath($parameters['remotePath']); |
||
76 | $entity->preUpload(); |
||
77 | $entity->upload(); |
||
78 | } |
||
79 | |||
80 | unset($parameters['file']); |
||
81 | } elseif ('new' === $action) { |
||
82 | return $this->returnError('File of the asset is required.', Response::HTTP_BAD_REQUEST); |
||
83 | } |
||
84 | |||
85 | return $parameters; |
||
86 | } |
||
87 | } |
||
88 |