Passed
Push — dev5 ( 1f3b64...3a6272 )
by Ron
06:33
created

GuestLinksController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 69
c 0
b 0
f 0
dl 0
loc 143
rs 10
ccs 0
cts 72
cp 0
wmc 14

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getFiles() 0 13 1
A notify() 0 11 1
A index() 0 4 1
A saveFile() 0 28 1
B show() 0 35 7
A update() 0 32 3
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\FileLinkNotes;
0 ignored issues
show
Bug introduced by
The type App\FileLinkNotes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use App\FileLinkFiles;
10
use Illuminate\Http\Request;
11
use Illuminate\Http\UploadedFile;
12
use Illuminate\Support\Facades\Log;
13
use App\Notifications\NewFileUpload;
14
use App\Http\Controllers\Controller;
15
use Illuminate\Support\Facades\Route;
16
use Illuminate\Support\Facades\Notification;
17
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
18
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
19
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
20
21
use App\http\Resources\FileLinkFilesCollection;
22
23
24
class GuestLinksController extends Controller
25
{
26
    //  Landing page if no link is sent
27
    public function index()
28
    {
29
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
30
        return view('links.userIndex');
31
    }
32
33
    //  Show the link details for the user
34
    public function show($id)
35
    {
36
        $details = FileLinks::where('link_hash', $id)->first();
37
38
        //  Verify that the link is valid
39
        if(empty($details))
40
        {
41
            Log::warning('Visitor '.\Request::ip().' visited bad link Hash - '.$id);
42
            return view('links.guestBadLink');
43
        }
44
        //  Verify that the link has not expired
45
        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...
46
        {
47
            Log::warning('Visitor '.\Request::ip().' visited expired link Hash - '.$id);
48
            return view('links.guestExpiredLink');
49
        }
50
51
        //  Link is valid - determine if the link has files that can be downloaded
52
        $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...
53
            ->where('upload', false)
54
            ->count();
55
56
        if($files == 0 && $details->allow_upload === 'No')
57
        {
58
            Log::warning('Visitor ' . \Request::ip() . ' visited a link that they cannot do anything with.  Hash - ' . $id);
59
            return view('links.guestDeadLink');
60
        }
61
62
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
63
        Log::debug('Link Hash-'.$id);
64
        return view('links.guestDetails', [
65
            'hash'    => $id,
66
            'details' => $details,
67
            'hasFiles' => $files > 0 ? true : false,
68
            'allowUp' => $details->allow_upload === 'Yes' ? true : false,
69
        ]);
70
    }
71
72
    //  Get the guest available files for the link
73
    public function getFiles($id)
74
    {
75
        $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...
76
77
        $files = new FileLinkFilesCollection(
78
            FileLinkFiles::where('link_id', $linkID)
79
                ->where('upload', 0)
80
                ->orderBy('created_at', 'ASC')
81
                ->with('Files')
82
                ->get()
83
        );
84
85
        return $files;
86
    }
87
88
    //  Upload new file
89
    public function update(Request $request, $id)
90
    {
91
        $request->validate(['name' => 'required', 'file' => 'required']);
92
93
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
94
95
        //  Verify that the upload is valid and being processed
96
        if($receiver->isUploaded() === false)
97
        {
98
            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

98
            Log::error('Upload File Missing - './** @scrutinizer ignore-type */ $request->toArray());
Loading history...
99
            throw new UploadMissingFileException();
100
        }
101
102
        //  Recieve and process the file
103
        $save = $receiver->receive();
104
105
        //  See if the uploade has finished
106
        if($save->isFinished())
107
        {
108
            $this->saveFile($save->getFile(), $id, $request);
109
110
            return 'uploaded successfully';
111
        }
112
113
        //  Get the current progress
114
        $handler = $save->handler();
115
116
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.\Request::ip());
117
        Log::debug('File being uploaded.  Percentage done - '.$handler->getPercentageDone());
118
        return response()->json([
119
            'done'   => $handler->getPercentageDone(),
120
            'status' => true
121
        ]);
122
    }
123
124
    //  Save the file in the database
125
    private function saveFile(UploadedFile $file, $id, $request)
126
    {
127
        $details = FileLinks::where('link_hash', $id)->first();
128
        $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...
129
130
        $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
131
        $file->storeAs($filePath, $fileName);
132
133
        //  Place file in Files table of DB
134
        $newFile = Files::create([
135
            'file_name' => $fileName,
136
            'file_link' => $filePath.DIRECTORY_SEPARATOR
137
        ]);
138
        $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...
139
140
        //  Place the file in the file link files table of DB
141
        FileLinkFiles::create([
142
            'link_id'  => $details->link_id,
143
            'file_id'  => $fileID,
144
            'added_by' => $request->name,
145
            'upload'   => 1,
146
            'note'     => $request->note
147
        ]);
148
149
        Log::info('File uploaded by guest '.\Request::ip().' for file link -'.$details->link_id);
150
        Log::debug('File Data -', $request->toArray());
151
152
        return response()->json(['success' => true]);
153
    }
154
155
    //  Notify the owner of the link that files were uploaded
156
    public function notify(Request $request, $id)
157
    {
158
        $request->validate([
159
            '_complete' => 'required',
160
            'count'     => 'required|integer'
161
        ]);
162
163
        $details = FileLinks::where('link_hash', $id)->first();
164
165
        $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...
166
        Notification::send($user, new NewFileUpload($details));
167
    }
168
}
169