Passed
Push — master ( e9462d...446e6b )
by Ron
02:18
created

FileLinksController::update()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 1
nop 2
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Illuminate\Http\Response;
6
use Illuminate\Http\Request;
7
use Illuminate\Support\Facades\Auth;
8
use Illuminate\Support\Facades\Log;
9
use Zip;
10
use App\Files;
11
use App\FileLinks;
12
use App\FileLinkFiles;
13
use App\FileLinkNotes;
14
15
class FileLinksController extends Controller
16
{
17
    //  Only authorized users have access
18
    public function __construct()
19
    {
20
        $this->middleware('auth');
21
    }
22
    
23
    //  Landing page
24
    public function index()
25
    {
26
        return view('links.index');
27
    }
28
29
    //  Create a new File Link
30
    public function create()
31
    {
32
        return view('links.form.newLink');
33
    }
34
35
    //  Store the new file link
36
    public function store(Request $request)
37
    {
38
        $request->validate(['name' => 'required', 'expire' => 'required']);
39
        
40
        //  Generate a random hash to use as the file link and make sure it is not already in use
41
        do
42
        {
43
            $hash = strtolower(str_random(15));
44
            $dup = FileLinks::where('link_hash', $hash)->get()->count();
45
        }while($dup != 0);
46
        
47
        //  Create the new file link
48
        $link = FileLinks::create([
49
            'user_id'      => Auth::user()->user_id,
50
            'link_hash'    => $hash,
51
            'link_name'    => $request->name,
52
            'expire'       => $request->expire,
53
            'allow_upload' => isset($request->allowUp) && $request->allowUp ? true : false
54
        ]);
55
        $linkID = $link->link_id;
56
        
57
        //  If there are any files, process them
58
        if(!empty($request->file))
59
        {
60
            $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$linkID;
61
            foreach($request->file as $file)
62
            {
63
                //  Clean the file and store it
64
                $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
65
                $file->storeAs($filePath, $fileName);
66
                
67
                //  Place file in Files table of DB
68
                $newFile = Files::create([
69
                    'file_name' => $fileName,
70
                    'file_link' => $filePath.DIRECTORY_SEPARATOR
71
                ]);
72
                $fileID = $newFile->file_id;
73
                
74
                //  Place the file in the file link files table of DB
75
                FileLinkFiles::create([
76
                    'link_id'  => $linkID,
77
                    'file_id'  => $fileID,
78
                    'user_id'  => Auth::user()->user_id,
79
                    'upload'   => 0
80
                ]);
81
                
82
                //  Log stored file
83
                Log::info('File Stored', ['file_id' => $fileID, 'file_path' => $filePath.DIRECTORY_SEPARATOR.$fileName]);
84
            }
85
        }
86
        
87
        Log::info('File Link Created', ['link_id' => $linkID, 'user_id' => Auth::user()->user_id]);
88
        
89
        return $linkID;
90
    }
91
92
    //  Show file links for a specific user
93
    public function show($id)
94
    {
95
        $links = FileLinks::where('user_id', $id)
96
            ->withCount('FileLinkFiles')
97
            ->orderBy('expire', 'desc')
98
            ->get();
99
        
100
        return view('links.loadLinks', [
101
            'links' => $links
102
        ]);
103
    }
104
    
105
    //  Show a links information
106
    public function details($id, $name)
107
    {
108
        $linkData = FileLinks::find($id);
109
        
110
        if(empty($linkData))
111
        {
112
            return view('links.badLink');
113
        }
114
        
115
        $emailMsg = "mailto:?subject=A File Link Has Been Created For You&body=View the link details here:  ".route('userLink.details', ['link' => $linkData->link_hash]);
116
        
117
        return view('links.details', [
118
            'data' => $linkData,
119
            'emMsg' => $emailMsg
120
        ]);
121
    }
122
    
123
    //  Get the files for the link
124
    public function getFiles($type, $linkID)
125
    {
126
        $files = null;
127
        switch($type)
128
        {
129
            case 'down':
130
                $files = FileLInkFiles::where('link_id', $linkID)
131
                    ->where('upload', false)
132
                    ->join('files', 'file_link_files.file_id', '=', 'files.file_id')
133
                    ->get();
134
                break;
135
            case 'up':
136
                $files = Files::where('file_link_files.link_id', $linkID)
137
                    ->where('file_link_files.upload', true)
138
                    ->join('file_link_files', 'files.file_id', '=', 'file_link_files.file_id')
139
                    ->with('FileLinkNotes')
140
                    ->get();
141
                break;
142
        }
143
        
144
        return view('links.fileList', [
145
            'files'  => $files,
146
            'type'   => $type,
147
            'linkID' => $linkID
148
        ]);
149
    }
150
    
151
    //  Add a new file
152
    public function addFileForm($id)
153
    {
154
        return view('links.form.addFile', [
155
            'id' => $id
156
        ]);
157
    }
158
    
159
    //  Submit the additional files
160
    public function submitAddFile($id, Request $request)
161
    {
162
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$id;
163
        foreach($request->file as $file)
164
        {
165
            //  Clean the file and store it
166
            $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
167
            $file->storeAs($filePath, $fileName);
168
169
            //  Place file in Files table of DB
170
            $newFile = Files::create([
171
                'file_name' => $fileName,
172
                'file_link' => $filePath.DIRECTORY_SEPARATOR
173
            ]);
174
            $fileID = $newFile->file_id;
175
176
            //  Place the file in the file link files table of DB
177
            FileLinkFiles::create([
178
                'link_id'  => $id,
179
                'file_id'  => $fileID,
180
                'user_id'  => Auth::user()->user_id,
181
                'upload'   => 0
182
            ]);
183
            
184
            //  Log stored file
185
            Log::info('File Stored', ['file_id' => $fileID, 'file_path' => $filePath.DIRECTORY_SEPARATOR.$fileName]);
186
        }
187
    }
188
    
189
    //  Get a note that is attached to a file
190
    public function getNote($id)
191
    {
192
        $note = FileLinkNotes::find($id);
193
        
194
        return $note->note;
195
    }
196
197
    //  Edit a links basic informaiton
198
    public function edit($id)
199
    {
200
        $linkData = FileLinks::find($id);
201
        
202
        return view('links.form.editLink', [
203
            'data' => $linkData
204
        ]);
205
    }
206
    
207
    //  Download all files uploaded to a link
208
    public function downloadAllFiles($linkID)
209
    {
210
        $files = Files::where('file_link_files.link_id', $linkID)
211
            ->where('file_link_files.upload', true)
212
            ->join('file_link_files', 'files.file_id', '=', 'file_link_files.file_id')
213
            ->with('FileLinkNotes')
214
            ->get();
215
        $path = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR;
216
        
217
        $zip = Zip::create($path.'download.zip');
218
        foreach($files as $file)
219
        {
220
            $zip->add($path.$file->file_link.$file->file_name);
221
        }
222
        
223
        $zip->close();
224
        
225
        return response()->download($path.'download.zip')->deleteFileAfterSend(true);        
226
    }
227
228
    //  Submit the edit link form
229
    public function update(Request $request, $id)
230
    {
231
        $request->validate([
232
            'link_name' => 'required', 
233
            'expire'    => 'required'
234
        ]);
235
        
236
        FileLinks::find($id)->update([
237
            'link_name'    => $request->link_name,
238
            'expire'       => $request->expire,
239
            'allow_upload' => isset($request->allowUp) && $request->allowUp ? true : false
240
        ]);
241
        
242
        Log::info('File Link Updated', ['link_id' => $id]);
243
    }
244
    
245
    //  Delete a file attached to a link
246
    public function deleteLinkFile($linkFileID)
247
    {
248
        $fileData = FileLinkFiles::find($linkFileID);
249
        $fileID = $fileData->file_id;
250
        $fileData->delete();
251
        
252
        Files::deleteFile($fileID);
253
    }
254
255
    //  Delete a file link
256
    public function destroy($id)
257
    {
258
        //  Remove the file from database
259
        $data = FileLinkFiles::where('link_id', $id)->get();
260
        if(!$data->isEmpty())
261
        {
262
            foreach($data as $file)
263
            {
264
                $fileID = $file->file_id;
265
                $file->delete();
266
267
                //  Delete the file if it is no longer in use
268
                Files::deleteFile($fileID);
269
            }
270
        }
271
        
272
        FileLinks::find($id)->delete();
273
        
274
        Log::info('File link deleted', ['link_id' => $id, 'user_id' => Auth::user()->user_id]);
275
    }
276
}
277