/
DownloadController.php
197 lines (188 loc) · 6.66 KB
/
DownloadController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace Filehosting\Controllers;
use Slim\Http\Request;
use Slim\Http\Response;
use Slim\Views\Twig;
use Slim\Csrf\Guard;
use Illuminate\Database\Capsule\Manager as DB;
use Filehosting\Auth\UploaderAuth;
use Filehosting\Validators\CommentValidator;
use Filehosting\Helpers\{
FileSystem, SphinxSearch
};
use Filehosting\Models\{
File, Comment
};
use Filehosting\Exceptions\CommentAdditionException;
/**
* Handles File Download Page
* Class DownloadController
* @package Filehosting\Controllers
*/
class DownloadController extends Controller
{
/**
* @var Guard
*/
protected $csrf;
/**
* @var UploaderAuth
*/
protected $uploaderAuth;
/**
* @var FileSystem
*/
protected $fileSystem;
/**
* @var CommentValidator
*/
protected $commentValidator;
/**
* @var SphinxSearch
*/
protected $sphinxSearch;
/**
* DownloadController constructor.
* @param \Slim\Container $container
*/
public function __construct(Twig $twig, Guard $csrf, UploaderAuth $uploaderAuth, FileSystem $fileSystem, CommentValidator $commentValidator, SphinxSearch $sphinxSearch)
{
parent::__construct($twig);
$this->csrf = $csrf;
$this->uploaderAuth = $uploaderAuth;
$this->fileSystem = $fileSystem;
$this->commentValidator = $commentValidator;
$this->sphinxSearch = $sphinxSearch;
}
/**
*
* Shows download page
*
* @param Request $request
* @param Response $response
* @param array $args
* @return Response
*/
public function index(Request $request, Response $response, array $args = []): Response
{
$file = File::find(intval($args['id']));
if (!$file) {
return $response->withStatus(404);
}
$file->size = round($file->size / (1024 * 1024), 2);
$file->uploaded = date("m/d/Y", $file->uploaded);
return $this->twig->render($response, 'download.twig', ['csrfNameKey' => $this->csrf->getTokenNameKey(),
'csrfValueKey' => $this->csrf->getTokenValueKey(),
'csrfName' => $csrfName = $request->getAttribute($this->csrf->getTokenNameKey()),
'csrfValue' => $csrfValue = $request->getAttribute($this->csrf->getTokenValueKey()),
'file' => $file,
'link' => $request->getUri(),
'comments' => $file->getSortedComments(),
'commentsAllowed' => $file->countRootComments() < 998 ? true : false,
'isAuth' => $this->uploaderAuth->isAuth($request),
'pathToThumbnail' => $file->isImage() ? $this->fileSystem->getPathToThumbnail($file) : null
]);
}
/**
*
* Forces file download
* Uses xsendfile module if it is installed on a server, if not uses php
*
* @param Request $request
* @param Response $response
* @param array $args
* @return Response
*/
public function forceFileDownload(Request $request, Response $response, array $args = []): Response
{
$file = File::findOrFail(intval($args['id']));
$filePath = $this->fileSystem->getAbsolutePathToFile($file);
if (!$request->getParam('player')) {
$file->download_counter++;
}
if (!(in_array('mod_xsendfile', apache_get_modules()))) {
$response = $response->withHeader('Content-Type', $file->media_type)
->withHeader('Content-Disposition', 'attachment;filename="' . $file->original_name . '"')
->withHeader('Content-Length', $file->size)
->write($filePath);
} else {
$response = $response->withHeader('X-SendFile', $filePath)
->withHeader('Content-Type', $file->media_type)
->withHeader('Content-Length', $file->size)
->withHeader('Content-Disposition', 'attachment;filename="' . $file->original_name . '"');
}
$file->save();
return $response;
}
/**
*
* Deletes file from storage and it's entity from database
* Also deletes uploader's cookie
*
* @param Request $request
* @param Response $response
* @param array $args
* @return Response
* @throws \Filehosting\Exceptions\FileSystemException
*/
public function deleteFile(Request $request, Response $response, array $args = []): Response
{
$file = File::findOrFail(intval($args['id']));
$filePath = $this->fileSystem->getAbsolutePathToFile($file);
try {
DB::beginTransaction();
unlink($filePath);
$file->comments->delete();
File::destroy($file->id);
$response = $this->uploaderAuth->deleteUploaderToken($file->id, $response);
} catch (\Exception $e) {
DB::rollback();
throw new $e;
}
DB::commit();
$this->sphinxSearch->deleteIndexedFile($file->id);
return $response->withRedirect('/?deleted=success');
}
/**
*
* Creates and adds a comment from parsed post-vars
* If there are more than 998 comments throws CommentAdditionException
* @param Request $request
* @param Response $response
* @param array $args
* @return Response
* @throws CommentAdditionException
*/
public function addComment(Request $request, Response $response, array $args = []): Response
{
$file = File::findOrFail(intval($args['id']));
if ($file->countRootComments() > 998) {
// throws an Exception because matpath format which is used in the app doesn't expect more than 999 root comments
throw new CommentAdditionException('Комментарии закрыты');
}
// Parses POST-vars
$parentId = intval($request->getParam('parentId'));
$comment = new Comment (['file_id' => $file->id,
'comment_text' => $request->getParam('comment'),
'author' => $request->getParam('author')]);
if ($parentId) {
$comment->parent_id = $parentId;
}
$comment->generateMatpath();
$errors = $this->commentValidator->validate($comment);
if (empty($errors['errors'])) {
$comment->save();
$date = new \DateTime('now');
$comment->posted = $date->format('m/d/Y'); // sets a date in order to use it in the template
if ($request->isXhr()) {
return $response->withJson($comment);
}
return $response->withRedirect($request->getUri());
} else {
if ($request->isXhr()) {
return $response->withJson($errors);
}
return $response->withRedirect($request->getUri());
}
}
}