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