Passed
Branch dev5 (c43caf)
by Ron
07:00
created

GuestLinksController::update()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.9148

Importance

Changes 0
Metric Value
cc 3
eloc 15
c 0
b 0
f 0
nc 3
nop 2
dl 0
loc 32
ccs 8
cts 15
cp 0.5333
crap 3.9148
rs 9.7666
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'))
1 ignored issue
show
Bug introduced by
The property expire does not seem to exist on App\FileLinks. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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)
1 ignored issue
show
Bug introduced by
The property link_id does not seem to exist on App\FileLinks. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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;
1 ignored issue
show
Bug introduced by
The property link_id does not seem to exist on App\FileLinks. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
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 - '.$request->toArray());
0 ignored issues
show
Bug introduced by
Are you sure $request->toArray() of type array can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

95
            Log::error('Upload File Missing - './** @scrutinizer ignore-type */ $request->toArray());
Loading history...
96
            throw new UploadMissingFileException();
97
        }
98
99
        //  Recieve and process the file
100 6
        $save = $receiver->receive();
101
102
        //  See if the uploade has finished
103 6
        if($save->isFinished())
104
        {
105 6
            $this->saveFile($save->getFile(), $id, $request);
106
107 6
            return 'uploaded successfully';
108
        }
109
110
        //  Get the current progress
111
        $handler = $save->handler();
112
113
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
114
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
115
        return response()->json([
116
            'done'   => $handler->getPercentageDone(),
117
            'status' => true
118
        ]);
119
    }
120
121
    //  Save the file in the database
122 6
    private function saveFile(UploadedFile $file, $id, $request)
123
    {
124 6
        $details = FileLinks::where('link_hash', $id)->first();
125 6
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id;
1 ignored issue
show
Bug introduced by
The property link_id does not seem to exist on App\FileLinks. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
126
127 6
        $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
128 6
        $file->storeAs($filePath, $fileName);
129
130
        //  Place file in Files table of DB
131 6
        $newFile = Files::create([
132 6
            'file_name' => $fileName,
133 6
            'file_link' => $filePath.DIRECTORY_SEPARATOR
134
        ]);
135 6
        $fileID = $newFile->file_id;
1 ignored issue
show
Bug introduced by
The property file_id does not seem to exist on App\Files. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
136
137
        //  Place the file in the file link files table of DB
138 6
        FileLinkFiles::create([
139 6
            'link_id'  => $details->link_id,
140 6
            'file_id'  => $fileID,
141 6
            'added_by' => $request->name,
142 6
            'upload'   => 1,
143 6
            'note'     => $request->note
144
        ]);
145
146 6
        Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id);
147 6
        Log::debug('File Data -', $request->toArray());
148
149 6
        return response()->json(['success' => true]);
150
    }
151
152
    //  Notify the owner of the link that files were uploaded
153 2
    public function notify(Request $request, $id)
154
    {
155 2
        $request->validate([
156 2
            '_complete' => 'required',
157
            'count'     => 'required|integer'
158
        ]);
159
160 2
        $details = FileLinks::where('link_hash', $id)->first();
161
162 2
        $user = User::find($details->user_id);
1 ignored issue
show
Bug introduced by
The property user_id does not seem to exist on App\FileLinks. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
163 2
        Notification::send($user, new NewFileUpload($details));
164
165 2
        Log::debug('Notification of file upload sent to User ID - '.$user->user_id);
166 2
    }
167
}
168