|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Support\Facades\Storage; |
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
|
8
|
|
|
use Illuminate\Support\Facades\Auth; |
|
9
|
|
|
|
|
10
|
|
|
class Files extends Model |
|
11
|
|
|
{ |
|
12
|
|
|
protected $primaryKey = 'file_id'; |
|
13
|
|
|
protected $fillable = ['file_name', 'file_link', 'mime_type']; |
|
14
|
|
|
|
|
15
|
|
|
public function systemFiles() |
|
16
|
|
|
{ |
|
17
|
|
|
return $this->hasMany('App\SystemFiles', 'file_id', 'file_id'); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// public function fileLinkFiles() |
|
21
|
|
|
// { |
|
22
|
|
|
// return $this->hasOne('App\FileLinkFiles', 'file_id', 'file_id'); |
|
23
|
|
|
// } |
|
24
|
|
|
|
|
25
|
|
|
public function fileLinkNotes() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->hasOne('App\FileLinkNotes', 'file_id', 'file_id'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// Remove any illegal characters from filename and make sure it is unique |
|
31
|
|
|
public static function cleanFileName($path, $fileName) |
|
32
|
|
|
{ |
|
33
|
|
|
// Remove all spaces |
|
34
|
|
|
$fileName = str_replace(' ', '_', $fileName); |
|
35
|
|
|
|
|
36
|
|
|
// Determine if the filename already exists |
|
37
|
|
|
if(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName)) |
|
38
|
|
|
{ |
|
39
|
|
|
$fileParts = pathinfo($fileName); |
|
40
|
|
|
$extension = isset($fileParts['extension']) ? ('.'.$fileParts['extension']) : ''; |
|
41
|
|
|
|
|
42
|
|
|
// Look to see if a number is already appended to a file. (example - file(1).pdf) |
|
43
|
|
|
if(preg_match('/(.*?)(\d+)$/', $fileParts['filename'], $match)) |
|
44
|
|
|
{ |
|
45
|
|
|
// Has a number, increment it |
|
46
|
|
|
$base = $match[1]; |
|
47
|
|
|
$number = intVal($match[2]); |
|
48
|
|
|
} |
|
49
|
|
|
else |
|
50
|
|
|
{ |
|
51
|
|
|
// No number, add one |
|
52
|
|
|
$base = $fileParts['filename']; |
|
53
|
|
|
$number = 0; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
// Increase the number until one that is not in use is found |
|
57
|
|
|
do |
|
58
|
|
|
{ |
|
59
|
|
|
$fileName = $base.'('.++$number.')'.$extension; |
|
60
|
|
|
} while(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName)); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return $fileName; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
// Delete a file from both the database and file system |
|
67
|
|
|
public static function deleteFile($fileID) |
|
68
|
|
|
{ |
|
69
|
|
|
try |
|
70
|
|
|
{ |
|
71
|
|
|
// Try to delete file from database - will throw error if foreign key is in use |
|
72
|
|
|
$fileData = Files::find($fileID); |
|
73
|
|
|
$fileLink = $fileData->file_link.$fileData->file_name; |
|
74
|
|
|
$fileData->delete(); |
|
75
|
|
|
} |
|
76
|
|
|
catch(\Illuminate\Database\QueryException $e) |
|
77
|
|
|
{ |
|
78
|
|
|
// Unable to remove file from the database |
|
79
|
|
|
Log::info('File Unable to delete - '.$e, ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
|
|
|
|
|
|
80
|
|
|
return false; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
// Delete the file from the storage system |
|
84
|
|
|
Storage::delete($fileLink); |
|
85
|
|
|
|
|
86
|
|
|
Log::info('File Deleted', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
|
87
|
|
|
return true; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|