|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace VideoGamesRecords\CoreBundle\Controller\PlayerChart; |
|
4
|
|
|
|
|
5
|
|
|
use Aws\S3\S3Client; |
|
6
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
7
|
|
|
use Exception; |
|
8
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
12
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Picture; |
|
13
|
|
|
use VideoGamesRecords\CoreBundle\Entity\PlayerChart; |
|
14
|
|
|
use VideoGamesRecords\CoreBundle\Entity\PlayerChartStatus; |
|
15
|
|
|
use VideoGamesRecords\CoreBundle\Entity\Proof; |
|
16
|
|
|
use VideoGamesRecords\CoreBundle\Exception\AccessDeniedException; |
|
17
|
|
|
use VideoGamesRecords\CoreBundle\Security\UserProvider; |
|
18
|
|
|
|
|
19
|
|
|
class SendPicture extends AbstractController |
|
20
|
|
|
{ |
|
21
|
|
|
private S3Client $s3client; |
|
22
|
|
|
private UserProvider $userProvider; |
|
23
|
|
|
private EntityManagerInterface $em; |
|
24
|
|
|
|
|
25
|
|
|
private array $extensions = array( |
|
26
|
|
|
'image/png' => '.png', |
|
27
|
|
|
'image/jpeg' => '.jpg', |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
public function __construct( |
|
31
|
|
|
S3Client $s3client, |
|
32
|
|
|
UserProvider $userProvider, |
|
33
|
|
|
EntityManagerInterface $em |
|
34
|
|
|
) { |
|
35
|
|
|
$this->s3client = $s3client; |
|
36
|
|
|
$this->userProvider = $userProvider; |
|
37
|
|
|
$this->em = $em; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @param PlayerChart $playerChart |
|
42
|
|
|
* @param Request $request |
|
43
|
|
|
* @return Response |
|
44
|
|
|
* @throws Exception |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __invoke(PlayerChart $playerChart, Request $request): Response |
|
47
|
|
|
{ |
|
48
|
|
|
$player = $this->userProvider->getPlayer(); |
|
49
|
|
|
|
|
50
|
|
|
if ($playerChart->getPlayer() !== $player) { |
|
51
|
|
|
throw new AccessDeniedException('ACESS DENIED'); |
|
52
|
|
|
} |
|
53
|
|
|
if (!in_array($playerChart->getStatus()->getId(), PlayerChartStatus::getStatusForProving())) { |
|
54
|
|
|
throw new AccessDeniedException('ACESS DENIED'); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$id = $playerChart->getId(); |
|
58
|
|
|
$idPlayer = $playerChart->getPlayer()->getId(); |
|
59
|
|
|
$idGame = $playerChart->getChart()->getGroup()->getGame()->getId(); |
|
60
|
|
|
|
|
61
|
|
|
$data = json_decode($request->getContent(), true); |
|
62
|
|
|
$file = $data['file']; |
|
63
|
|
|
|
|
64
|
|
|
$hash = hash_file('sha256', $file); |
|
65
|
|
|
$picture = $this->em->getRepository('VideoGamesRecords\CoreBundle\Entity\Picture')->findOneBy( |
|
66
|
|
|
array( |
|
67
|
|
|
'hash' => $hash, |
|
68
|
|
|
'player' => $playerChart->getPlayer(), |
|
69
|
|
|
'game' => $playerChart->getChart()->getGroup()->getGame(), |
|
70
|
|
|
) |
|
71
|
|
|
); |
|
72
|
|
|
|
|
73
|
|
|
if ($picture == null) { |
|
74
|
|
|
$fp = fopen($file, 'r'); |
|
75
|
|
|
$meta = stream_get_meta_data($fp); |
|
76
|
|
|
|
|
77
|
|
|
$metadata = [ |
|
78
|
|
|
'idplayer' => $idPlayer, |
|
79
|
|
|
'idgame' => $idGame, |
|
80
|
|
|
]; |
|
81
|
|
|
$key = $idPlayer . '/' . $idGame . '/' . uniqid() . $this->extensions[$meta['mediatype']]; |
|
82
|
|
|
|
|
83
|
|
|
$this->s3client->putObject([ |
|
84
|
|
|
'Bucket' => $_ENV['AWS_S3_BUCKET_PROOF'], |
|
85
|
|
|
'Key' => $key, |
|
86
|
|
|
'Body' => $fp, |
|
87
|
|
|
'ACL' => 'public-read', |
|
88
|
|
|
'ContentType' => $meta['mediatype'], |
|
89
|
|
|
'Metadata' => [ |
|
90
|
|
|
'idplayer' => $idPlayer, |
|
91
|
|
|
'idgame' => $idGame |
|
92
|
|
|
], |
|
93
|
|
|
'StorageClass' => 'STANDARD', |
|
94
|
|
|
]); |
|
95
|
|
|
|
|
96
|
|
|
//-- Picture |
|
97
|
|
|
$picture = new Picture(); |
|
98
|
|
|
$picture->setPath($key); |
|
99
|
|
|
$picture->setMetadata(serialize($metadata)); |
|
100
|
|
|
$picture->setPlayer($playerChart->getPlayer()); |
|
101
|
|
|
$picture->setGame($playerChart->getChart()->getGroup()->getGame()); |
|
102
|
|
|
$picture->setHash($hash); |
|
103
|
|
|
$this->em->persist($picture); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
|
|
107
|
|
|
//-- Proof |
|
108
|
|
|
$proof = new Proof(); |
|
109
|
|
|
$proof->setPicture($picture); |
|
110
|
|
|
$proof->setPlayer($playerChart->getPlayer()); |
|
111
|
|
|
$proof->setChart($playerChart->getChart()); |
|
112
|
|
|
$this->em->persist($proof); |
|
113
|
|
|
|
|
114
|
|
|
//-- PlayerChart |
|
115
|
|
|
$playerChart->setProof($proof); |
|
116
|
|
|
if ($playerChart->getStatus()->getId() === PlayerChartStatus::ID_STATUS_NORMAL) { |
|
117
|
|
|
// NORMAL TO NORMAL_SEND_PROOF |
|
118
|
|
|
$playerChart->setStatus( |
|
119
|
|
|
$this->em->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_NORMAL_SEND_PROOF) |
|
|
|
|
|
|
120
|
|
|
); |
|
121
|
|
|
} else { |
|
122
|
|
|
// INVESTIGATION TO DEMAND_SEND_PROOF |
|
123
|
|
|
$playerChart->setStatus( |
|
124
|
|
|
$this->em->getReference(PlayerChartStatus::class, PlayerChartStatus::ID_STATUS_DEMAND_SEND_PROOF) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
$this->em->flush(); |
|
128
|
|
|
|
|
129
|
|
|
return new JsonResponse([ |
|
130
|
|
|
'id' => $id, |
|
131
|
|
|
'file' => $file, |
|
132
|
|
|
], 200); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|