1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains\Files; |
4
|
|
|
|
5
|
|
|
use App\Files; |
6
|
|
|
|
7
|
|
|
use Illuminate\Http\UploadedFile; |
8
|
|
|
use Illuminate\Support\Facades\Log; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Illuminate\Support\Facades\Storage; |
11
|
|
|
|
12
|
|
|
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver; |
13
|
|
|
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; |
14
|
|
|
|
15
|
|
|
class SetFiles |
16
|
|
|
{ |
17
|
|
|
protected $path, $disk; |
18
|
|
|
|
19
|
|
|
// Constructor will set the default path for files to be stored |
20
|
20 |
|
public function __construct() |
21
|
|
|
{ |
22
|
20 |
|
$this->path = config('filesystems.paths.default'); |
23
|
20 |
|
$this->disk = 'local'; |
24
|
20 |
|
} |
25
|
|
|
|
26
|
|
|
// Process a single chunk of a larger file |
27
|
4 |
|
protected function getChunk($request) |
28
|
|
|
{ |
29
|
4 |
|
$receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request)); |
30
|
4 |
|
$save = $receiver->receive(); |
31
|
|
|
|
32
|
4 |
|
if($save->isFinished()) |
33
|
|
|
{ |
34
|
4 |
|
return $this->saveFile($save->getFile()); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
return false; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
// Save the file to the storage system |
41
|
4 |
|
protected function saveFile(UploadedFile $file) |
42
|
|
|
{ |
43
|
4 |
|
$fileName = $this->cleanFilename($file->getClientOriginalName()); |
44
|
4 |
|
$fileName = $this->checkForDuplicate($fileName); |
45
|
|
|
|
46
|
4 |
|
$file->storeAs($this->path, $fileName); |
47
|
4 |
|
$user = isset(Auth::user()->full_name) ? Auth::user()->full_name : \Request::ip(); |
48
|
4 |
|
Log::info('New file '.$fileName.' stored in location - '.$this->path.' by '.$user); |
49
|
|
|
|
50
|
4 |
|
return $fileName; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// Delete a file from the database and file system - note will fail if the file is linked in the database |
54
|
4 |
|
protected function deleteFile($fileID) |
55
|
|
|
{ |
56
|
4 |
|
$fileData = Files::find($fileID); |
57
|
4 |
|
$fileLink = $fileData->file_link.$fileData->file_name; |
58
|
|
|
|
59
|
|
|
try |
60
|
|
|
{ |
61
|
|
|
// Try to delete file from database - will throw error if foreign key is in use |
62
|
4 |
|
$fileData->delete(); |
63
|
|
|
} |
64
|
|
|
catch(\Illuminate\Database\QueryException $e) |
65
|
|
|
{ |
66
|
|
|
// Unable to remove file from the database |
67
|
|
|
Log::warning('Attempt to delete file failed. Reason - '.$e.'. Additional File Data - ', ['file_id' => $fileID, 'file_name' => $fileLink]); |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Delete the file from the storage system |
72
|
4 |
|
Storage::delete($fileLink); |
73
|
|
|
|
74
|
4 |
|
Log::notice('File deleted. File Information - ', ['file_id' => $fileID, 'file_name' => $fileLink]); |
75
|
4 |
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Move a file from one folder to another |
79
|
|
|
protected function moveFile($fileID, $newPath) |
80
|
|
|
{ |
81
|
|
|
$this->path = $newPath; |
82
|
|
|
$file = Files::find($fileID); |
83
|
|
|
$newName = $this->checkForDuplicate($file->file_name); |
84
|
|
|
|
85
|
|
|
Storage::move($file->file_link.$file->file_name, $newPath.$file->file_name); |
86
|
|
|
$file->update([ |
87
|
|
|
'file_link' => $newPath, |
88
|
|
|
'file_name' => $newName, |
89
|
|
|
]); |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
// Add the new file to the database |
95
|
4 |
|
protected function addDatabaseRow($filename, $path) |
96
|
|
|
{ |
97
|
4 |
|
$file = Files::create([ |
98
|
4 |
|
'file_name' => $filename, |
99
|
4 |
|
'file_link' => $path.DIRECTORY_SEPARATOR, |
100
|
|
|
]); |
101
|
|
|
|
102
|
4 |
|
return $file->file_id; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Sanitize the filename to remove any illegal characters and spaces |
106
|
4 |
|
protected function cleanFilename($name) |
107
|
|
|
{ |
108
|
4 |
|
return preg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $name); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
// Check to see if the file already exists, if so append filename as needed |
112
|
4 |
|
protected function checkForDuplicate($name) |
113
|
|
|
{ |
114
|
4 |
|
if(Storage::disk($this->disk)->exists($this->path.DIRECTORY_SEPARATOR.$name)) |
115
|
|
|
{ |
116
|
|
|
$parts = pathinfo($name); |
117
|
|
|
$ext = isset($parts['extension']) ? ('.'.$parts['extension']) : ''; |
118
|
|
|
$base = $parts['filename']; |
119
|
|
|
$number = 0; |
120
|
|
|
|
121
|
|
|
do |
122
|
|
|
{ |
123
|
|
|
$name = $base.'('.++$number.')'.$ext; |
124
|
|
|
} while(Storage::disk($this->disk)->exists($this->path.DIRECTORY_SEPARATOR.$name)); |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
return $name; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|