1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\Log; |
6
|
|
|
use Illuminate\Support\Facades\Auth; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Illuminate\Support\Facades\Storage; |
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->belongsTo('App\FileLinkFiles', 'file_id', 'file_id'); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
// public function fileLinkNotes() |
26
|
|
|
// { |
27
|
|
|
// return $this->belongsTo()('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
|
|
|
$fileLink = ''; |
70
|
|
|
try |
71
|
|
|
{ |
72
|
|
|
// Try to delete file from database - will throw error if foreign key is in use |
73
|
|
|
$fileData = Files::find($fileID); |
74
|
|
|
$fileLink = $fileData->file_link.$fileData->file_name; |
|
|
|
|
75
|
|
|
$fileData->delete(); |
76
|
|
|
} |
77
|
|
|
catch(\Illuminate\Database\QueryException $e) |
78
|
|
|
{ |
79
|
|
|
// Unable to remove file from the database |
80
|
|
|
Log::info('File Unable to delete - '.$e, ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// Delete the file from the storage system |
85
|
|
|
Storage::delete($fileLink); |
86
|
|
|
|
87
|
|
|
Log::info('File Deleted', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.