|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please see the |
|
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
12
|
|
|
* |
|
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
|
14
|
|
|
* @license http://www.superdesk.org/license |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ContentBundle\Controller; |
|
18
|
|
|
|
|
19
|
|
|
use Hoa\Mime\Mime; |
|
20
|
|
|
use SWP\Component\Bridge\Events; |
|
21
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
|
22
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
|
23
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
|
24
|
|
|
use SWP\Bundle\ContentBundle\Form\Type\MediaFileType; |
|
25
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMedia; |
|
26
|
|
|
use SWP\Bundle\ContentBundle\Provider\FileProvider; |
|
27
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
|
28
|
|
|
use SWP\Component\Common\Response\SingleResourceResponse; |
|
29
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
|
30
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
31
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
32
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
|
33
|
|
|
|
|
34
|
|
|
class ContentPushController extends Controller |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* Receives HTTP Push Request's payload. |
|
38
|
|
|
* |
|
39
|
|
|
* @ApiDoc( |
|
40
|
|
|
* resource=true, |
|
41
|
|
|
* description="Adds a new content from HTTP Push", |
|
42
|
|
|
* statusCodes={ |
|
43
|
|
|
* 201="Returned on success" |
|
44
|
|
|
* } |
|
45
|
|
|
* ) |
|
46
|
|
|
* @Route("/api/{version}/content/push", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_content_push") |
|
47
|
|
|
* @Method("POST") |
|
48
|
|
|
*/ |
|
49
|
|
|
public function pushContentAction(Request $request) |
|
50
|
12 |
|
{ |
|
51
|
|
|
$package = $this->container->get('swp_bridge.transformer.json_to_package')->transform($request->getContent()); |
|
52
|
12 |
|
$this->container->get('event_dispatcher')->dispatch(Events::SWP_VALIDATION, new GenericEvent($package)); |
|
53
|
12 |
|
|
|
54
|
|
|
$payload = \serialize([ |
|
55
|
12 |
|
'package' => $package, |
|
56
|
|
|
'tenant' => $this->container->get('swp_multi_tenancy.tenant_context')->getTenant(), |
|
57
|
12 |
|
]); |
|
58
|
|
|
$this->container->get('old_sound_rabbit_mq.content_push_producer')->publish($payload); |
|
59
|
|
|
|
|
60
|
|
|
return new SingleResourceResponse(['status' => 'OK'], new ResponseContext(201)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Receives HTTP Push Request's assets payload which is then processed and stored. |
|
65
|
|
|
* |
|
66
|
12 |
|
* @ApiDoc( |
|
67
|
12 |
|
* resource=true, |
|
68
|
11 |
|
* description="Adds new assets from HTTP Push", |
|
69
|
11 |
|
* statusCodes={ |
|
70
|
|
|
* 201="Returned on successful post.", |
|
71
|
11 |
|
* 500="Returned on invalid file.", |
|
72
|
|
|
* 200="Returned on form errors" |
|
73
|
|
|
* }, |
|
74
|
|
|
* input="SWP\Bundle\ContentBundle\Form\Type\MediaFileType" |
|
75
|
|
|
* ) |
|
76
|
|
|
* @Route("/api/{version}/assets/push", options={"expose"=true}, defaults={"version"="v1"}, name="swp_api_assets_push") |
|
77
|
|
|
* @Method("POST") |
|
78
|
|
|
*/ |
|
79
|
|
|
public function pushAssetsAction(Request $request) |
|
80
|
|
|
{ |
|
81
|
|
|
$form = $this->createForm(MediaFileType::class); |
|
82
|
|
|
$form->handleRequest($request); |
|
83
|
|
|
|
|
84
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
85
|
|
|
$mediaManager = $this->get('swp_content_bundle.manager.media'); |
|
86
|
|
|
$uploadedFile = $form->getData()['media']; |
|
87
|
|
|
$mediaId = $request->request->get('media_id'); |
|
88
|
|
|
|
|
89
|
|
|
if ($uploadedFile->isValid()) { |
|
90
|
|
|
$fileProvider = $this->container->get(FileProvider::class); |
|
91
|
|
|
$file = $fileProvider->getFile(ArticleMedia::handleMediaId($mediaId), $uploadedFile->guessExtension()); |
|
92
|
|
|
|
|
93
|
|
|
if (null === $file) { |
|
94
|
|
|
$file = $mediaManager->handleUploadedFile($uploadedFile, $mediaId); |
|
95
|
|
|
$this->get('swp.object_manager.media')->flush(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return new SingleResourceResponse( |
|
99
|
|
|
[ |
|
100
|
|
|
'media_id' => $mediaId, |
|
101
|
|
|
'URL' => $mediaManager->getMediaPublicUrl($file), |
|
102
|
|
|
'media' => base64_encode($mediaManager->getFile($file)), |
|
103
|
|
|
'mime_type' => Mime::getMimeFromExtension($file->getFileExtension()), |
|
104
|
|
|
'filemeta' => [], |
|
105
|
|
|
], new ResponseContext(201) |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
throw new \Exception('Uploaded file is not valid:'.$uploadedFile->getErrorMessage()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return new SingleResourceResponse($form); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Checks if media exists in storage. |
|
117
|
|
|
* |
|
118
|
|
|
* @ApiDoc( |
|
119
|
|
|
* resource=true, |
|
120
|
|
|
* description="Gets a single media file", |
|
121
|
|
|
* statusCodes={ |
|
122
|
|
|
* 404="Returned when file doesn't exist.", |
|
123
|
|
|
* 200="Returned on form errors" |
|
124
|
|
|
* } |
|
125
|
|
|
* ) |
|
126
|
|
|
* @Route("/api/{version}/assets/push/{mediaId}", options={"expose"=true}, defaults={"version"="v1"}, requirements={"mediaId"=".+"}, name="swp_api_assets_get") |
|
127
|
|
|
* @Route("/api/{version}/assets/get/{mediaId}", options={"expose"=true}, defaults={"version"="v1"}, requirements={"mediaId"=".+"}, name="swp_api_assets_get_1") |
|
128
|
|
|
* @Method("GET") |
|
129
|
|
|
*/ |
|
130
|
|
|
public function getAssetsAction(string $mediaId) |
|
131
|
|
|
{ |
|
132
|
|
|
$image = $this->get('swp.repository.image') |
|
133
|
|
|
->findImageByAssetId(ArticleMedia::handleMediaId($mediaId)); |
|
134
|
|
|
|
|
135
|
|
|
if (null === $image) { |
|
136
|
|
|
throw new NotFoundHttpException('Media don\'t exist in storage'); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
$mediaManager = $this->get('swp_content_bundle.manager.media'); |
|
140
|
|
|
|
|
141
|
|
|
return new SingleResourceResponse( |
|
142
|
|
|
[ |
|
143
|
|
|
'media_id' => $mediaId, |
|
144
|
|
|
'URL' => $mediaManager->getMediaPublicUrl($image), |
|
145
|
|
|
'media' => base64_encode($mediaManager->getFile($image)), |
|
146
|
|
|
'mime_type' => Mime::getMimeFromExtension($image->getFileExtension()), |
|
147
|
|
|
'filemeta' => [], |
|
148
|
|
|
] |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|