1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Superdesk Web Publisher Content Bundle. |
5
|
|
|
* |
6
|
|
|
* Copyright 2016 Sourcefabric z.ú. and contributors. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please see the |
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @copyright 2016 Sourcefabric z.ú |
12
|
|
|
* @license http://www.superdesk.org/license |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace SWP\Bundle\ContentBundle\Manager; |
16
|
|
|
|
17
|
|
|
use SWP\Bundle\ContentBundle\Doctrine\ArticleMediaRepositoryInterface; |
18
|
|
|
use SWP\Bundle\ContentBundle\Factory\MediaFactoryInterface; |
19
|
|
|
use SWP\Bundle\ContentBundle\Model\ArticleMedia; |
20
|
|
|
use SWP\Bundle\ContentBundle\Model\FileInterface; |
21
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
22
|
|
|
use League\Flysystem\Filesystem; |
23
|
|
|
use Symfony\Component\Routing\RouterInterface; |
24
|
|
|
|
25
|
|
|
class MediaManager implements MediaManagerInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var MediaFactoryInterface |
29
|
|
|
*/ |
30
|
|
|
protected $mediaFactory; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var Filesystem |
34
|
|
|
*/ |
35
|
|
|
protected $filesystem; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var RouterInterface |
39
|
|
|
*/ |
40
|
|
|
protected $router; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ArticleMediaRepositoryInterface |
44
|
|
|
*/ |
45
|
|
|
protected $mediaRepository; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* MediaManager constructor. |
49
|
|
|
* |
50
|
|
|
* @param ArticleMediaRepositoryInterface $mediaRepository |
51
|
|
|
* @param MediaFactoryInterface $mediaFactory |
52
|
|
|
* @param Filesystem $filesystem |
53
|
|
|
* @param RouterInterface $router |
54
|
|
|
*/ |
55
|
8 |
|
public function __construct( |
56
|
|
|
ArticleMediaRepositoryInterface $mediaRepository, |
57
|
|
|
MediaFactoryInterface $mediaFactory, |
58
|
|
|
Filesystem $filesystem, |
59
|
|
|
RouterInterface $router |
60
|
|
|
) { |
61
|
8 |
|
$this->mediaRepository = $mediaRepository; |
62
|
8 |
|
$this->mediaFactory = $mediaFactory; |
63
|
8 |
|
$this->filesystem = $filesystem; |
64
|
8 |
|
$this->router = $router; |
65
|
8 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function handleUploadedFile(UploadedFile $uploadedFile, $mediaId) |
71
|
|
|
{ |
72
|
|
|
$mediaId = ArticleMedia::handleMediaId($mediaId); |
73
|
|
|
$this->saveFile($uploadedFile, $mediaId); |
74
|
|
|
|
75
|
|
|
$asset = $this->mediaFactory->createMediaAsset($uploadedFile, $mediaId); |
76
|
|
|
$this->mediaRepository->add($asset); |
77
|
|
|
|
78
|
|
|
return $asset; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function getFile(FileInterface $media) |
85
|
|
|
{ |
86
|
|
|
return $this->filesystem->read($this->getMediaBasePath().'/'.$media->getAssetId().'.'.$media->getFileExtension()); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* {@inheritdoc} |
91
|
|
|
*/ |
92
|
|
|
public function saveFile(UploadedFile $uploadedFile, $fileName) |
93
|
|
|
{ |
94
|
|
|
$stream = fopen($uploadedFile->getRealPath(), 'r+'); |
95
|
|
|
$result = $this->filesystem->writeStream($this->getMediaBasePath().'/'.$fileName.'.'.$uploadedFile->guessClientExtension(), $stream); |
96
|
|
|
fclose($stream); |
97
|
|
|
|
98
|
|
|
return $result; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
|
|
public function getMediaPublicUrl(FileInterface $media) |
105
|
|
|
{ |
106
|
|
|
return $this->getMediaUri($media, RouterInterface::ABSOLUTE_URL); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
|
|
public function getMediaUri(FileInterface $media, $type = RouterInterface::ABSOLUTE_PATH) |
113
|
|
|
{ |
114
|
|
|
return $this->router->generate('swp_media_get', [ |
115
|
|
|
'mediaId' => $media->getAssetId(), |
116
|
|
|
'extension' => $media->getFileExtension(), |
117
|
|
|
], $type); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
protected function getMediaBasePath(): string |
121
|
|
|
{ |
122
|
|
|
$pathElements = ['swp', 'media']; |
123
|
|
|
|
124
|
|
|
return implode('/', $pathElements); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|