-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathExportations.php
123 lines (108 loc) · 3.77 KB
/
Exportations.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
<?php
namespace App\controllers;
use Minz\Request;
use Minz\Response;
use App\auth;
use App\jobs;
use App\models;
/**
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
class Exportations
{
/**
* @response 302 /login?redirect_to=/exportations
* If user is not connected
* @response 200
* On success
*/
public function show(Request $request): Response
{
$user = auth\CurrentUser::get();
if (!$user) {
$redirect_to = \Minz\Url::for('exportation');
return Response::redirect('login', ['redirect_to' => $redirect_to]);
}
$exportation = models\Exportation::findBy([
'user_id' => $user->id,
]);
return Response::ok('exportations/show.phtml', [
'exportation' => $exportation,
]);
}
/**
* @request_param string csrf
*
* @response 400
* If CSRF is invalid, or if an exportation is already ongoing
* @response 302 /login?redirect_to=/exportations
* If user is not connected
* @response 302 /exportations
* On success
*/
public function create(Request $request): Response
{
$user = auth\CurrentUser::get();
$csrf = $request->param('csrf', '');
if (!$user) {
$redirect_to = \Minz\Url::for('exportation');
return Response::redirect('login', ['redirect_to' => $redirect_to]);
}
if (!\Minz\Csrf::validate($csrf)) {
return Response::badRequest('exportations/show.phtml', [
'exportation' => null,
'error' => _('A security verification failed.'),
]);
}
$exportation = models\Exportation::findBy([
'user_id' => $user->id,
]);
if ($exportation && $exportation->status !== 'ongoing') {
@unlink($exportation->filepath);
models\Exportation::delete($exportation->id);
} elseif ($exportation) {
return Response::badRequest('exportations/show.phtml', [
'exportation' => $exportation,
'error' => _('You already have an ongoing exportation.'),
]);
}
$exportation = new models\Exportation($user->id);
$exportation->save();
$exportator_job = new jobs\Exportator();
$exportator_job->performAsap($exportation->id);
return Response::redirect('exportation');
}
/**
* @response 302 /login?redirect_to=/exportations
* If user is not connected
* @response 404
* If there’s no archive to download
* @response 200
* On success
*/
public function download(Request $request): Response
{
$user = auth\CurrentUser::get();
if (!$user) {
$redirect_to = \Minz\Url::for('exportation');
return Response::redirect('login', ['redirect_to' => $redirect_to]);
}
$exportation = models\Exportation::findBy([
'user_id' => $user->id,
'status' => 'finished',
]);
if (!$exportation || !file_exists($exportation->filepath)) {
return Response::notFound('not_found.phtml');
}
$filename_date = $exportation->created_at->format('Y-m-d');
$filename_brand = \App\Configuration::$application['brand'];
$filename = "{$filename_date}_{$filename_brand}_data.zip";
$file_output = new \Minz\Output\File($exportation->filepath);
$response = new Response(200, $file_output);
$filesize = filesize($exportation->filepath);
$response->setHeader('Content-Length', (string) $filesize);
$response->setHeader('Content-Disposition', "filename={$filename}");
return $response;
}
}