1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AppBundle\Service\File; |
4
|
|
|
|
5
|
|
|
use AppBundle\Entity\Event; |
6
|
|
|
use AppBundle\Entity\Image; |
7
|
|
|
use AppBundle\Entity\Repository\ImageRepository; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @author Vehsamrak |
11
|
|
|
*/ |
12
|
|
|
class FileService |
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** @var ImageRepository */ |
16
|
|
|
private $imageRepository; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $imagesPath; |
20
|
|
|
|
21
|
|
|
/** @var ImageExtensionChecker */ |
22
|
|
|
private $extensionChecker; |
23
|
|
|
|
24
|
6 |
|
public function __construct( |
25
|
|
|
string $applicationRootPath, |
26
|
|
|
ImageRepository $imageRepository, |
27
|
|
|
ImageExtensionChecker $extensionChecker |
28
|
|
|
) |
29
|
|
|
{ |
30
|
6 |
|
$this->imageRepository = $imageRepository; |
31
|
6 |
|
$this->imagesPath = $applicationRootPath . '/../var/upload/images'; |
32
|
|
|
|
33
|
6 |
|
if (!is_dir($this->imagesPath)) { |
34
|
1 |
|
mkdir($this->imagesPath, 0755, true); |
35
|
|
|
} |
36
|
6 |
|
$this->extensionChecker = $extensionChecker; |
37
|
6 |
|
} |
38
|
|
|
|
39
|
1 |
|
public function createBase64Image(string $fileName, string $fileContents, $entity): Image |
40
|
|
|
{ |
41
|
1 |
|
$image = new Image($fileName); |
42
|
1 |
|
$this->imageRepository->persist($image); |
43
|
|
|
|
44
|
1 |
|
$filePath = sprintf('%s%s%s', $this->imagesPath, DIRECTORY_SEPARATOR, $image->getName()); |
45
|
1 |
|
file_put_contents($filePath, base64_decode($fileContents)); |
46
|
|
|
|
47
|
1 |
|
if ($entity instanceof Event) { |
48
|
1 |
|
$entity->addImage($image); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$this->imageRepository->flush(); |
52
|
|
|
|
53
|
1 |
|
return $image; |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
public function getExtensionFromBase64File($imageContent): string |
57
|
|
|
{ |
58
|
1 |
|
return $this->extensionChecker->getExtensionFromBase64File($imageContent); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|