|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Controller\Admin; |
|
4
|
|
|
|
|
5
|
|
|
use App\Entity\Image; |
|
6
|
|
|
use App\Form\ImageType; |
|
7
|
|
|
use App\Form\DropzoneImageType; |
|
8
|
|
|
use App\Repository\ImageRepository; |
|
9
|
|
|
use Knp\Component\Pager\PaginatorInterface; |
|
10
|
|
|
use Liip\ImagineBundle\Imagine\Cache\CacheManager; |
|
11
|
|
|
use Liip\ImagineBundle\Imagine\Filter\FilterManager; |
|
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
17
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
|
18
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
19
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
|
20
|
|
|
use Symfony\Component\Serializer\Serializer; |
|
21
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
22
|
|
|
use Vich\UploaderBundle\Form\Type\VichImageType; |
|
23
|
|
|
use Vich\UploaderBundle\Templating\Helper\UploaderHelper; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @Route("/admin/images", name="admin_images_") |
|
27
|
|
|
*/ |
|
28
|
|
|
class ImageController extends AbstractController |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @Route("/", name="index", methods={"GET"}) |
|
32
|
|
|
*/ |
|
33
|
|
|
public function index(ImageRepository $imageRepository, PaginatorInterface $paginator, Request $request): Response |
|
34
|
|
|
{ |
|
35
|
|
|
$query = $imageRepository |
|
36
|
|
|
->createQueryBuilder('i') |
|
37
|
|
|
// Nice orphan check: ->where('i.wanders is empty') |
|
38
|
|
|
->orderBy('i.capturedAt', 'DESC') |
|
39
|
|
|
->getQuery(); |
|
40
|
|
|
|
|
41
|
|
|
$pagination = $paginator->paginate( |
|
42
|
|
|
$query, |
|
43
|
|
|
$request->query->getInt('page', 1), |
|
44
|
|
|
10); |
|
45
|
|
|
|
|
46
|
|
|
return $this->render('admin/image/index.html.twig', [ |
|
47
|
|
|
'pagination' => $pagination |
|
48
|
|
|
]); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @Route("/cluster", name="cluster", methods={"GET"}) |
|
53
|
|
|
*/ |
|
54
|
|
|
public function cluster() |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->render('admin/image/cluster.html.twig', [ |
|
57
|
|
|
]); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @Route("/upload", name="upload", methods={"GET", "POST"}) |
|
62
|
|
|
*/ |
|
63
|
|
|
public function upload( |
|
64
|
|
|
Request $request, |
|
65
|
|
|
SerializerInterface $serializer, |
|
66
|
|
|
string $gpxDirectory // TODO Fix this; it should be using the image uploads directory |
|
67
|
|
|
): Response |
|
68
|
|
|
{ |
|
69
|
|
|
if ($request->isMethod('POST')) { |
|
70
|
|
|
|
|
71
|
|
|
$token = $request->request->get('token'); |
|
72
|
|
|
if (!$this->isCsrfTokenValid('image_upload', $token)) { |
|
73
|
|
|
return $this->json([ 'error' => 'Invalid CSRF token'], 401); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$file = $request->files->get('file'); |
|
77
|
|
|
|
|
78
|
|
|
if ($file instanceof UploadedFile) { |
|
79
|
|
|
$image = new Image(); |
|
80
|
|
|
$image->setImageFile($file); |
|
81
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
82
|
|
|
$entityManager->persist($image); |
|
83
|
|
|
$entityManager->flush(); |
|
84
|
|
|
// It's not exactly an API response, but it'll do until we switch to handling this |
|
85
|
|
|
// a bit more properly. At least it's a JSON repsonse and *doesn't include the entire |
|
86
|
|
|
// file we just uploaded*, thanks to the IGNORED_ATTRIBUTES. Because we set up the |
|
87
|
|
|
// image URIs in a postPersist event listener, this also contains everything you'd |
|
88
|
|
|
// need to build an image in HTML. |
|
89
|
|
|
return new JsonResponse($serializer->serialize($image, 'jsonld', [AbstractNormalizer::IGNORED_ATTRIBUTES => ['imageFile']]), 201,[], true); |
|
90
|
|
|
} else { |
|
91
|
|
|
throw new HttpException(500, "No uploaded file found."); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
else |
|
95
|
|
|
{ |
|
96
|
|
|
$disk = []; |
|
97
|
|
|
$disk['free'] = disk_free_space($gpxDirectory); |
|
98
|
|
|
$disk['total'] = disk_total_space($gpxDirectory); |
|
99
|
|
|
$disk['used'] = $disk['total'] - $disk['free']; |
|
100
|
|
|
$disk['percent'] = $disk['used'] / $disk['total']; |
|
101
|
|
|
return $this->render('admin/image/upload.html.twig', [ |
|
102
|
|
|
'disk' => $disk |
|
103
|
|
|
]); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @Route("/{id}", name="show", methods={"GET"}) |
|
109
|
|
|
*/ |
|
110
|
|
|
public function show(Image $image): Response |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->render('/admin/image/show.html.twig', [ |
|
113
|
|
|
'image' => $image, |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @Route("/{id}/edit", name="edit", methods={"GET","POST"}) |
|
119
|
|
|
*/ |
|
120
|
|
|
public function edit(Request $request, Image $image): Response |
|
121
|
|
|
{ |
|
122
|
|
|
$form = $this->createForm(ImageType::class, $image); |
|
123
|
|
|
$form->handleRequest($request); |
|
124
|
|
|
if ($form->isSubmitted() && $form->isValid()) { |
|
125
|
|
|
$this->getDoctrine()->getManager()->flush(); |
|
126
|
|
|
//dd($form); |
|
127
|
|
|
return $this->redirectToRoute('admin_images_show', ['id' => $image->getId()]); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $this->render('admin/image/edit.html.twig', [ |
|
131
|
|
|
'image' => $image, |
|
132
|
|
|
'form' => $form->createView(), |
|
133
|
|
|
]); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* @Route("/{id}", name="delete", methods={"DELETE"}) |
|
138
|
|
|
*/ |
|
139
|
|
|
public function delete(Request $request, Image $image): Response |
|
140
|
|
|
{ |
|
141
|
|
|
if ($this->isCsrfTokenValid('delete'.$image->getId(), $request->request->get('_token'))) { |
|
142
|
|
|
$entityManager = $this->getDoctrine()->getManager(); |
|
143
|
|
|
$entityManager->remove($image); |
|
144
|
|
|
$entityManager->flush(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $this->redirectToRoute('admin_images_index'); |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|