|
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\AppBundle\Controller; |
|
10
|
|
|
|
|
11
|
|
|
use Symfony\Component\Form\Form; |
|
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
|
15
|
|
|
use AnimeDb\Bundle\AppBundle\Entity\Field\Image as ImageField; |
|
16
|
|
|
use AnimeDb\Bundle\AppBundle\Form\Type\Field\Image\Upload as UploadImage; |
|
17
|
|
|
use AnimeDb\Bundle\AppBundle\Form\Type\Field\LocalPath\Choice as ChoiceLocalPath; |
|
18
|
|
|
use AnimeDb\Bundle\AppBundle\Util\Filesystem; |
|
19
|
|
|
|
|
20
|
|
|
class FormController extends BaseController |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Form field local path. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Request $request |
|
26
|
|
|
* |
|
27
|
|
|
* @return Response |
|
28
|
|
|
*/ |
|
29
|
|
|
public function localPathAction(Request $request) |
|
30
|
|
|
{ |
|
31
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse(); |
|
32
|
|
|
// response was not modified for this request |
|
33
|
|
|
if ($response->isNotModified($request)) { |
|
34
|
|
|
return $response; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
$form = $this->createForm( |
|
38
|
|
|
new ChoiceLocalPath(), |
|
39
|
|
|
['path' => $request->get('path') ?: ''] |
|
40
|
|
|
); |
|
41
|
|
|
|
|
42
|
|
|
return $this->render('AnimeDbAppBundle:Form:local_path.html.twig', [ |
|
43
|
|
|
'form' => $form->createView(), |
|
44
|
|
|
], $response); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Return list folders for path. |
|
49
|
|
|
* |
|
50
|
|
|
* @param Request $request |
|
51
|
|
|
* |
|
52
|
|
|
* @return JsonResponse |
|
53
|
|
|
*/ |
|
54
|
|
|
public function localPathFoldersAction(Request $request) |
|
55
|
|
|
{ |
|
56
|
|
|
$form = $this->createForm(new ChoiceLocalPath()); |
|
57
|
|
|
$form->handleRequest($request); |
|
58
|
|
|
$path = $form->get('path')->getData() ?: Filesystem::getUserHomeDir(); |
|
59
|
|
|
|
|
60
|
|
|
if (($root = $request->get('root')) && strpos($path, $root) !== 0) { |
|
61
|
|
|
$path = $root; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/* @var $response JsonResponse */ |
|
65
|
|
|
$response = $this->getCacheTimeKeeper() |
|
66
|
|
|
->getResponse([(new \DateTime())->setTimestamp(filemtime($path))], -1, new JsonResponse()); |
|
67
|
|
|
// response was not modified for this request |
|
68
|
|
|
if ($response->isNotModified($request)) { |
|
69
|
|
|
return $response; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $response->setData([ |
|
73
|
|
|
'path' => $path, |
|
74
|
|
|
'folders' => Filesystem::scandir($path, Filesystem::DIRECTORY), |
|
75
|
|
|
]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Form field image. |
|
80
|
|
|
* |
|
81
|
|
|
* @param Request $request |
|
82
|
|
|
* |
|
83
|
|
|
* @return Response |
|
84
|
|
|
*/ |
|
85
|
|
|
public function imageAction(Request $request) |
|
86
|
|
|
{ |
|
87
|
|
|
$response = $this->getCacheTimeKeeper()->getResponse(); |
|
88
|
|
|
// response was not modified for this request |
|
89
|
|
|
if ($response->isNotModified($request)) { |
|
90
|
|
|
return $response; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->render('AnimeDbAppBundle:Form:image.html.twig', [ |
|
94
|
|
|
'form' => $this->createForm(new UploadImage())->createView(), |
|
95
|
|
|
'change' => (bool) $request->get('change', false), |
|
96
|
|
|
], $response); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param Request $request |
|
101
|
|
|
* |
|
102
|
|
|
* @return JsonResponse |
|
103
|
|
|
*/ |
|
104
|
|
|
public function imageUploadAction(Request $request) |
|
105
|
|
|
{ |
|
106
|
|
|
$image = new ImageField(); |
|
107
|
|
|
/* @var $form Form */ |
|
108
|
|
|
$form = $this->createForm(new UploadImage(), $image); |
|
109
|
|
|
$form->handleRequest($request); |
|
110
|
|
|
|
|
111
|
|
|
if (!$form->isValid()) { |
|
112
|
|
|
$errors = $form->getErrors(); |
|
113
|
|
|
|
|
114
|
|
|
return new JsonResponse(['error' => $this->get('translator')->trans($errors[0]->getMessage())], 404); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
// try upload file |
|
118
|
|
|
try { |
|
119
|
|
|
$this->get('anime_db.downloader')->imageField($image); |
|
120
|
|
|
|
|
121
|
|
|
return new JsonResponse([ |
|
122
|
|
|
'path' => $image->getFilename(), |
|
123
|
|
|
'image' => $image->getWebPath(), |
|
124
|
|
|
]); |
|
125
|
|
|
} catch (\InvalidArgumentException $e) { |
|
126
|
|
|
return new JsonResponse(['error' => $this->get('translator')->trans($e->getMessage())], 404); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|