|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* AnimeDb package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Peter Gribanov <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
7
|
|
|
* @license http://opensource.org/licenses/GPL-3.0 GPL v3 |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace AnimeDb\Bundle\CatalogBundle\Controller; |
|
10
|
|
|
|
|
11
|
|
|
use AnimeDb\Bundle\CatalogBundle\Entity\Storage; |
|
12
|
|
|
use AnimeDb\Bundle\CatalogBundle\Repository\Storage as StorageRepository; |
|
13
|
|
|
use Symfony\Component\Form\Form; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
15
|
|
|
use AnimeDb\Bundle\CatalogBundle\Form\Type\Entity\Storage as StorageForm; |
|
16
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
17
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Storages. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Peter Gribanov <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class StorageController extends BaseController |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Link to guide, how add a new storage. |
|
28
|
|
|
* |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
const GUIDE_LINK = '/guide/storage/add.html'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Storage list. |
|
35
|
|
|
* |
|
36
|
|
|
* @param Request $request |
|
37
|
|
|
* |
|
38
|
|
|
* @return Response |
|
39
|
|
|
*/ |
|
40
|
|
|
public function listAction(Request $request) |
|
41
|
|
|
{ |
|
42
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse('AnimeDbCatalogBundle:Storage'); |
|
43
|
|
|
// response was not modified for this request |
|
44
|
|
|
if ($response->isNotModified($request)) { |
|
45
|
|
|
return $response; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/* @var $rep StorageRepository */ |
|
49
|
|
|
$rep = $this->getDoctrine()->getRepository('AnimeDbCatalogBundle:Storage'); |
|
50
|
|
|
|
|
51
|
|
|
return $this->render('AnimeDbCatalogBundle:Storage:list.html.twig', [ |
|
52
|
|
|
'storages' => $rep->getList(), |
|
53
|
|
|
], $response); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Change storage. |
|
58
|
|
|
* |
|
59
|
|
|
* @param Storage $storage |
|
60
|
|
|
* @param Request $request |
|
61
|
|
|
* |
|
62
|
|
|
* @return Response |
|
63
|
|
|
*/ |
|
64
|
|
|
public function changeAction(Storage $storage, Request $request) |
|
65
|
|
|
{ |
|
66
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse($storage->getDateUpdate()); |
|
67
|
|
|
// response was not modified for this request |
|
68
|
|
|
if ($response->isNotModified($request)) { |
|
69
|
|
|
return $response; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/* @var $form Form */ |
|
73
|
|
|
$form = $this->createForm(new StorageForm(), $storage); |
|
74
|
|
|
|
|
75
|
|
|
if ($request->getMethod() == 'POST') { |
|
76
|
|
|
$form->handleRequest($request); |
|
77
|
|
|
if ($form->isValid()) { |
|
78
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
79
|
|
|
$em->persist($storage); |
|
80
|
|
|
$em->flush(); |
|
81
|
|
|
|
|
82
|
|
|
return $this->redirect($this->generateUrl('storage_list')); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $this->render('AnimeDbCatalogBundle:Storage:change.html.twig', [ |
|
87
|
|
|
'storage' => $storage, |
|
88
|
|
|
'form' => $form->createView(), |
|
89
|
|
|
], $response); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Add storage. |
|
94
|
|
|
* |
|
95
|
|
|
* @param Request $request |
|
96
|
|
|
* |
|
97
|
|
|
* @return Response |
|
98
|
|
|
*/ |
|
99
|
|
|
public function addAction(Request $request) |
|
100
|
|
|
{ |
|
101
|
|
|
$storage = new Storage(); |
|
102
|
|
|
|
|
103
|
|
|
/* @var $form Form */ |
|
104
|
|
|
$form = $this->createForm(new StorageForm(), $storage); |
|
105
|
|
|
|
|
106
|
|
|
if ($request->getMethod() == 'POST') { |
|
107
|
|
|
$form->handleRequest($request); |
|
108
|
|
|
if ($form->isValid()) { |
|
109
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
110
|
|
|
$em->persist($storage); |
|
111
|
|
|
$em->flush(); |
|
112
|
|
|
|
|
113
|
|
|
return $this->redirect($this->generateUrl('storage_list')); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->render('AnimeDbCatalogBundle:Storage:add.html.twig', [ |
|
118
|
|
|
'form' => $form->createView(), |
|
119
|
|
|
'guide' => $this->get('anime_db.api.client')->getSiteUrl(self::GUIDE_LINK), |
|
120
|
|
|
]); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Delete storage. |
|
125
|
|
|
* |
|
126
|
|
|
* @param Storage $storage |
|
127
|
|
|
* |
|
128
|
|
|
* @return Response |
|
129
|
|
|
*/ |
|
130
|
|
|
public function deleteAction(Storage $storage) |
|
131
|
|
|
{ |
|
132
|
|
|
$em = $this->getDoctrine()->getManager(); |
|
133
|
|
|
$em->remove($storage); |
|
134
|
|
|
$em->flush(); |
|
135
|
|
|
|
|
136
|
|
|
return $this->redirect($this->generateUrl('storage_list')); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Get storage path. |
|
141
|
|
|
* |
|
142
|
|
|
* @param Request $request |
|
143
|
|
|
* |
|
144
|
|
|
* @return Response |
|
145
|
|
|
*/ |
|
146
|
|
|
public function getPathAction(Request $request) |
|
147
|
|
|
{ |
|
148
|
|
|
/* @var $response JsonResponse */ |
|
149
|
|
|
$response = $this->getCacheTimeKeeper() |
|
150
|
|
|
->getResponse('AnimeDbCatalogBundle:Storage', -1, new JsonResponse()); |
|
151
|
|
|
// response was not modified for this request |
|
152
|
|
|
if ($response->isNotModified($request)) { |
|
153
|
|
|
return $response; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/* @var $storage Storage */ |
|
157
|
|
|
$storage = $this->getDoctrine()->getManager() |
|
158
|
|
|
->find('AnimeDbCatalogBundle:Storage', $request->get('id')); |
|
159
|
|
|
|
|
160
|
|
|
return $response->setData([ |
|
161
|
|
|
'required' => $storage->isPathRequired(), |
|
162
|
|
|
'path' => $storage->getPath(), |
|
163
|
|
|
]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Scan storage. |
|
168
|
|
|
* |
|
169
|
|
|
* @param Storage $storage |
|
170
|
|
|
* |
|
171
|
|
|
* @return Response |
|
172
|
|
|
*/ |
|
173
|
|
|
public function scanAction(Storage $storage) |
|
174
|
|
|
{ |
|
175
|
|
|
$this->get('anime_db.storage.scan_executor')->export($storage); |
|
176
|
|
|
|
|
177
|
|
|
return $this->render('AnimeDbCatalogBundle:Storage:scan.html.twig', [ |
|
178
|
|
|
'storage' => $storage, |
|
179
|
|
|
]); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Get storage scan output. |
|
184
|
|
|
* |
|
185
|
|
|
* @param Storage $storage |
|
186
|
|
|
* @param Request $request |
|
187
|
|
|
* |
|
188
|
|
|
* @return JsonResponse |
|
189
|
|
|
*/ |
|
190
|
|
|
public function scanOutputAction(Storage $storage, Request $request) |
|
191
|
|
|
{ |
|
192
|
|
|
$filename = $this->container->getParameter('anime_db.catalog.storage.scan_output'); |
|
193
|
|
|
$filename = sprintf($filename, $storage->getId()); |
|
194
|
|
|
if (!file_exists($filename)) { |
|
195
|
|
|
throw $this->createNotFoundException('Log file is not found'); |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
$log = file_get_contents($filename); |
|
199
|
|
|
$is_end = preg_match('/\nTime: \d+ s./', $log); |
|
200
|
|
|
|
|
201
|
|
|
if (($offset = $request->query->get('offset', 0)) && is_numeric($offset) && $offset > 0) { |
|
202
|
|
|
$log = (string) mb_substr($log, $offset, mb_strlen($log, 'UTF-8') - $offset, 'UTF-8'); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return new JsonResponse(['content' => $log, 'end' => $is_end]); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get storage scan progress. |
|
210
|
|
|
* |
|
211
|
|
|
* @param Storage $storage |
|
212
|
|
|
* |
|
213
|
|
|
* @return JsonResponse |
|
214
|
|
|
*/ |
|
215
|
|
|
public function scanProgressAction(Storage $storage) |
|
216
|
|
|
{ |
|
217
|
|
|
$filename = $this->container->getParameter('anime_db.catalog.storage.scan_progress'); |
|
218
|
|
|
$filename = sprintf($filename, $storage->getId()); |
|
219
|
|
|
if (!file_exists($filename)) { |
|
220
|
|
|
throw $this->createNotFoundException('The progress status cannot be read'); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$log = trim(file_get_contents($filename), " \r\n%"); |
|
224
|
|
|
|
|
225
|
|
|
return new JsonResponse(['status' => ($log != '' ? intval($log) : 100)]); |
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|