1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Domains; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Support\Facades\Storage; |
8
|
|
|
|
9
|
|
|
use App\Files; |
10
|
|
|
|
11
|
|
|
class FilesDomain |
12
|
|
|
{ |
13
|
|
|
protected $receiver, $path, $fileID; |
14
|
|
|
|
15
|
|
|
// Constructor will set a default path location in the event one is not set through child class |
16
|
28 |
|
public function __construct() |
17
|
|
|
{ |
18
|
28 |
|
$this->path = config('filesystems.paths.default'); |
19
|
28 |
|
} |
20
|
|
|
|
21
|
|
|
// Save a file after it has been uploaded |
22
|
10 |
|
protected function saveFile($file) |
23
|
|
|
{ |
24
|
10 |
|
$fileName = $this->cleanFilename($file->getClientOriginalName()); |
25
|
10 |
|
$fileName = $this->isFileDup($fileName); |
26
|
10 |
|
$file->storeAs($this->path, $fileName); |
27
|
|
|
|
28
|
|
|
// Place file in Files table of DB |
29
|
10 |
|
$newFile = Files::create([ |
30
|
10 |
|
'file_name' => $fileName, |
31
|
10 |
|
'file_link' => $this->path.DIRECTORY_SEPARATOR |
32
|
|
|
]); |
33
|
|
|
|
34
|
10 |
|
$user = isset(Auth::user()->full_name) ? Auth::user()->full_name : \Request::ip(); |
35
|
10 |
|
Log::info('New file stored in database by '.$user.'. File Data - ', array($newFile)); |
36
|
10 |
|
return $newFile->file_id; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// Try to delete a file - note this will fail if it is still in use |
40
|
2 |
|
protected function deleteFile($fileID) |
41
|
|
|
{ |
42
|
|
|
try |
43
|
|
|
{ |
44
|
|
|
// Try to delete file from database - will throw error if foreign key is in use |
45
|
2 |
|
$fileData = Files::find($fileID); |
46
|
2 |
|
$fileLink = $fileData->file_link.$fileData->file_name; |
47
|
2 |
|
$fileData->delete(); |
48
|
|
|
} |
49
|
|
|
catch(\Illuminate\Database\QueryException $e) |
50
|
|
|
{ |
51
|
|
|
// Unable to remove file from the database |
52
|
|
|
Log::warning('Attempt to delete file failed. Reason - '.$e.'. Additional File Data - ', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
|
|
|
|
53
|
|
|
return false; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Delete the file from the storage system |
57
|
2 |
|
Storage::delete($fileLink); |
58
|
|
|
|
59
|
2 |
|
Log::notice('File deleted by '.Auth::user()->full_name.'. File Information - ', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
60
|
2 |
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Clean a file name to remove any spaces |
64
|
10 |
|
protected function cleanFilename($name) |
65
|
|
|
{ |
66
|
|
|
// Remove all spaces |
67
|
10 |
|
$fileName = str_replace(' ', '_', $name); |
68
|
10 |
|
Log::debug('Cleaning filename. Old name - '.$name.'. New Name - '.$fileName); |
69
|
|
|
|
70
|
10 |
|
return $fileName; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Determine if the already exists and should be appended |
74
|
10 |
|
protected function isFileDup($fileName) |
75
|
|
|
{ |
76
|
|
|
// Determine if the filename already exists |
77
|
10 |
|
if (Storage::exists($this->path . DIRECTORY_SEPARATOR . $fileName)) |
78
|
|
|
{ |
79
|
|
|
$fileParts = pathinfo($fileName); |
80
|
|
|
$extension = isset($fileParts['extension']) ? ('.' . $fileParts['extension']) : ''; |
81
|
|
|
|
82
|
|
|
// Look to see if a number is already appended to a file. (example - file(1).pdf) |
83
|
|
|
if (preg_match('/(.*?)(\d+)$/', $fileParts['filename'], $match)) |
84
|
|
|
{ |
85
|
|
|
// Has a number, increment it |
86
|
|
|
$base = $match[1]; |
87
|
|
|
$number = intVal($match[2]); |
88
|
|
|
} |
89
|
|
|
else |
90
|
|
|
{ |
91
|
|
|
// No number, add one |
92
|
|
|
$base = $fileParts['filename']; |
93
|
|
|
$number = 0; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Increase the number until one that is not in use is found |
97
|
|
|
do |
98
|
|
|
{ |
99
|
|
|
$fileName = $base . '(' . ++$number . ')' . $extension; |
100
|
|
|
} while (Storage::exists($this->path.DIRECTORY_SEPARATOR.$fileName)); |
101
|
|
|
} |
102
|
|
|
|
103
|
10 |
|
return $fileName; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// When multiple files are uploaded at once, they are placed in a temporary location. This moves them to the final folder |
107
|
6 |
|
protected function moveFile($newPath, $fileID) |
108
|
|
|
{ |
109
|
6 |
|
$data = Files::find($fileID); |
110
|
|
|
// Move the file to the proper folder |
111
|
|
|
try{ |
112
|
6 |
|
Log::debug('Attempting to moving file '.$fileID.' to '.$newPath); |
113
|
6 |
|
Storage::move($data->file_link.$data->file_name, $newPath.DIRECTORY_SEPARATOR.$data->file_name); |
114
|
|
|
} |
115
|
2 |
|
catch(\Exception $e) |
116
|
|
|
{ |
117
|
2 |
|
report($e); |
118
|
2 |
|
return false; |
119
|
|
|
} |
120
|
4 |
|
Log::info('Moved file '.$data->file_name.'from '.$data->file_link.' to new location - '.$newPath.'. File Details - ', array($data)); |
121
|
|
|
|
122
|
|
|
// Update file link in DB |
123
|
4 |
|
$data->update([ |
124
|
4 |
|
'file_link' => $newPath.DIRECTORY_SEPARATOR |
125
|
|
|
]); |
126
|
|
|
|
127
|
4 |
|
return true; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|