1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
6
|
|
|
use Illuminate\Pagination\Paginator; |
7
|
|
|
use App\Http\Controllers\Controller; |
8
|
|
|
use App\Models\{{class_name}} as {{class_name}}Model;; |
|
|
|
|
9
|
|
|
use App\Http\Controllers\File as ScaffolderFile; |
10
|
|
|
use Log; |
11
|
|
|
use Flow\Config as FlowConfig; |
12
|
|
|
use Flow\Request as FlowRequest; |
13
|
|
|
use Flow\ConfigInterface; |
14
|
|
|
use Flow\RequestInterface; |
15
|
|
|
|
16
|
|
|
class {{class_name}}Controller extends Controller |
17
|
|
|
{ |
18
|
|
|
// blade views |
19
|
|
|
// index view |
20
|
|
|
|
21
|
|
|
public function show($id) |
22
|
|
|
{ |
23
|
|
|
$file = {{class_name}}Model::find($id); |
24
|
|
|
return $file ; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function upload() |
28
|
|
|
{ |
29
|
|
|
$config = new FlowConfig(); |
30
|
|
|
$config->setTempDir(storage_path() . '/tmp'); |
31
|
|
|
$config->setDeleteChunksOnSave(true); |
32
|
|
|
|
33
|
|
|
$request = new FlowRequest(); |
34
|
|
|
|
35
|
|
|
$totalSize = $request->getTotalSize(); |
36
|
|
|
|
37
|
|
|
if ($totalSize && $totalSize > (1024 * 1024 * 4)) |
38
|
|
|
{ |
39
|
|
|
return $this->responseWithError('File size exceeds 4MB'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$requestFile = $request->getFile(); |
43
|
|
|
|
44
|
|
|
$file = new ScaffolderFile($config, $request); |
45
|
|
|
|
46
|
|
|
if ($file->validateChunk()) { |
47
|
|
|
$file->saveChunk(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
if ($file->save(storage_path() . '/tmp/' . $request->getFileName())) |
51
|
|
|
{ |
52
|
|
|
$file = FileModel::create([ |
53
|
|
|
'mime_type' => $requestFile['type'], |
54
|
|
|
'size' => $requestFile['size'], |
55
|
|
|
'file_path' => storage_path() . '/tmp/', |
56
|
|
|
'filename' => $requestFile['name'], |
57
|
|
|
'disk' => 'local', |
58
|
|
|
'status' => false, |
59
|
|
|
]); |
60
|
|
|
|
61
|
|
|
return $file->id; |
62
|
|
|
} |
63
|
|
|
else |
64
|
|
|
{ |
65
|
|
|
// Indicate that we are not done with all the chunks. |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.