-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathFileController.php
More file actions
339 lines (290 loc) · 11.9 KB
/
Copy pathFileController.php
File metadata and controls
339 lines (290 loc) · 11.9 KB
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<?php
namespace IXP\Http\Controllers\DocstoreCustomer;
/*
* Copyright (C) 2009 - 2021 Internet Neutral Exchange Association Company Limited By Guarantee.
* All Rights Reserved.
*
* This file is part of IXP Manager.
*
* IXP Manager is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version v2.0 of the License.
*
* IXP Manager is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License v2.0
* along with IXP Manager. If not, see:
*
* http://www.gnu.org/licenses/gpl-2.0.html
*/
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
use Log, Storage;
use Former\Facades\Former;
use Illuminate\Http\{
RedirectResponse,
Request
};
use Illuminate\Validation\Rule;
use Illuminate\View\View;
use IXP\Http\Controllers\Controller;
use IXP\Models\{
Customer,
DocstoreCustomerDirectory,
DocstoreCustomerFile,
User
};
use IXP\Utils\View\Alert\{
Alert,
Container as AlertContainer
};
use League\Flysystem\FilesystemException;
/**
* FileController Controller
*
* @author Barry O'Donovan <barry@islandbridgenetworks.ie>
* @author Yann Robin <yann@islandbridgenetworks.ie>
* @category DocstoreCustomer
* @copyright Copyright (C) 2009 - 2021 Internet Neutral Exchange Association Company Limited By Guarantee
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU GPL V2.0
*/
class FileController extends Controller
{
/**
* View a docstore customer file apply to allowed mimetype ( DocstoreFile::$
*
* @param Customer $cust
* @param DocstoreCustomerFile $file
*
* @return mixed
*
* @throws AuthorizationException|FileNotFoundException
*/
public function view( Customer $cust, DocstoreCustomerFile $file )
{
$this->authorize( 'view', $file );
if( !$file->isViewable() ) {
return redirect( route( 'docstore-c-file@download', [ 'cust' => $cust, 'file' => $file->id ] ) );
}
return view( 'docstore-customer/file/view', [
'file' => $file,
'cust' => $cust,
'content' => Storage::disk( $file->disk )->get( $file->path )
] );
}
/**
* Download a docstore customer file
*
* @param Customer $cust
* @param DocstoreCustomerFile $file
*
* @return mixed
*
* @throws AuthorizationException
*/
public function download( Customer $cust, DocstoreCustomerFile $file )
{
$this->authorize( 'download', $file );
try {
/** @psalm-suppress UndefinedInterfaceMethod */
return Storage::disk( $file->disk )->download( $file->path, $file->name );
} catch( FilesystemException $e ) {
AlertContainer::push( "This customer file could not be found / downloaded. Please report this error to the support team.", Alert::DANGER );
return redirect( route( 'docstore-c-dir@list', [ 'cust' => $file->customer->id , 'dir' => $file->docstore_customer_directory_id ] ) );
}
}
/**
* Get information on a docstore customer file
*
* @param DocstoreCustomerFile $file
*
* @return mixed
*
* @throws AuthorizationException
*/
public function info( DocstoreCustomerFile $file )
{
$this->authorize( 'info', $file );
return view( 'docstore-customer/file/info', [
'file' => $file,
'size' => Storage::disk( $file->disk )->size( $file->path ),
'last_modified' => Storage::disk( $file->disk )->lastModified( $file->path ),
'dspath' => config( 'filesystems.disks.' . $file->disk . '.root', '*** UNKNOWN LOCATION ***' ) . '/' . $file->path,
'created_by' => User::find( $file->created_by ),
'created_at' => $file->created_at,
]);
}
/**
* Upload a new docstore customer file
*
* @param Request $r
* @param Customer $cust
*
* @return View
*
* @throws AuthorizationException
*/
public function upload( Request $r, Customer $cust ): View
{
$this->authorize( 'create', DocstoreCustomerFile::class );
Former::populate([
'min_privs' => $r->old( 'min_privs', (string) User::AUTH_SUPERUSER )
]);
return view( 'docstore-customer/file/upload', [
'file' => false,
'cust' => $cust,
'dirs' => DocstoreCustomerDirectory::getListingForDropdown( DocstoreCustomerDirectory::getListing( $cust, $r->user() ) ),
] );
}
/**
* Store a docstore customer file uploaded
*
* @param Request $r
* @param Customer $cust
*
* @return RedirectResponse
*
* @throws AuthorizationException
*/
public function store( Request $r, Customer $cust ): RedirectResponse
{
$this->authorize( 'create', DocstoreCustomerFile::class );
$this->checkForm( $r );
$uploadedFile = $r->file('uploadedFile' );
$path = $uploadedFile->store( (string) $cust->id, 'docstore_customers' );
/** @psalm-suppress InvalidArgument */
$file = DocstoreCustomerFile::create( [
'name' => $r->name,
'description' => $r->description,
'cust_id' => $cust->id,
'min_privs' => $r->min_privs,
'path' => $path,
'sha256' => hash_file( 'sha256', $uploadedFile ),
'created_by' => $r->user()->id,
'file_last_updated' => now(),
'docstore_customer_directory_id' => $r->docstore_customer_directory_id,
] );
Log::info( sprintf( "DocStore: file [%d|%s] uploaded by %s for the customer [%d|%s]", $file->id, $file->name, $r->user()->username, $cust->id, $cust->name ) );
AlertContainer::push( ucfirst( config( 'ixp_fe.lang.customer.one' ) ) . " File <em>{$r->name}</em> uploaded.", Alert::SUCCESS );
return redirect( route( 'docstore-c-dir@list', [ 'cust' => $cust , 'dir' => $file->docstore_customer_directory_id ] ) );
}
/**
* Edit a docstore customer file uploaded
*
* @param Request $r
* @param Customer $cust
* @param DocstoreCustomerFile $file
*
* @return View
*
* @throws AuthorizationException
*/
public function edit( Request $r, Customer $cust, DocstoreCustomerFile $file ): View
{
$this->authorize( 'update', $file );
Former::populate([
'name' => $r->old( 'name', $file->name ),
'description' => $r->old( 'descripton', $file->description ),
'sha256' => $r->old( 'sha256', $file->sha256 ),
'min_privs' => $r->old( 'min_privs', (string) $file->min_privs ),
'docstore_customer_directory_id' => $r->old( 'docstore_customer_directory_id',$file->docstore_customer_directory_id ?? '' ),
]);
return view( 'docstore-customer/file/upload', [
'file' => $file,
'cust' => $cust,
'dirs' => DocstoreCustomerDirectory::getListingForDropdown( DocstoreCustomerDirectory::getListing( $file->customer, $r->user() ) )
] );
}
/**
* Update a docstore customer file uploaded
*
* @param Request $r
* @param Customer $cust
* @param DocstoreCustomerFile $file
*
* @return RedirectResponse
*
* @throws AuthorizationException
*/
public function update( Request $r, Customer $cust, DocstoreCustomerFile $file ): RedirectResponse
{
$this->authorize( 'update', $file );
$this->checkForm( $r, $file );
// if a new file is updated
if( $r->uploadedFile ) {
// get path of the old file in order to delete it later
$oldPath = $file->path;
$uploadedFile = $r->file('uploadedFile');
$path = $uploadedFile->store( $file->customer->id, 'docstore_customers' );
/** @psalm-suppress InvalidArgument */
$file->update([
'path' => $path,
'sha256' => hash_file( 'sha256', $uploadedFile ),
'file_last_updated' => now(),
]);
// Delete the old file
Storage::disk( $file->disk )->delete( $oldPath );
}
$file->update( [
'name' => $r->name,
'description' => $r->description,
'docstore_customer_directory_id' => $r->docstore_customer_directory_id,
'min_privs' => $r->min_privs
] );
Log::info( sprintf( "DocStore: customer file [%d|%s] edited by %s for the customer [%d|%s]", $file->id, $file->name, $r->user()->username, $cust->id, $cust->name ) );
AlertContainer::push( ucfirst( config( 'ixp_fe.lang.customer.one' ) ) . " file <em>{$r->name}</em> updated.", Alert::SUCCESS );
return redirect( route( 'docstore-c-dir@list', [ 'cust' => $cust , 'dir' => $file->docstore_customer_directory_id ] ) );
}
/**
* Delete a file
*
* @param Request $r
* @param DocstoreCustomerFile $file
*
* @return RedirectResponse
*
* @throws AuthorizationException
*/
public function delete( Request $r , DocstoreCustomerFile $file ): RedirectResponse
{
$this->authorize( 'delete', $file );
$dir = $file->directory;
$cust = $file->customer;
Storage::disk( $file->disk )->delete( $file->path );
AlertContainer::push( ucfirst( config( 'ixp_fe.lang.customer.one' ) ) . " file <em>{$file->name}</em> deleted.", Alert::SUCCESS );
Log::info( sprintf( "DocStore: customer file [%d|%s] deleted by %s for the customer [%d|%s]", $file->id, $file->name, $r->user()->username, $cust->id, $cust->name ) );
$file->delete();
return redirect( route( 'docstore-c-dir@list', [ 'cust' => $cust, 'dir' => $dir ] ) );
}
/**
* Check if the form is valid
*
* @param Request $r
* @param DocstoreCustomerFile|null $file
*
* @return void
*/
private function checkForm( Request $r, ?DocstoreCustomerFile $file = null ): void
{
$r->validate( [
'name' => 'required|max:100',
'uploadedFile' => Rule::requiredIf( function() use ( $r, $file ) {
return !$file;
}),
'sha256' => [ 'nullable', 'max:64',
function ( $attribute, $value, $fail ) use( $r ) {
/** @psalm-suppress InvalidArgument */
if( $value && $r->file('uploadedFile' ) && $value !== hash_file( 'sha256', $r->file( 'uploadedFile' ) ) ) {
return $fail( 'The sha256 checksum calculated on the server does not match the one you provided.' );
}
return null;
},
],
'min_privs' => 'required|integer|in:' . implode( ',', array_keys( User::$PRIVILEGES ) ),
'docstore_customer_directory_id' => 'nullable|integer|exists:docstore_customer_directories,id',
] );
}
}