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
|
|
|
// Remove any illegal characters from filename and make sure it is unique |
26
|
10 |
|
public static function cleanFileName($path, $fileName) |
27
|
|
|
{ |
28
|
|
|
// Remove all spaces |
29
|
10 |
|
$fileName = str_replace(' ', '_', $fileName); |
30
|
|
|
|
31
|
|
|
// Determine if the filename already exists |
32
|
10 |
|
if(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName)) |
33
|
|
|
{ |
34
|
|
|
$fileParts = pathinfo($fileName); |
35
|
|
|
$extension = isset($fileParts['extension']) ? ('.'.$fileParts['extension']) : ''; |
36
|
|
|
|
37
|
|
|
// Look to see if a number is already appended to a file. (example - file(1).pdf) |
38
|
|
|
if(preg_match('/(.*?)(\d+)$/', $fileParts['filename'], $match)) |
39
|
|
|
{ |
40
|
|
|
// Has a number, increment it |
41
|
|
|
$base = $match[1]; |
42
|
|
|
$number = intVal($match[2]); |
43
|
|
|
} |
44
|
|
|
else |
45
|
|
|
{ |
46
|
|
|
// No number, add one |
47
|
|
|
$base = $fileParts['filename']; |
48
|
|
|
$number = 0; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Increase the number until one that is not in use is found |
52
|
|
|
do |
53
|
|
|
{ |
54
|
|
|
$fileName = $base.'('.++$number.')'.$extension; |
55
|
|
|
} while(Storage::exists($path.DIRECTORY_SEPARATOR.$fileName)); |
56
|
|
|
} |
57
|
|
|
|
58
|
10 |
|
return $fileName; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Delete a file from both the database and file system |
62
|
4 |
|
public static function deleteFile($fileID) |
63
|
|
|
{ |
64
|
4 |
|
$fileLink = ''; |
65
|
|
|
try |
66
|
|
|
{ |
67
|
|
|
// Try to delete file from database - will throw error if foreign key is in use |
68
|
4 |
|
$fileData = Files::find($fileID); |
69
|
4 |
|
$fileLink = $fileData->file_link.$fileData->file_name; |
70
|
4 |
|
$fileData->delete(); |
71
|
|
|
} |
72
|
|
|
catch(\Illuminate\Database\QueryException $e) |
73
|
|
|
{ |
74
|
|
|
// Unable to remove file from the database |
75
|
|
|
Log::info('File Unable to delete - '.$e, ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
76
|
|
|
return false; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// Delete the file from the storage system |
80
|
4 |
|
Storage::delete($fileLink); |
81
|
|
|
|
82
|
4 |
|
Log::info('File Deleted', ['file_id' => $fileID, 'file_name' => $fileLink, 'user_id' => Auth::user()->user_id]); |
83
|
4 |
|
return true; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|