Completed
Push — dev5 ( 096a7b...37965d )
by Ron
09:47
created

UserLinksController   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 117
rs 10
c 0
b 0
f 0
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A index() 0 3 1
A saveFile() 0 37 2
A show() 0 34 4
A update() 0 29 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;
9
use App\FileLinkFiles;
10
use Illuminate\Http\Request;
11
use Illuminate\Http\UploadedFile;
12
use Illuminate\Support\Facades\Log;
13
use Illuminate\Support\Facades\Auth;
14
use App\Notifications\NewFileUpload;
15
use App\Http\Controllers\Controller;
16
use Illuminate\Support\Facades\Storage;
17
use Illuminate\Support\Facades\Notification;
18
use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;
19
use Pion\Laravel\ChunkUpload\Handler\HandlerFactory;
20
use Pion\Laravel\ChunkUpload\Handler\AbstractHandler;
21
use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException;
22
23
class UserLinksController extends Controller
24
{
25
    //  Landing page if no link is sent
26
    public function index()
27
    {
28
        return view('links.userIndex');
29
    }
30
31
    //  Show the link details for the user
32
    public function show($id)
33
    {
34
        $details = FileLinks::where('link_hash', $id)->first();
35
        
36
        //  Verify that the link is valid
37
        if(empty($details))
38
        {
39
            return view('links.userBadLink');
40
        }
41
        //  Verify that the link has not expired
42
        else if($details->expire <= date('Y-m-d'))
43
        {
44
            return view('links.userExpiredLink');
45
        }
46
        
47
        $files = FileLinkFiles::where('link_id', $details->link_id)
48
            ->where('upload', false)
49
            ->join('files', 'file_link_files.file_id', '=', 'files.file_id')
50
            ->get();
51
        
52
        //  Gather the array for the "download all link" and update the time stamp for a more readable format
53
        $fileArr = [];
54
        foreach($files as $file)
55
        {
56
            $fileArr[]       = $file->file_id;
57
            $file->timestamp = date('M d, Y', strtotime($file->created_at));
58
        }
59
        
60
        return view('links.userDetails', [
61
            'hash'    => $id,
62
            'details' => $details,
63
            'files'   => $files,
64
            'allowUp' => $details->allow_upload,
65
            'fileArr' => $fileArr
66
        ]);
67
    }
68
69
    //  Upload new file
70
    public function update(Request $request, $id)
71
    {        
72
        $request->validate(['name' => 'required', 'file' => 'required']);
73
        
74
        $receiver = new FileReceiver('file', $request, HandlerFactory::classFromRequest($request));
75
            
76
        //  Verify that the upload is valid and being processed
77
        if($receiver->isUploaded() === false)
78
        {
79
            throw new UploadMissingFileException();
80
        }
81
82
        //  Recieve and process the file
83
        $save = $receiver->receive();
84
85
        //  See if the uploade has finished
86
        if($save->isFinished())
87
        {
88
            $fileID = $this->saveFile($save->getFile(), $id, $request);
0 ignored issues
show
Unused Code introduced by
The assignment to $fileID is dead and can be removed.
Loading history...
89
90
            return 'uploaded successfully';
91
        }
92
93
        //  Get the current progress
94
        $handler = $save->handler();
95
96
        return response()->json([
97
            'done'   => $handler->getPercentageDone(),
98
            'status' => true
99
        ]);
100
    }
101
    
102
    //  Save the file in the database
103
    private function saveFile(UploadedFile $file, $id, $request)
104
    {        
105
        $details = FileLinks::where('link_hash', $id)->first();
106
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id;
107
        
108
        $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
109
        $file->storeAs($filePath, $fileName);
110
111
        //  Place file in Files table of DB
112
        $newFile = Files::create([
113
            'file_name' => $fileName,
114
            'file_link' => $filePath.DIRECTORY_SEPARATOR
115
        ]);
116
        $fileID = $newFile->file_id;
117
118
        //  Place the file in the file link files table of DB
119
        FileLinkFiles::create([
120
            'link_id'  => $details->link_id,
121
            'file_id'  => $fileID,
122
            'added_by' => $request->name,
123
            'upload'   => 1
124
        ]);
125
126
        if(!empty($request->note))
127
        {
128
            FileLinkNotes::create([
129
                'link_id' => $details->link_id,
130
                'file_id' => $fileID,
131
                'note'    => $request->note
132
            ]);
133
        }
134
135
        //  Send email and notification to the creator of the link
136
        $user = User::find($details->user_id);
137
        Notification::send($user, new NewFileUpload($details));
138
139
        return response()->json(['success' => true]);
140
    }
141
}
142