Test Failed
Push — dev5 ( 054b32...4c0df5 )
by Ron
12:54
created

FileLinksController::details()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 12
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 21
ccs 0
cts 13
cp 0
crap 6
rs 9.8666
1
<?php
2
3
namespace App\Http\Controllers\FileLinks;
4
5
use App\Files;
6
use Carbon\Carbon;
7
use App\FileLinks;
8
use App\FileLinkFiles;
9
use App\CustomerFileTypes;
10
use Illuminate\Support\Str;
11
use Illuminate\Http\Request;
12
use Illuminate\Http\UploadedFile;
13
use Illuminate\Support\Facades\Log;
14
use Illuminate\Support\Facades\Auth;
15
use App\Http\Controllers\Controller;
16
use Illuminate\Support\Facades\Route;
17
use Illuminate\Support\Facades\Storage;
18
use App\Http\Resources\FileLinksCollection;
19
use App\Http\Resources\CustomerFileTypesCollection;
20
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
21
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
22
use App\Http\Resources\FileLinks as FileLinksResource;
23
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
24
25
class FileLinksController extends Controller
26
{
27
    private $user;
28
29
    //  Only authorized users have access
30 76
    public function __construct()
31
    {
32
        //  Verify the user is logged in and has permissions for this page
33 76
        $this->middleware('auth');
34
        $this->middleware(function($request, $next) {
35
            $this->user = auth()->user();
36
            $this->authorize('hasAccess', 'Use File Links');
37
            return $next($request);
38 76
        });
39 76
    }
40
41
    //  Landing page shows all links that the user owns
42
    public function index()
43
    {
44
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
45
        return view('links.index');
46
    }
47
48
    //  Ajax call to show the links for a specific user
49
    public function find($id)
50
    {
51
        //  Verify if the user is trying to pull their own links
52
        if($id == 0)
53
        {
54
            $id = Auth::user()->user_id;
55
        }
56
        //  If the user is trying to pull someone elses links, they must be able to manage users
57
        else if ($id != Auth::user()->user_id)
58
        {
59
            $this->authorize('hasAccess', 'manage_users');
60
        }
61
62
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
63
        $links = new FileLinksCollection(
64
            FileLinks::where('user_id', $id)
65
                ->withCount('FileLinkFiles')
66
                ->orderBy('expire', 'desc')->get()
67
        );
68
69
        return $links;
70
    }
71
72
    //  Create a new file link form
73
    public function create()
74
    {
75
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
76
        return view('links.newLink');
77
    }
78
79
    //  Submit the new file link form
80
    public function store(Request $request)
81
    {
82
        $request->validate([
83
            'name'       => 'required',
84
            'expire'     => 'required',
85
            'customerID' => 'exists:customers,cust_id|nullable'
86
        ]);
87
88
        if(!empty($request->file))
89
        {
90
            $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
91
92
            //  Verify that the upload is valid and being processed
93
            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
            $save = $receiver->receive();
103
104
            //  See if the uploade has finished
105
            if($save->isFinished())
106
            {
107
                $this->saveFile($save->getFile());
108
109
                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-'.Auth::user()->user_id);
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
        //  If there are no files being uploaded or the file uploade process is done
124
        if(isset($request->_completed) && $request->_completed)
125
        {
126
            Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
127
            Log::debug('Link data submitted.  Data - ',
128
            /** @scrutinizer ignore-type */
129
            $request->toArray());
130
            $linkID = $this->createLink($request);
131
            if($request->session()->has('newLinkFile'))
132
            {
133
                //  If there were files uploaded to the link, process them
134
                $files = session('newLinkFile');
135
                $path = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$linkID;
136
137
                foreach($files as $file)
138
                {
139
                    $data = Files::find($file);
140
                    //  Move the file to the proper folder
141
                    Storage::move($data->file_link.$data->file_name, $path.DIRECTORY_SEPARATOR.$data->file_name);
142
                    //  Update file link in DB
143
                    $data->update([
144
                        'file_link' => $path.DIRECTORY_SEPARATOR
145
                    ]);
146
                    //  Attach file to the link
147
                    FileLinkFiles::create([
148
                        'link_id' => $linkID,
149
                        'file_id' => $data->file_id,
150
                        'user_id' => Auth::user()->user_id,
151
                        'upload' => 0
152
                    ]);
153
154
                    Log::debug('File Added for link ID-'.$linkID.'.  File ID-'.$data->file_id);
155
                }
156
157
                $request->session()->forget('newLinkFile');
158
            }
159
160
            return response()->json(['link' => $linkID, 'name' => Str::slug($request->name)]);
161
        }
162
163
        return response()->json(['complete' => false]);
164
    }
165
166
    //  Create the new file link
167
    private function createLink($data)
168
    {
169
        //  Generate a random hash to use as the file link and make sure it is not already in use
170
        do
171
        {
172
            $hash = strtolower(Str::random(15));
173
            $dup  = FileLinks::where('link_hash', $hash)->get()->count();
174
        } while($dup != 0);
175
176
        //  Create the new file link
177
        $link = FileLinks::create([
178
            'user_id'      => Auth::user()->user_id,
179
            'cust_id'      => $data->customerID,
180
            'link_hash'    => $hash,
181
            'link_name'    => $data->name,
182
            'expire'       => $data->expire,
183
            'allow_upload' => isset($data->allowUp) && $data->allowUp ? true : false,
184
            'note'         => $data->note
185
        ]);
186
187
        Log::info('File Link Created for User ID-'.Auth::user()->user_id.'.  Link ID-'.$link->link_id);
188
189
        return $link->link_id;
190
    }
191
192
    //  Save a file attached to the link
193
    private function saveFile(UploadedFile $file)
194
    {
195
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.'_tmp';
196
197
        //  Clean the file and store it
198
        $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
199
        $file->storeAs($filePath, $fileName);
200
201
        //  Place file in Files table of DB
202
        $newFile = Files::create([
203
            'file_name' => $fileName,
204
            'file_link' => $filePath.DIRECTORY_SEPARATOR
205
        ]);
206
        $fileID = $newFile->file_id;
207
208
        //  Save the file ID in the session array
209
        $fileArr = session('newLinkFile') != null ? session('newLinkFile') : [];
210
        $fileArr[] = $fileID;
211
        session(['newLinkFile' => $fileArr]);
212
213
        //  Log stored file
214
        Log::info('File Stored', ['file_id' => $fileID, 'file_path' => $filePath.DIRECTORY_SEPARATOR.$fileName]);
215
        return $fileID;
216
    }
217
218
    //  Show details about a file link
219
    public function details($id, /** @scrutinizer ignore-unused */ $name)
220
    {
221
        //  Verify that the link is a valid link
222
        $linkData = FileLinks::find($id);
223
        $fileTypes = new CustomerFileTypesCollection(CustomerFileTypes::all());
224
225
        //  If the link is invalid, return an error page
226
        if(empty($linkData))
227
        {
228
            Log::warning('User tried to view bad file link', ['user_id' => Auth::user()->user_id, 'link_id' => $id]);
229
            return view('links.badLink');
230
        }
231
232
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
233
        Log::debug('Link Detials - ',
234
        /** @scrutinizer ignore-type */
235
        $linkData->toArray());
236
        return view('links.details', [
237
            'link_id'    => $linkData->link_id,
238
            'cust_id'    => $linkData->cust_id,
239
            'file_types' => $fileTypes
240
        ]);
241
    }
242
243
    //  Ajax call te get JSON details of the link
244
    public function show($id)
245
    {
246
        $linkData = new FileLinksResource(FileLinks::find($id));
247
248
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
249
        return $linkData;
250
    }
251
252
    //  Update the link's details
253
    public function update(Request $request, $id)
254
    {
255
        $request->validate([
256
            'name'       => 'required',
257
            'expire'     => 'required',
258
            'customerTag' => 'exists:customers,cust_id|nullable'
259
        ]);
260
261
        FileLinks::find($id)->update([
262
            'link_name'    => $request->name,
263
            'expire'       => $request->expire,
264
            'allow_upload' => isset($request->allowUpload) && $request->allowUpload ? true : false,
265
            'cust_id'      => $request->customerTag,
266
            'note'         => $request->hasInstructions ? $request->instructions : null
267
        ]);
268
269
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
270
        Log::debug('Updated Link Data for link ID-'.$id,
271
        /** @scrutinizer ignore-type */
272
        $request->toArray());
273
        Log::info('File Link Updated', ['link_id' => $id]);
274
275
        return response()->json(['success' => true]);
276
    }
277
278
    //  Disable a file linke, but do not remove it (set the expire date to a previous date)
279
    public function disableLink($id)
280
    {
281
        //  update the expire date for the link
282
        FileLinks::find($id)->update([
283
            'expire' => Carbon::yesterday()
284
        ]);
285
286
        Log::info('User ID '.Auth::user()->user_id.' disabled link ID - '.$id);
287
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
288
        return response()->json(['success' => true]);
289
    }
290
291
    //  Delete a file link
292
    public function destroy($id)
293
    {
294
        //  Remove the file from database
295
        $data = FileLinkFiles::where('link_id', $id)->get();
296
        if(!$data->isEmpty())
297
        {
298
            foreach($data as $file)
299
            {
300
                $fileID = $file->file_id;
301
                $file->delete();
302
303
                //  Delete the file if it is no longer in use
304
                Files::deleteFile($fileID);
305
            }
306
        }
307
308
        FileLinks::find($id)->delete();
309
310
        Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id);
311
        Log::info('File link deleted', ['link_id' => $id, 'user_id' => Auth::user()->user_id]);
312
313
        return response()->json([
314
            'success' => true
315
        ]);
316
    }
317
}
318