Passed
Pull Request — master (#77)
by Ron
41:51 queued 13:39
created

GuestLinksController::show()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 35
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 20
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 35
ccs 21
cts 21
cp 1
crap 7
rs 8.6666
1
<?php
2
3
namespace App\Http\Controllers\FileLinks;
4
5
use App\User;
6
use App\Files;
7
use App\FileLinks;
8
use App\FileLinkFiles;
9
use Illuminate\Http\Request;
10
use Illuminate\Http\UploadedFile;
11
use Illuminate\Support\Facades\Log;
12
use App\Notifications\NewFileUpload;
13
use App\Http\Controllers\Controller;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Notification;
16
use App\Http\Resources\FileLinkFilesCollection;
17
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
18
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
19
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
20
21
class GuestLinksController extends Controller
22
{
23
    //  Landing page if no link is sent
24 2
    public function index()
25
    {
26 2
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
27 2
        return view('links.guestIndex');
28
    }
29
30
    //  Show the link details for the user
31 12
    public function show($id)
32
    {
33 12
        $details = FileLinks::where('link_hash', $id)->first();
34
35
        //  Verify that the link is valid
36 12
        if(empty($details))
37
        {
38 2
            Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id);
39 2
            return view('links.guestBadLink');
40
        }
41
        //  Verify that the link has not expired
42 10
        else if($details->expire <= date('Y-m-d'))
43
        {
44 2
            Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id);
45 2
            return view('links.guestExpiredLink');
46
        }
47
48
        //  Link is valid - determine if the link has files that can be downloaded
49 8
        $files = FileLinkFiles::where('link_id', $details->link_id)
50 8
            ->where('upload', false)
51 8
            ->count();
52
53 8
        if($files == 0 && $details->allow_upload === 'No')
54
        {
55 2
            Log::warning('Visitor ' . \Request::ip() . ' visited a link that they cannot do anything with.  Hash - ' . $id);
56 2
            return view('links.guestDeadLink');
57
        }
58
59 6
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
60 6
        Log::debug('Link Hash-'.$id);
61 6
        return view('links.guestDetails', [
62 6
            'hash'    => $id,
63 6
            'details' => $details,
64 6
            'hasFiles' => $files > 0 ? true : false,
65 6
            'allowUp' => $details->allow_upload === 'Yes' ? true : false,
66
        ]);
67
    }
68
69
    //  Get the guest available files for the link
70 6
    public function getFiles($id)
71
    {
72 6
        $linkID = FileLinks::where('link_hash', $id)->first()->link_id;
73
74 6
        $files = new FileLinkFilesCollection(
75 6
            FileLinkFiles::where('link_id', $linkID)
76 6
                ->where('upload', 0)
77 6
                ->orderBy('created_at', 'ASC')
78 6
                ->with('Files')
79 6
                ->get()
80
        );
81
82 6
        return $files;
83
    }
84
85
    //  Upload new file
86 6
    public function update(Request $request, $id)
87
    {
88 6
        $request->validate(['name' => 'required', 'file' => 'required']);
89
90 6
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
91
92
        //  Verify that the upload is valid and being processed
93 6
        if($receiver->isUploaded() === false)
94
        {
95
            Log::error('Upload File Missing - ' .
96
            /** @scrutinizer ignore-type */
97
            $request->toArray());
98
            throw new UploadMissingFileException();
99
        }
100
101
        //  Recieve and process the file
102 6
        $save = $receiver->receive();
103
104
        //  See if the uploade has finished
105 6
        if($save->isFinished())
106
        {
107 6
            $this->saveFile($save->getFile(), $id, $request);
108
109 6
            return 'uploaded successfully';
110
        }
111
112
        //  Get the current progress
113
        $handler = $save->handler();
114
115
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
116
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
117
        return response()->json([
118
            'done'   => $handler->getPercentageDone(),
119
            'status' => true
120
        ]);
121
    }
122
123
    //  Save the file in the database
124 6
    private function saveFile(UploadedFile $file, $id, $request)
125
    {
126 6
        $details = FileLinks::where('link_hash', $id)->first();
127 6
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id;
128
129 6
        $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
130 6
        $file->storeAs($filePath, $fileName);
131
132
        //  Place file in Files table of DB
133 6
        $newFile = Files::create([
134 6
            'file_name' => $fileName,
135 6
            'file_link' => $filePath.DIRECTORY_SEPARATOR
136
        ]);
137 6
        $fileID = $newFile->file_id;
138
139
        //  Place the file in the file link files table of DB
140 6
        FileLinkFiles::create([
141 6
            'link_id'  => $details->link_id,
142 6
            'file_id'  => $fileID,
143 6
            'added_by' => $request->name,
144 6
            'upload'   => 1,
145 6
            'note'     => $request->note
146
        ]);
147
148 6
        Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id);
149 6
        Log::debug('File Data -', $request->toArray());
150
151 6
        return response()->json(['success' => true]);
152
    }
153
154
    //  Notify the owner of the link that files were uploaded
155 2
    public function notify(Request $request, $id)
156
    {
157 2
        $request->validate([
158 2
            '_complete' => 'required',
159
            'count'     => 'required|integer'
160
        ]);
161
162 2
        $details = FileLinks::where('link_hash', $id)->first();
163
164 2
        $user = User::find($details->user_id);
165 2
        Notification::send($user, new NewFileUpload($details));
166
167 2
        Log::debug('Notification of file upload sent to User ID - '.$user->user_id);
168 2
    }
169
}
170