1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backend\Modules\MediaLibrary\Ajax; |
4
|
|
|
|
5
|
|
|
use Backend\Core\Engine\Authentication as BackendAuthentication; |
6
|
|
|
use Backend\Core\Engine\Base\AjaxAction as BackendBaseAJAXAction; |
7
|
|
|
use Backend\Core\Language\Language; |
8
|
|
|
use Backend\Modules\MediaLibrary\Domain\MediaFolder\Exception\MediaFolderNotFound; |
9
|
|
|
use Backend\Modules\MediaLibrary\Domain\MediaItem\Command\CreateMediaItemFromMovieUrl; |
10
|
|
|
use Backend\Modules\MediaLibrary\Domain\MediaFolder\MediaFolder; |
11
|
|
|
use Backend\Modules\MediaLibrary\Domain\MediaItem\StorageType; |
12
|
|
|
use Common\Exception\AjaxExitException; |
13
|
|
|
use InvalidArgumentException; |
14
|
|
|
use Symfony\Component\HttpFoundation\Response; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* This AJAX-action will add a new MediaItem movie. |
18
|
|
|
*/ |
19
|
|
|
class MediaItemAddMovie extends BackendBaseAJAXAction |
20
|
|
|
{ |
21
|
|
|
public function execute(): void |
22
|
|
|
{ |
23
|
|
|
parent::execute(); |
24
|
|
|
|
25
|
|
|
/** @var CreateMediaItemFromMovieUrl $createMediaItemFromMovieUrl */ |
26
|
|
|
$createMediaItemFromMovieUrl = $this->createMovieMediaItem(); |
27
|
|
|
|
28
|
|
|
// Output success message |
29
|
|
|
$this->output( |
30
|
|
|
Response::HTTP_OK, |
31
|
|
|
$createMediaItemFromMovieUrl->getMediaItem(), |
32
|
|
|
Language::msg('MediaUploadedSuccessful') |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
private function createMovieMediaItem(): CreateMediaItemFromMovieUrl |
37
|
|
|
{ |
38
|
|
|
/** @var CreateMediaItemFromMovieUrl $createMediaItem */ |
39
|
|
|
$createMediaItemFromMovieUrl = new CreateMediaItemFromMovieUrl( |
40
|
|
|
$this->getMovieStorageType(), |
41
|
|
|
$this->getMovieId(), |
42
|
|
|
$this->getMovieTitle(), |
43
|
|
|
$this->getMediaFolder(), |
44
|
|
|
BackendAuthentication::getUser()->getUserId() |
45
|
|
|
); |
46
|
|
|
|
47
|
|
|
// Handle the MediaItem create |
48
|
|
|
$this->get('command_bus')->handle($createMediaItemFromMovieUrl); |
49
|
|
|
|
50
|
|
|
return $createMediaItemFromMovieUrl; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getMovieId(): string |
54
|
|
|
{ |
55
|
|
|
$movieId = trim($this->getRequest()->request->get('id')); |
56
|
|
|
|
57
|
|
|
// Movie id not null |
58
|
|
|
if (empty($movieId)) { |
59
|
|
|
throw new AjaxExitException(Language::err('MediaMovieIdIsRequired')); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Movie url (= externalVideoId) already exists in our repository |
63
|
|
|
if ($this->get('media_library.repository.item')->existsOneByUrl((string) $movieId)) { |
64
|
|
|
throw new AjaxExitException(Language::err('MediaMovieIdAlreadyExists')); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Make sure the movie id is valid |
68
|
|
|
if (!preg_match('/^[a-zA-Z0-9._-]+$/', (string) $movieId)) { |
69
|
|
|
throw new AjaxExitException(Language::err('InvalidValue')); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $movieId; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
protected function getMovieTitle(): string |
76
|
|
|
{ |
77
|
|
|
$movieTitle = trim($this->getRequest()->request->get('title')); |
78
|
|
|
|
79
|
|
|
// Title not valid |
80
|
|
|
if (empty($movieTitle)) { |
81
|
|
|
throw new AjaxExitException(Language::err('MediaMovieTitleIsRequired')); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $movieTitle; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected function getMediaFolder(): MediaFolder |
88
|
|
|
{ |
89
|
|
|
$id = $this->getRequest()->request->getInt('folder_id'); |
90
|
|
|
|
91
|
|
|
if ($id === 0) { |
92
|
|
|
throw new AjaxExitException(Language::err('MediaFolderIsRequired')); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
|
|
/** @var MediaFolder */ |
97
|
|
|
return $this->get('media_library.repository.folder')->findOneById($id); |
98
|
|
|
} catch (MediaFolderNotFound $mediaFolderNotFound) { |
99
|
|
|
throw new AjaxExitException(Language::err('ParentNotExists')); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function getMovieStorageType(): StorageType |
104
|
|
|
{ |
105
|
|
|
$movieStorageType = $this->getRequest()->request->get('storageType'); |
106
|
|
|
|
107
|
|
|
if ($movieStorageType === null || !in_array( |
108
|
|
|
(string) $movieStorageType, |
109
|
|
|
StorageType::POSSIBLE_VALUES_FOR_MOVIE, |
110
|
|
|
true |
111
|
|
|
)) { |
112
|
|
|
throw new AjaxExitException(Language::err('MovieStorageTypeNotExists')); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
try { |
116
|
|
|
return StorageType::fromString($movieStorageType); |
117
|
|
|
} catch (InvalidArgumentException $invalidArgumentException) { |
118
|
|
|
throw new AjaxExitException(Language::err('MovieStorageTypeNotExists')); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|