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

UserLinksController::downloadAllFiles()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 1
dl 0
loc 26
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use Zip;
6
use Exception;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Mail;
9
use Illuminate\Support\Facades\Log;
10
use Illuminate\Support\Facades\Notification;
11
use App\Files;
12
use App\FileLinks;
13
use App\FileLinkFiles;
14
use App\FileLinkNotes;
15
use App\User;
16
use App\Notifications\NewFileUploaded;
17
18
class UserLinksController extends Controller
19
{
20
    public function index()
21
    {
22
        echo 'file link home';
23
    }
24
    
25
    public function details($id)
26
    {
27
        $details = FileLinks::where('link_hash', $id)->first();
28
        
29
        //  Verify that the link is valid
30
        if(empty($details))
31
        {
32
            return view('links.guest.badLink');
33
        }
34
        //  Verify that the link has not expired
35
        else if($details->expire <= date('Y-m-d'))
36
        {
37
            return view('links.guest.expiredLink');
38
        }
39
        
40
        $files = FileLinkFiles::where('link_id', $details->link_id)
41
            ->where('upload', false)
42
            ->join('files', 'file_link_files.file_id', '=', 'files.file_id')
43
            ->get();
44
        
45
        return view('links.guest.details', [
46
            'hash'    => $id,
47
            'details' => $details,
48
            'files'   => $files,
49
            'allowUp' => $details->allow_upload
50
        ]);
51
    }
52
    
53
    //  Downalod all files available to user
54
    public function downloadAllFiles($hash)
55
    {
56
        $details = FileLinks::where('link_hash', $hash)->first();
57
        
58
        //  Verify that the link is valid
59
        if(empty($details))
60
        {
61
            return abort(404);
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort(404) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
62
        }
63
        
64
        $files = FileLinkFiles::where('link_id', $details->link_id)
65
            ->where('upload', false)
66
            ->join('files', 'file_link_files.file_id', '=', 'files.file_id')
67
            ->get();
68
        
69
        $path = config('filesystems.disks.local.root').DIRECTORY_SEPARATOR;
70
        
71
        $zip = Zip::create($path.'download.zip');
72
        foreach($files as $file)
73
        {
74
            $zip->add($path.$file->file_link.$file->file_name);
75
        }
76
        
77
        $zip->close();
78
        
79
        return response()->download($path.'download.zip')->deleteFileAfterSend(true); 
80
    }
81
    
82
    public function uploadFiles($hash, Request $request)
83
    {
84
        $request->validate(['name' => 'required', 'file' => 'required']);
85
            
86
        $details = FileLinks::where('link_hash', $hash)->first();
87
        
88
        $filePath = config('filesystems.paths.links').DIRECTORY_SEPARATOR.$details->link_id;
89
90
        foreach($request->file as $file)
91
        {
92
            //  Clean the file and store it
93
            $fileName = Files::cleanFilename($filePath, $file->getClientOriginalName());
94
            $file->storeAs($filePath, $fileName);
95
96
            //  Place file in Files table of DB
97
            $newFile = Files::create([
98
                'file_name' => $fileName,
99
                'file_link' => $filePath.DIRECTORY_SEPARATOR
100
            ]);
101
            $fileID = $newFile->file_id;
102
103
            //  Place the file in the file link files table of DB
104
            FileLinkFiles::create([
105
                'link_id'  => $details->link_id,
106
                'file_id'  => $fileID,
107
                'added_by' => $request->name,
108
                'upload'   => 1
109
            ]);
110
            
111
            if(!empty($request->note))
112
            {
113
                FileLinkNotes::create([
114
                    'link_id' => $details->link_id,
115
                    'file_id' => $fileID,
116
                    'note'    => $request->note
117
                ]);
118
            }
119
        }
120
        
121
        //  Send email and notification to the creator of the link
122
        $user = User::find($details->user_id);
123
        Notification::send($user, new NewFileUploaded($details));
124
        
125
        return $request->all();
126
    }
127
}
128