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 Zip; |
10
|
|
|
use Carbon\Carbon; |
11
|
|
|
|
12
|
|
|
use App\Files; |
13
|
|
|
|
14
|
|
|
class DownloadDomain |
15
|
|
|
{ |
16
|
|
|
protected $file, $user, $file_id, $file_name; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
if(Auth::check()) |
21
|
|
|
{ |
22
|
|
|
$this->user = Auth::user()->full_name; |
23
|
|
|
} |
24
|
|
|
else |
25
|
|
|
{ |
26
|
|
|
$this->user = \Request::ip(); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function setFileDetails($fileID, $fileName) |
31
|
|
|
{ |
32
|
|
|
$this->file_id = $fileID; |
33
|
|
|
$this->file_name = $fileName; |
34
|
|
|
$this->file = Files::where('file_id', $fileID)->where('file_name', $fileName)->first(); |
35
|
|
|
Log::debug('File Details set for new file download by '.$this->user.'. Details - ', array($this->file)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getFileLinkForDownload() |
39
|
|
|
{ |
40
|
|
|
Log::info('User '.$this->user.' is downloading a file. Details - ', array($this->file)); |
41
|
|
|
return $this->file->file_link.$this->file->file_name; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Determine if the file is actually a valid file - name and ID must match |
45
|
|
|
public function isFileValid() |
46
|
|
|
{ |
47
|
|
|
$valid = $this->file ? true : false; |
48
|
|
|
if(!$valid) |
49
|
|
|
{ |
50
|
|
|
Log::error('User '.$this->user.' is attempting to download an invalid file. Details - ', ['File ID' => $this->file_id, 'File Name' => $this->file_name]); |
51
|
|
|
} |
52
|
|
|
else if(!Storage::exists($this->file->file_link.$this->file->file_name)) |
53
|
|
|
{ |
54
|
|
|
$valid = false; |
55
|
|
|
Log::error('User '.$this->user.' is attempting to download a missing file. Details - ', ['File ID' => $this->file_id, 'File Name' => $this->file_name, 'File Path' => $this->file->file_link]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $valid; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Determine if the file is public or if the user is logged in |
62
|
|
|
public function canUserDownload() |
63
|
|
|
{ |
64
|
|
|
if($this->file->public || Auth::check()) |
65
|
|
|
{ |
66
|
|
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
Log::error('User '.$this->user.' is attempting to download a file they do not have permission to download. Details - ', ['File ID' => $this->file_id, 'File Name' => $this->file_name]); |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Create an archive of files that the user can download in a single zip |
74
|
|
|
public function createArchive($fileArr) |
75
|
|
|
{ |
76
|
|
|
// Create the zip archive structure |
77
|
|
|
$rootPath = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR; |
78
|
|
|
$archivePath = 'archive_downloads'.DIRECTORY_SEPARATOR; |
79
|
|
|
$archiveName = 'zip_archive_'.Carbon::now()->timestamp.'.zip'; |
80
|
|
|
Storage::put($archivePath.'.ignore', ''); |
81
|
|
|
$absolutePath = $rootPath.$archivePath.$archiveName; |
82
|
|
|
|
83
|
|
|
// Create the zip archive |
84
|
|
|
$zip = Zip::create($absolutePath); |
85
|
|
|
Log::info('Zip archive '.$absolutePath.' opened for user '.$this->user); |
86
|
|
|
|
87
|
|
|
// Add files to the archive |
88
|
|
|
foreach($fileArr as $file) |
89
|
|
|
{ |
90
|
|
|
$this->setFileDetails($file['file_id'], $file['file_name']); |
91
|
|
|
if($this->isFileValid() && $this->canUserDownload()) |
92
|
|
|
{ |
93
|
|
|
$filePath = $this->file->file_link.$this->file->file_name; |
94
|
|
|
$zip->add($rootPath.$filePath); |
95
|
|
|
Log::info('File '.$filePath.' added to File Archive '.$archiveName.' by '.$this->user); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
// Close the zip archive |
100
|
|
|
$zip->close(); |
101
|
|
|
|
102
|
|
|
Log::info('Zip archive '.$archiveName.' creation completed'); |
103
|
|
|
return $archiveName; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
// Validate that a given archive name exists - returns the full path of the archive |
107
|
|
|
public function validateArchive($archiveName) |
108
|
|
|
{ |
109
|
|
|
$rootPath = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR; |
110
|
|
|
$archivePath = 'archive_downloads'.DIRECTORY_SEPARATOR; |
111
|
|
|
|
112
|
|
|
if(Storage::exists($archivePath.$archiveName)) |
113
|
|
|
{ |
114
|
|
|
return $rootPath.$archivePath.$archiveName; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return false; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
// Delete the archive after it has been downloaded |
121
|
|
|
public function deleteArchive($archiveName) |
122
|
|
|
{ |
123
|
|
|
Storage::delete('archive_downloads'.DIRECTORY_SEPARATOR.$archiveName); |
124
|
|
|
Log::debug('Archive '.$archiveName.' deleted after download'); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|